0% found this document useful (0 votes)
10 views

Sample Practical File2

Uploaded by

attarimohsin56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Sample Practical File2

Uploaded by

attarimohsin56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

PRACTICAL FILE

NAME : ADITYA CHOWDHURY


CLASS : X
SEC : A
ROLL NO : 9
SUBJECT : COMPUTER
YEAR : 2020-21
INDEX
S NO PROGRAMS PAGE
NO
1. Program in Java to input a number and check whether it is a Harshad
1-2
Number or Niven Number or not.
2. Program in Java to input 2 numbers and find their Greatest Common Divisor (GCD).
3-4
3. Program in Java to input a number and check whether it is an Automorphic Number
5-6
or not.
4. Program to accept a two-digit number. Add the sum of its digits to the product of its
7-8
digits. If the value is equal to the number , output the “special-two digit number”
otherwise, output “Not a special two-digit number”.
5. Program to input a number. Count and print the frequency of each digit present
9-10
in that number.
6. Program to input a string (word). Convert it into lowercase letters. Count and
11-13
print the frequency of each alphabet present in the string.
7. Program to find the shortest and the longest word in a sentence and print them
14-16
along
with their length.
8. Program to Create two array A [ ] and B [ ] having size m and n respectively,
17-18
create a third array C [ ] which will merge array A [ ] and B [] together and store in it
and print array C [ ].
9. Program to Search a particular element of an array using Binary Search
19-20
Technique.
10. Program to Input a Sentence which ends with a full stop and print the words of
21-22
the sentence in the following format:- Input: City Of Joy. Output: Joy Of City.
11. Program to accept a positive whole number and find the binary equivalent of the
23-25
number and count the number of 1’s in it and display whether it is a Evil number or
not with an appropriate message.
12. Program to take a word as an input, and then encode it into a Pig Latin.
26-27
13. Program to accept a list of 20 integers. Sort all the numbers in ascending order
27-28
by using 'Bubble Sort' technique.
14. Program in Java to accept the name of an employee and his/her annual income. Pass
29-31
the name and the annual income to a function Tax(String name, int income) which
displays the name of the employee and the income tax as per the given tariff:
15. Write a class with the name Area using function overloading that computes the area
32-34
of a parallelogram, a rhombus and a trapezium.
16. Design a class to overload the function : 1. void display(String str, char ch) 2. void
35-37
display(String str1, String str2) 3. void display(String str, int n)
17. Program to input two numbers and check whether they are twin prime numbers
38-40
or not.
18. Using the switch statement, write a menu driven program for the following:
41-43
(a) To print the Floyd's triangle: (b) To display the following pattern:
19. Design a program to input distance travelled by the passenger. Calculate and display
44-45
the fare to be paid.

20. Write a program to calculate the Sum of given series:


46-47
1 + (x2/2!) + (x3/3!) + (x4/4!) + ...........(xn/n!)
1. Write a Program in Java to input a number and check whether it is a Harshad
Number or Niven Number or not. Harshad Number : In recreational mathematics, a
Harshad number (or Niven number), is an integer (in base 10) that is divisible by the
sum of its digits.The number 18 is a Harshad number in base 10, because the sum of
the digits 1 and 8 is 9 (1+ 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0):

SOURCE CODE:
import java.util.Scanner;

class HarshadNumber

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number : ");

int n = sc.nextInt();

int c = n, d, sum = 0;

//finding sum of digits

while(c>0)

d = c%10;

sum = sum + d;

c = c/10;

if(n%sum == 0)

System.out.println(n+" is a Harshad Number.");

else

System.out.println(n+" is not a Harshad Number.");

}
OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE


n int To accept the number given by user. main()

d int To find the dividend. main()

sum int To calculate the sum of the digit. main()

c int To copy the number and reduce the number main()


to lowest term.
2. Write a Program in Java to input 2 numbers and find their Greatest Common
Divisor (GCD).

SOURCE CODE:
import java.util.Scanner;

class GCD

public static void main(String args[])

Scanner sc =new Scanner (System.in);

System.out.println("ENTER THE FIRST NUMBER");

int n1=sc.nextInt();

System.out.println("ENTER THE SECOND NUMBER");

int n2=sc.nextInt();

int r;

while(n2!=0)

r=n1%n2;

n1=n2;

n2=r;

System.out.println("GCD="+n1);

}
OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE


n1 int To accept the first number given by user main()

n2 int To accept the second number given by user. main()

r int To calculate the GCD of the programme. main()


3. Write a Program in Java to input a number and check whether it is an Automorphic
Number or not. Note: An automorphic number is a number which is present in the
last digit(s) of its square. Example: 25 is an automorphic number as its square is 625
and 25 is present as the last digits.

SOURCE CODE:
import java.util.Scanner;

class AutomorphicNumber

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number");

int num = sc.nextInt();

int c=0, sqr = num*num;

int temp =num; //copying num

//countint digits of num

while(temp>0)

c++;

temp=temp/10;

int lastSquareDigits = (int) (sqr%(Math.pow(10,c)));

if(num == lastSquareDigits)

System.out.println("Automorphic number");

else

System.out.println("Not an Automorphic number");

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
num int To accept the number given by user. main()

c int Count number of digits number has. main()

sqr int To find the square of the number main()

lastSquareDigits int To find last two digit of the square main()

temp int To copy the number main()

4. A special two-digit number is such that when the sum of the digits is added to
the product of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.Sum of digits = 5+9=14 Product of its digits = 5
x 9 = 45 Sum of the digits and product of digits = 14 + 45 = 59.

Write a program to accept a two-digit number. Add the sum of its digits to the
product of its digits. If the value is equal to the number input, output the message
“special-two digit number” otherwise, output the message “Not a special two-
digit number”.

SOURCE CODE:
import java.util.Scanner;

class Special2digitNumber

public static void main(String args[])

int product=1,sum=0,t=0;

Scanner sc =new Scanner(System.in);

System.out.println("ENTER TWO DIGIT NUMBER");

int n=sc.nextInt();

product=(n%10)*(n/10);

sum=(n%10)+(n/10);

t=product+sum;

if(t==n)

System.out.println("SPECIAL TWO DIGIT NUMBER");

else

System.out.println("NOT A SPECIAL TWO DIGIT NUMBER");

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
product int To calculate the product of the digits main()

sum int To calculate the sum of the digits main()

t int To calculate the sum of product of the main()


digits and sum of the digits
n int To initialise a two digit number main()

5. Write a program to input a number. Count and print the frequency of each digit
present in that number. The output should be given as: Sample Input: 44514621
Sample Output: =====================

Digit Frequency

=====================

1 2

2 1

4 3

5 1

6 1

SOURCE CODE:
import java.util.Scanner;

class FrequencyOfEachDigit

public static void main(String args[])

Scanner sc =new Scanner (System.in);

System.out.println("ENTER A NUMBER");

int n=sc.nextInt();

int f[]=new int[10];

for(int i=0;i<10;i++)

f[i]=0;

System.out.println("====================================");

System.out.println(" "+"DIGIT"+"\t"+" "+"FREQUENCY");

System.out.println("====================================");

int d;

while(n>0)

{
d=n%10;

f[d]++;

n=n/10;

for(int i=0;i<10;i++)

if(f[i]!=0)

System.out.println("\t"+i+"\t"+"\t"+f[i]);

OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE


n int To initialise the number main()

f[] int To accept the elements of array main()

i int For running the for loop main()

d int To extract the digit main()

6. Write a program to input a string (word). Convert it into lowercase letters. Count and
print the frequency of each alphabet present in the string. The output should be given
as:

Sample Input: Alphabet: Sample Output:


==========================

Alphabet Frequency

==========================

a 2

b 1

e 1

h 1

l 1

p 1

s 1

t 1

SOURCE CODE:

import java.io.*;

class Frequency_ALPHABET

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter any string :");

String s = br.readLine();

s=s.toLowerCase();

int l=s.length();

char ch;

System.out.println("==========================");

System.out.println("Alphabet\tFrequency");

System.out.println("==========================");

int count=0;

for(char i='a'; i<='z'; i++)


{

count = 0;

for(int j=0; j<l; j++)

ch=s.charAt(j);

if(ch==i)

count++;

if(count!=0)

System.out.println(i+"\t\t"+count);

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
s String To initialise the string main()

l int To initialise the length of the string main()

ch char To initialise the alphabet main()

count int To count the frequency main()

i char To initialise an alphabet main()

j int To run the loop main()

7. Write a program to find the shortest and the longest word in a sentence and print
them along with their length. Sample Input: I am learning Java

Sample Output:
Shortest word = I

Length = 1

Longest word = learning

Length = 8

SOURCE CODE:

import java.io.*;

class Short_long_word

public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter any sentence : ");

String s=br.readLine();

s=s+" ";

int len=s.length();

String x="",maxw="",minw="";

char ch;

int p,maxl=0,minl=len;

for(int i=0;i<len;i++)

ch=s.charAt(i);

if(ch!=' ')

x=x+ch;

else

{
p=x.length();

if(p<minl)

minl=p;

minw=x;

if(p>maxl)

maxl=p;

maxw=x;

x="";

System.out.println("Shortest word = "+minw+"\nLength = "+minl);

System.out.println("Longest word = "+maxw+"\nLength = "+maxl);

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
s String To accept the line main()

len int To store the length of the line. main()

maxw String To store the maximum word present main()

minw String To store the minimum word present main()

ch char To store the alphabets main()

x String To calculate the position of each main()


alphabets
p int To store the length of x. main()

maxl int To store the length of the maximum main()


word present
minl int To store the length of the minimum main()
word present
8. Write a Program to Create two array A [ ] and B [ ] having size m and n respectively,
create a third array C [ ] which will merge array A [ ] and B [] together and store in it
and print array C [ ].

SOURCE CODE:
import java.util.*;

class MERGE_2_ARRAY

public static void main(String args[])

int m,n,i;

Scanner in = new Scanner(System.in);

System.out.println("ENTER THE SIZE OF THE TWO ARRAYS");

m = in.nextInt();

n = in.nextInt();

int A[] = new int[m];

int B[] = new int[n];

System.out.println("ENTER THE FIRST ARRAY ELEMENTS");

for(i = 0;i < m;i++)

A[i]=in.nextInt();

System.out.println("ENTER THE ARRAY ELEMENTS OF SECOND ARRAY");

for(i=0;i<n;i++)

B[i]=in.nextInt();

System.out.println("THE THIRD ARRAY AFTER MERGING FIRST AND SECOND ARRAY");

int C[]=new int[m+n];

for(i=0;i<m;i++)

C[i]=A[i];

for(i=0;i<n;i++)

{
C[m+i]=B[i];

for(i=0;i<(m+n);i++)

System.out.println(""+C[i]);

OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE

m int To accept length of array A[]. main()

n int To accept length of array B[]. main()

i int For running the for loop main()

A[] int To accept first array main()

B[] int To accept second array main()

C[] int Array after merging array A[] and B[] main()
9. Write a Program to Search a particular element of an array using Binary Search
Technique.

SOURCE CODE:
import java.util.Scanner;

class Binary_SEARCH

public static void main(String args[])

int counter, num, item, array[], first, last, middle;

Scanner input = new Scanner(System.in);

System.out.println("Enter number of elements:");

num = input.nextInt();

array = new int[num];

System.out.println("Enter " + num + " integers");

for (counter = 0; counter < num; counter++)

array[counter] = input.nextInt();

System.out.println("Enter the search value:");

item = input.nextInt();

first = 0;

last = num - 1;

middle = (first + last)/2;

while( first <= last )

if ( array[middle] < item )

first = middle + 1;

else if ( array[middle] == item )

System.out.println(item + " found at location " + (middle + 1) + ".");

break;
}

else

last = middle - 1;

middle = (first + last)/2;

if ( first > last )

System.out.println(item + " is not found.\n");

OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE


counter int To run the loop. main()

num int To accept the length of array main()

item int Accept searching number given by main()


user.
first int To find first digit main()

last int To find last digit main()

middle int To find the middle term main()

array[] int To Accept the array main()

10. Write a Program to Input a Sentence which ends with a full stop and print the
words of the sentence in the following format:-
Input: City Of Joy.

Output: Joy Of City.

SOURCE CODE:
import java.util.Scanner;

class reverse

public static void main(String args[])

String s,w=" ",r=" ";

char x=' ';

int l,i;

Scanner sc = new Scanner(System.in);

System.out.println("ENTER A SENTENCE");

s=sc.nextLine();

s=s+" ";

l=s.length();

for(i=0;i<l-1;i++)

x=s.charAt(i);

if(x!=' ')

w=w+x;

else

r=w+r;

w=" ";

System.out.println("THE SENTENCE IN REVERSE ORDER IS :"+r+".");

}
OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE

s String To accept the line given by user main()

w String To store the words main()

r String To store the words main()

x char To store the alphabets main()

l int To store the position number of each main()


alphabets
i int To run the loop main()

11. Write a Program in Java to input a number and check whether it is an Evil Number
or not. Evil Number : An Evil number is a positive whole number which has even
number of 1’s in its binary equivalent. Example: Binary equivalent of 9 is 1001, which
contains even number of 1’s. A few evil numbers are 3, 5, 6, 9....
Design a program to accept a positive whole number and find the binary equivalent of
the number and count the number of 1’s in it and display whether it is a Evil number or
not with an appropriate message. Output the result in format given below:

INPUT : 15

BINARY EQUIVALENT : 1111

NO. OF 1’s : 4

OUTPUT : EVIL NUMBER

SOURCE CODE:
import java.util.*;

class EvilNumber

String toBinary(int n)

int r;

String s="";

char dig[]={'0','1'};

while(n>0)

r=n%2;

s=dig[r]+s;

n=n/2;

return s;

int countOne(String s)

int c = 0, l = s.length();

char ch;
for(int i=0; i<l; i++)

ch=s.charAt(i);

if(ch=='1')

c++;

return c;

public static void main(String args[])

EvilNumber ob = new EvilNumber();

Scanner sc = new Scanner(System.in);

System.out.print("Enter a positive number : ");

int n = sc.nextInt();

String bin = ob.toBinary(n);

System.out.println("Binary Equivalent = "+bin);

int x = ob.countOne(bin);

System.out.println("Number of Ones = "+x);

if(x%2==0)

System.out.println(n+" is an Evil Number.");

else

System.out.println(n+" is Not an Evil Number.");

}
OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE


r int To divide by 2. In the function String
toBinary(int n)
s string To find the sum. In the function String
toBinary(int n)
n int To accept the number given by user. In main function and
String toBinary(int n)
ch char To accept the data. In the function
countOne(String s)
c int To run the counter. In the function
countOne(String s)
l int To store the position of each. In the function
countOne(String s)
i int To run the loop In the function
countOne(String s)
x int To pass the value to the function. In the main function

dig[] char To store default 1 and 0. In the function String


toBinary(int n)
12. Design a program to take a word as an input, and then encode it into a Pig Latin. A
Pig Latin is an encrypted word in English, which is generated by doing following
alterations: The first vowel occurring in the input word is placed at the start of the new
word along with the remaining alphabets of it. The alphabets present before the first
vowel are shifted at the end of the new word followed by “ay”.

Examples:
Input: s = "paris"

Output: arispay

Input: s = "amazon"

Output: amazonay

SOURCE CODE:
import java.util.Scanner;

class PigLatin

public static void main(String args[])

Scanner in=new Scanner(System.in);

System.out.println("ENTER A WORD");

String s=in.nextLine();

s=s.toLowerCase();

int i;

for(i=0;i<s.length();i++)

char ch=s.charAt(i);

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

break;

String f=s.substring(i)+s.substring(0,i)+"ay";

System.out.println(f);

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE

s String To store the word given by user main()

i int To run the loop. main()

ch char To check the vowel. main()

f String To make word and ad ay at end. main()

13. Write a program to accept a list of 20 integers. Sort all the numbers in ascending
order by using 'Bubble Sort' technique.

SOURCE CODE:
import java.util.Scanner;
class BubbleSort

public static void main(String []args)

int num, i, j, temp;

Scanner input = new Scanner(System.in);

System.out.println("Enter the number of integers to sort:");

num = input.nextInt();

int array[] = new int[num];

System.out.println("Enter " + num + " integers: ");

for (i = 0; i < num; i++)

array[i] = input.nextInt();

for (i = 0; i < ( num - 1 ); i++)

for (j = 0; j < num - i - 1; j++)

if (array[j] > array[j+1])

temp = array[j];

array[j] = array[j+1];

array[j+1] = temp;

System.out.println("Sorted list of integers:");


for (i = 0; i < num; i++)

System.out.println(array[i]);

OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE


i int To run the loop. main()

j int To run the loop. main()

num int To store the range which is 20. main()

temp int To check grater then 20 or not main()

array[] int To accept the numbers in array. main()

14. Write a program in Java to accept the name of an employee and his/her annual
income. Pass the name and the annual income to a function Tax(String name, int
income) which displays the name of the employee and the income tax as per the given
tariff: Annual Income Income Tax

Up to ₹2,50,000 No tax
₹2,50,001 to ₹5,00,000 10% of the income exceeding ₹2,50,000

₹5,00,001 to ₹10,00,000 ₹30,000 + 20% of the amount exceeding ₹5,00,000

₹10,00,001 and above ₹50,000 + 30% of the amount exceeding ₹10,00,000

SOURCE CODE:
import java.util.Scanner;

public class EmployeeTax

public void tax(String name, int income) {

double tax;

if (income <= 250000)

tax = 0;

else if (income <= 500000)

tax = (income - 250000) * 0.1;

else if (income <= 1000000)

tax = 30000 + ((income - 500000) * 0.2);

else

tax = 50000 + ((income - 1000000) * 0.3);

System.out.println("Name: " + name);

System.out.println("Income Tax: " + tax);

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter name: ");

String n = in.nextLine();

System.out.print("Enter annual income: ");

int i = in.nextInt();
EmployeeTax obj = new EmployeeTax();

obj.tax(n, i);

OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE

i int To accept amount. main()

n int To accept name. main()

tax Double To calculate the tax to be paid. void tax(String


name, int income)

15. Write a class with the name Area using function overloading that computes the area
of a parallelogram, a rhombus and a trapezium.

Formula: Area of a parallelogram (pg) = base * ht

Area of a rhombus (rh) = (1/2) * d1 * d2 (where, d1 and d2 are the diagonals)


Area of a trapezium (tr) = (1/2) * ( a + b) * h(where a and b are the parallel
sides, h is the perpendicular distance between the parallel sides)

SOURCE CODE:

import java.util.Scanner;

public class Area

public double area(double base, double height) {

return base * height;

public double area(double c, double d1, double d2) {

return c * d1 * d2;

public double area(double c, double a, double b, double h) {

return c * (a + b) * h;

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

Area obj = new Area();

System.out.print("Enter base of parallelogram: ");

double base = in.nextDouble();

System.out.print("Enter height of parallelogram: ");

double ht = in.nextDouble();

System.out.println("Area of parallelogram = " + obj.area(base, ht));


System.out.print("Enter first diagonal of rhombus: ");

double d1 = in.nextDouble();

System.out.print("Enter second diagonal of rhombus: ");

double d2 = in.nextDouble();

System.out.println("Area of rhombus = " + obj.area(0.5, d1, d2));

System.out.print("Enter first parallel side of trapezium: ");

double a = in.nextDouble();

System.out.print("Enter second parallel side of trapezium: ");

double b = in.nextDouble();

System.out.print("Enter height of trapezium: ");

double h = in.nextDouble();

System.out.println("Area of trapezium = " + obj.area(0.5, a, b, h));

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE

base double To accept the base of parallelogram main()

ht double To accept the height of parallelogram main()

d1 double To accept the diagonal of rhombus. main()

d2 double To accept the second diagonal of main()


rhombus.
a double To accept the first parallel side of main()
trapezium.
b double To accept the second parallel side of main()
trapezium.
h double To accept the height of trapezium. main()

16. Design a class to overload the function display(.....) as follows:

1. void display(String str, char ch) — checks whether the word str contains the letter ch
at the beginning as well as at the end or not. If present, print 'Special Word'

otherwise print 'No special word'.

2. void display(String str1, String str2) — checks and prints whether both the words
are equal or not.

3. void display(String str, int n) — prints the character present at nth position in the

word str.

Write a suitable main() function.

SOURCE CODE:
import java.util.Scanner;

class OVERLOAD

public void display(String str,char ch)

char c1,c2;

c1=str.charAt(0);

c2=str.charAt(str.length()-1);

if(c1==ch && c2==ch)

System.out.println("SPECIAL WORD");

else

System.out.println(" NOT A SPECIAL WORD");

public void display(String str1,String str2)

if(str1.equals(str2)==true)

System.out.println("STRINGS ARE EQUAL");

else

System.out.println("STRINGS ARE NOT EQUAL");

public void display(String str,int n)


{

char c3;

c3=str.charAt(n);

System.out.println("CHARACTER AT POSITION "+n+"OF THE WORD IS "+c3);

public static void main(String args[])

OVERLOAD obj=new OVERLOAD();

Scanner sc = new Scanner (System.in);

System.out.println("ENTER A WORD");

String s =sc.nextLine();

System.out.println("ENTER THE CHARACTER TO BE CHECKED");

char c=sc.next().charAt(0);

obj.display(s,c);

System.out.println("ENTER THE FIRST STRING TO BE CHECKED FOR EQUALITY");

String x=sc.nextLine();

System.out.println("ENTER THE SECOND STRING TO BE CHECKED ");

String s2=sc.nextLine();

obj.display(x,s2);

System.out.println("ENTER THE WORD ");

String s3=sc.nextLine();

System.out.println("ENTER THE POSITION OF CHARACTER EXTRACTION");

int n=sc.nextInt();

obj.display(s3,n);

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
c1 char To extract the character public void
display(String
str,char ch)
c2 char To extract the character public void
display(String
str,char ch)
c3 char To extract the character public void
display(String str,int
n)
s String To take the word from the user main(0

x String To take the string from the user main()

s2 String To take the string from the user main()

s3 String To take the word from the user main()

c char To take a character from the user main()

n int To take the position of the character main()


from the user

17. Write a program to input two numbers and check whether they are twin prime
numbers or not.
Hint: Twin prime numbers are the prime numbers whose difference is 2.

For example: (5,7), (11,13), ....... and so on.

SOURCE CODE:
import java.util.Scanner;

public class TwinPrime

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter first number: ");

int a = in.nextInt();

System.out.print("Enter second number: ");

int b = in.nextInt();

boolean isAPrime = true;

for (int i = 2; i <= a / 2; i++) {

if (a % i == 0) {

isAPrime = false;

break;

if (isAPrime && Math.abs(a - b) == 2) {

boolean isBPrime = true;


for (int i = 2; i <= b / 2; i++) {

if (b % i == 0) {

isBPrime = false;

break;

if (isBPrime)

System.out.println(a + " and " + b + " are twin prime");

else

System.out.println(a + " and " + b + " are not twin prime");

else

System.out.println(a + " and " + b + " are not twin prime");

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE

i int To run the loop main()

a int To accept first number. main()

b int To accept second number. main()

18. Using the switch statement, write a menu driven program for the following:

(a) To print the Floyd's triangle:


1

23

456

7 8 9 10

11 12 13 14 15

(b) To display the following pattern:

IC

ICS

ICSE

For an incorrect option, an appropriate error message should be displayed.

SOURCE CODE:

import java.util.Scanner;

class Pattern

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Type 1 for Floyd's triangle");

System.out.println("Type 2 for an ICSE pattern");

System.out.print("Enter your choice: ");

int ch = in.nextInt();

switch (ch) {

case 1:

int a = 1;

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {


System.out.print(a++ + "\t");

System.out.println();

break;

case 2:

String s = "ICSE";

for (int i = 0; i < s.length(); i++) {

for (int j = 0; j <= i; j++) {

System.out.print(s.charAt(j) + " ");

System.out.println();

break;

default:

System.out.println("Incorrect Choice");

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE

i int To run the loop main()

j int To run the loop main()

ch int To select the case main()

19. An air-conditioned bus charges fare from the passengers based on the distance
travelled as per the tariff given below:
Distance Travelled Fare

Up to 10 km Fixed charge ₹80

11 km to 20 km ₹6/km

21 km to 30 km ₹5/km

31 km and above ₹4/km

Design a program to input distance travelled by the passenger. Calculate and display
the fare to be paid.

SOURCE CODE:
import java.util.Scanner;

public class BusFare

public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter distance travelled: ");

int dist = in.nextInt();

int fare = 0;

if (dist <= 0)

fare = 0;

else if (dist <= 10)

fare = 80;

else if (dist <= 20)

fare = 80 + (dist - 10) * 6;

else if (dist <= 30)

fare = 80 + 60 + (dist - 20) * 5;

else if (dist > 30)


fare = 80 + 60 + 50 + (dist - 30) * 4;

System.out.println("Fare = " + fare);

OUTPUT:

VARIABLE DATATYPE PURPOSE SCOPE

dist int To accept the distance travelled main()

fare int To calculate the total fare. main()

20. Write a program to calculate the Sum of given series:

1 + (x2/2!) + (x3/3!) + (x4/4!) + ...........(xn/n!)


SOURCE CODE:

import java.util.Scanner;

class SERIES

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int x,n,f=1;

double sum=0.0;

System.out.println("ENTER THE VALUES OF X AND N");

x=sc.nextInt();

n=sc.nextInt();

for(int i=1;i<= n;i++)

for(int j=1;j<=i;j++)

f=f*j;

sum=sum+(Math.pow(x,i))/f;

System.out.println("THE SUM IS"+sum);

OUTPUT:
VARIABLE DATATYPE PURPOSE SCOPE
x int To accept value from user main()

n int To accept value of n terms main()

f int To calculate the factorial main()

sum double To calculate the sum main()

i int To run the loop main()


SIGNATURE

INTERNAL EXTERNAL

You might also like