Ashutosh Behera - 12 B - IsC Computer Science Practical File
Ashutosh Behera - 12 B - IsC Computer Science Practical File
1|Page
Menu-driven program to find area of
Program 13 equilateral isosceles or scalene 42
triangles.
Menu driven program to calculate
gross salary net salary and check
Program 14 45
whether an employee has to pay
Income Tax or not.
Menu Driven program for finding
Program 15 discounted price based on type of 49
room and number of days of stay.
Menu Driven program to perform
Program 16 arithmetic operation on numbers 53
input by user.
Program to calculate the sum of
positive even numbers and negative
Program 17 56
odd numbers and accept 0 to quit
and display the result.
Accept a number and print the
Program 18 58
factorial using do while loop.
Check whether a number input by
Program 19 60
the user is even or odd.
Accept a number and print the odd
Program 20 62
numbers below it.
2|Page
To input and check whether a
Program 30 82
number is happy number or not.
To input and check whether a
Program 31 number is prime number or not and 84
display 10 prime number after it.
To print given patterns using switch
Program 32 88
case.
To print given pattern using switch
Program 33 91
case.
To print given pattern using switch
Program 34 95
case.
To print given pattern using switch
Program 35 98
case.
To print given pattern using switch
Program 36 102
case.
To input a number and check
Program 37 whether it is an automorphic number 106
or not.
To check whether the input code is a
Program 38 108
legal ISBN code.
To print the sum of pattern using
Program 39 111
switch case.
To print the sum of pattern using
Program 40 114
switch case.
To print the sum of pattern using
Program 41 117
switch case.
To convert a decimal number to
Program 42 binary number and vice-versa using 120
switch case.
To input a number and print a
pattern of consecutive natural
Program 43 123
numbers which add up to the
number itself.
To input a number between 1 to 5
Program 44 and display the required patterns 126
and number of patterns.
To round off a given number to
Program 45 certain number of digits as per user’s 129
choice.
To accept a word from the user and
Program 46 131
print the given pattern.
3|Page
Accept three words and perform the
Program 47 133
given tasks.
Accept a sentence and count and
Program 48 remove the words with repeating 137
letters.
To accept a sentence and pint the
Program 49 new sentence after performing the 140
given tasks.
To accept a sentence and change
Program 50 each consonant to its previous letter 143
provided that letter is not a vowel.
To accept a sentence and perform
Program 51 146
the given tasks.
To accept a word and print the
Program 52 151
patterns using switch case.
To accept 10 numbers in a single
Program 53 dimensional array and find a number 154
and print appropriate message.
To sort an array using selection sort
Program 54 156
technique.
To sort an array using bubble sort
Program 55 159
technique.
To merge two single dimensional
Program 56 162
arrays.
Program 57 To insert an element into an array. 165
To delete an element from the given
Program 58 array according to user input 167
position.
To accept 15 names and display the
names that start with the letter
Program 59 169
accepted and arrange given array in
alphabetical order.
To accept the seven wonders and
their respective countries in two
Program 60 different arrays and search for the 172
country in the array and display the
respective wonder.
To accept numbers in DDA and find
Program 61 174
sum of left and right diagonal.
To accept numbers in DDA and find
Program 62 177
sum of outer and inner elements.
4|Page
To accept the elements DDA and
Program 63 181
remove corner elements.
To accept elements of a matrix and
Program 64 arrange the rows of the array as per 184
in the question.
To accept the elements of a matrix
and print the sum of each column
Program 65 187
and row along with the elements of
that row and column.
To accept the elements of a matrix
and print the elements which are
Program 66 191
below the left and right diagonals of
the matrix.
To accept the elements of the matrix
Program 67 195
and print its mirror image.
To accept the elements of a matrix
and print the matrix after shifting
Program 68 198
each element to the next closest
prime number.
To accept integer numbers in a
Program 69 matrix and convert all elements into 203
roman numerals.
To accept elements of a matrix and
Program 70 206
check if it is a magic square.
To accept elements of matrix and
Program 71 display maximum and minimum 210
numbers. Then, sort the array.
Accept elements of single
dimensional array and search for
Program 72 number using binary search 215
technique. Perform this using
recursive function.
Accept a number and check if it is a
Program 73 218
smith number or not.
Accept a number and check if it is
Program 74 222
circular prime or not.
To accept an amount from user and
Program 75 print denomination in terms of rupee 226
notes.
Accept elements of a matrix, rotate it
Program 76 90 degrees clockwise, and find the 228
sum of corner elements.
5|Page
Accept a string and check its validity
Program 77 and then print number of vowels and 232
consonants in the given format.
To accept lower and upper limits of
Program 78 numbers and print those numbers 237
which have no repetition of digits.
6|Page
PROGRAMS
1. Program 1
Execute a program to input three sides of a triangle and check whether the triangle is possible
or not. If possible, then display whether the triangle is scalene, isosceles, or equilateral.
Otherwise, display the triangle is not possible.
Code:
import java.util.*;
double a, b, c;
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
7|Page
{
System.out.println("Triangle is possible.");
else if((a==b && b!=c) || (a==c && a!=b) || (b==c && b!=a))
else
else
Output:
8|Page
2. Program 2
A clock showroom has announced the following festival discount and assured gifts on the
purchase of items on the basis of cost of purchase. Execute a program to input total amount
of purchase and print the assured gift, discount, and final bill to be paid.
Code:
import java.util.*;
9|Page
public static void main(String args[ ])
TP = sc.nextDouble();
if(TP<=2000)
Disc = 5.0;
Disc = 10.0;
Disc = 15.0;
}
10 | P a g e
if(TP>10000)
Disc = 20.0;
DP = TP * Disc/100.0;
Amt= TP - DP;
Output:
11 | P a g e
12 | P a g e
3. Program 3
An ICSE school displays a notice regarding admission for grade 11 for choosing different
stream according to marks obtained in English, Math, and Science in ICSE exam. Print the
appropriate stream allotted to the candidate. Execute a program to accept marks from the
user. Calculate the average and then print the stream for the candidate.
Marks Stream
Code:
import java.util.*;
13 | P a g e
eng = sc.nextDouble();
maths = sc.nextDouble();
sci = sc.nextDouble();
14 | P a g e
Output:
15 | P a g e
4. Program 4
The electricity board charges from their consumers according to the units consumed. Execute
a program to input consumer name, consumer number and units consumed. Calculate and
print the amount to be paid according to units consumed.
The amount to be paid is as per the tariff given below:
Code:
import java.util.*;
16 | P a g e
long number = sc.nextLong();
else
amt = (100 * 5.5) + (200 * 6.5) + (300 * 7.5) + ((units - 600) * 8.5);
17 | P a g e
Output:
18 | P a g e
5. Program 5
Given below is the table by which the income tax of a person is calculated. If age is more than
65 years or gender is female, then display wrong category. If age is less than or equal to 65
then compute the income tax payable. Execute a program to input gender, age and taxable
income of a person and compute and print the tax to be paid.
If it is greater than Rs.160000 and less than (Taxable income - 160000) * 10%
or equal to Rs.500000
If it is greater than Rs.500000 and less than [(Taxable income - 500000) * 20%] + Rs.34000
or equal to 800000
Code:
import java.util.*;
System.out.println("Enter Gender(Male/Female):");
19 | P a g e
System.out.println("Enter Age: ");
double ti = sc.nextDouble();
System.out.println("Wrong Category.");
else
tax = 0.0;
else
20 | P a g e
}
Output:
21 | P a g e
6. Program 6
Execute a program to accept the weight of parcel and type of mail (O for ordinary mail and E
for express mail). Then calculate and print the charge of courier under each category as per
the table given below:
From 101 grams to 500 grams Rs.40 per 100 grams Rs.70 per 100 grams
501 grams and above Rs.35 per 100 grams Rs.65 per 100 grams
Code:
import java.util.*;
double wt = in.nextDouble();
System.out.println("Enter type of booking, 'O' for Ordinary Mail or 'E' for Express Mail : ");
double charge = 0;
22 | P a g e
if(type == 'O')
charge = 50;
else
charge = 80;
else
23 | P a g e
else
Output:
24 | P a g e
7. Program 7
Given below is a table for calculating fine to be paid when a book is returned late to the
library. Execute a program in Java (with relevant supporting material) to calculate assuming
that a book is returned n days late.
Code:
import java.util.*;
class Prog_7
int n;
double fine;
n = sc.nextInt();
if(n<=5)
25 | P a g e
fine=0.4*n;
else if(n<=10)
fine=(0.4*5)+(n-5)*0.65;
else
fine=(0.4*5)+(0.65*5)+(n-10)*0.80;
System.out.println("Fine="+fine);
Output:
26 | P a g e
8. Program 8
Execute a menu-driven program to find the area of circle, square, rectangle as given below:
(i) Area of circle= pie*radius*radius.
(ii) Area of square= side*side.
(iii) Area of rectangle=length*breadth.
The choice should be given as:
(i) c for area of circle
(ii) s for area of square
(iii) r for area of rectangle.
Code:
import java.util.*;
switch(choice)
case 'c':
27 | P a g e
double r = sc.nextDouble();
break;
case 's':
break;
case 'r':
double l = sc.nextDouble();
double b = sc.nextDouble();
double ra = l * b;
break;
default:
System.out.println("Invalid input.");
28 | P a g e
}
Output:
29 | P a g e
9. Program 9
Execute a program using switch case to find the volume of cube, sphere or cuboid. For an
incorrect choice, display an appropriate error message.
(i) Volume of cube= side*side*side.
(ii) Volume of sphere= 4*pie*r^3/3.
(iii) Volume of cuboid= length*breadth*height.
Code:
import java.util.*;
switch(choice)
case 1:
double cs = sc.nextDouble();
break;
case 2:
double r = sc.nextDouble();
break;
case 3:
double l = sc.nextDouble();
double b = sc.nextDouble();
double h = sc.nextDouble();
double vol = l * b * h;
break;
default:
System.out.println("Invalid input.");
31 | P a g e
}
Output:
32 | P a g e
10. Program 10
You want to buy an old car from ‘Sales and Purchase’, where you can get a car in its
depreciated price. The depreciation value of a car is calculated on its showroom price and the
number of years it has been used. The depreciated value of a car is calculated as per the tariff
given below:
1 10%
2 20%
3 30%
4 50%
Code:
import java.util.*;
int year;
33 | P a g e
System.out.println("Enter showroom price:");
price = sc.nextDouble();
year = sc.nextInt();
if(year == 1)
dep = price*0.1;
else if(year == 2)
dep = price*0.2;
else if(year == 3)
dep = price*0.3;
else if(year == 4)
dep = price*0.5;
dep = price*0.6;
34 | P a g e
Output:
35 | P a g e
11. Program 11
The relative velocity of two trains travelling in opposite directions is calculated by adding their
velocities. In case the trains are travelling in the same direction, the relative velocity is the
difference between the velocities of the two trains. Execute a menu-driven program to input
velocity and length of the trains. Calculate the relative velocity and time taken to cross each
other by indicating 1 for same direction and two for opposite direction.
Code:
import java.util.*;
switch(choice)
case 1:
break;
case 2:
break;
default:
System.out.println("The time taken by the trains to cross each other is: " + time);
37 | P a g e
Output:
38 | P a g e
12. Program 12
An electronic shop has announced the following essential discount on the purchase of certain
items. Execute a menu-driven program based on above criteria to input name, address,
amount of purchase (‘L’ for laptop and ‘D’ for desktop). Compute and print the net amount
to be paid with name and address.
Code:
import java.util.*;
39 | P a g e
System.out.println("Enter Amount of Purchase: ");
type = Character.toUpperCase(type);
if(type == 'L')
disc = 0;
else
40 | P a g e
else if (amt>25000 && amt <= 50000)
else
else
41 | P a g e
Output:
42 | P a g e
13. Program 13
Execute a menu-driven program to find area of equilateral, isosceles and scalene triangles.
(i) Area of equilateral triangle= sqrt (3) * a * a/4
(ii) Area of Isosceles triangle= ¼ * b * sqrt (4a^2 - b^2)
(iii) Area of scalene triangle= sqrt (s * (s-m) * (s-n) * (s-p))
Code:
import java.util.*;
int c;
double a,s,p,q,r,b,s1,s2,s3,sp=0.0;
double area;
c = sc.nextInt();
switch(c)
case 1:
43 | P a g e
System.out.println("Enter side of the equilateral triangle:");
s = sc.nextDouble();
area = Math.sqrt(3*s*s)/4;
break;
case 2:
a = sc.nextDouble();
b = sc.nextDouble();
break;
case 3:
s1 = sc.nextDouble();
s2 = sc.nextDouble();
s3 = sc.nextDouble();
sp = (s1 + s2 + s3)/2.0;
area = Math.sqrt(sp*(sp-s1)*(sp-s2)*(sp-s3));
break;
default:
44 | P a g e
System.out.println("Invalid input. Input 1,2 or 3 to find the area of the respective triangle.");
Output:
45 | P a g e
14. Program 14
Enter the record of the employees of a company as employee number, age and basic salary.
Gross Salary= Basic + DA + HRA.
Net Salary= Gross Salary - (PF+EPF)
Enter ‘M’ or ‘F’ to check the category to decide whether an employee has to pay Income Tax
Payer or not. Print employee number, gross salary, net salary and also an Income Tax payer
or not.
Execute a program to calculate monthly gross salary and net salary of each employee as
follows:
The program further checks whether an employee is an Income Tax payer or no, as under:
Code:
import java.util.*;
46 | P a g e
{
double bs = sc.nextDouble();
da = (bs * 25.0)/100.0;
pf = (bs * 12.0)/100.0;
gs = bs + da + hra;
47 | P a g e
System.out.println("Enter 'M' for male or 'F' for female: ");
char c = sc.next().charAt(0);
if(c == 'M')
else
if(c == 'F')
else
48 | P a g e
Output:
49 | P a g e
15. Program 15
A hotel is giving a seasonal discount on the total amount to be paid by the person staying.
Execute a program to input the name of the guest, category (‘S’ for Semi-Deluxe Room, ‘D’
for Deluxe Room and ‘SD’ for Super Deluxe Room) and number of days stayed in the hotel.
Calculate the discount and total amount to be paid. Print the bill along with the name.
The charges for different rooms are given below:
Category Tariff
Up to 3 days 10%
50 | P a g e
Code:
import java.util.*;
if (days <= 3)
dp = 10;
dp = 15;
dp = 20;
else
51 | P a g e
dp = 30;
if (cat.equalsIgnoreCase("S"))
t = 2500;
else if (cat.equalsIgnoreCase("D"))
t = 3500;
else if (cat.equalsIgnoreCase("SD"))
t = 5000;
else
amt = t * days;
52 | P a g e
Output:
53 | P a g e
16. Program 16
Execute a menu driven program to perform arithmetical operations on numbers input by the
user according to his choice. Addition will be indicated as ‘1’, subtraction will be indicated as
‘2’, multiplication will be indicated as ‘3’ and division will be indicated as ‘4’.
Code:
import java.util.*;
double x = sc.nextInt();
double y = sc.nextInt();
System.out.println("1)Addition.");
System.out.println("2)Subtraction.");
System.out.println("3)Multiplication.");
System.out.println("4)Division.");
int n = sc.nextInt();
switch(n)
54 | P a g e
{
case 1:
double sum;
sum = x + y;
break;
case 2:
double sub;
sub = x - y;
break;
case 3:
double mult;
mult = x * y;
break;
case 4:
double div;
div = x/y;
break;
default:
55 | P a g e
System.out.println("Invalid input. Enter either 1,2,3 or 4 for respective operation.");
Output:
56 | P a g e
17. Program 17
Execute a program to calculate the sum of all positive even numbers and all negative odd
numbers. Write zero to quit the program and display the result.
Code:
import java.util.*;
System.out.println("Enter the series of numbers for calculating sum of odd numbers and sum
of even numbers of the entered series. ");
while(num!=0)
int a = sc.nextInt();
num = a;
if(num == 0)
break;
if(a%2 == 0)
57 | P a g e
{
if(a > 0)
evensum = evensum + a;
if(a%2 != 0)
if(a>0)
oddsum = oddsum + a;
Output:
58 | P a g e
18. Program 18
Execute a program to accept value of n from a user and calculate and print the factorial of n
using do-while loop.
Code:
import java.util.*;
int fact = 1;
int i = 1;
do
fact = fact * i;
i++;
59 | P a g e
}
Output:
60 | P a g e
19. Program 19
Execute a program to check whether a number input by the user is even or odd.
Code:
import java.util.*;
class Prog_19
if (num % 2 == 0)
else
61 | P a g e
Output:
62 | P a g e
20. Program 20
Execute a program to accept a number and find the odd numbers below it.
Code:
import java.util.*;
System.out.println("Enter a number.");
if (i%2!=0)
63 | P a g e
Output:
64 | P a g e
21. Program 21
Execute a program to print the given pattern.
1
12
123
1234
12345
Code:
import java.util.*;
number=1;
number++;
System.out.println();
65 | P a g e
}
Output:
66 | P a g e
22. Program 22
Execute a program to accept the number and to check whether it is a perfect number or not.
A perfect number is called so when the sum of factors (including 1 and excluding the number
itself) is same as the original number.
Sample Input:6
Sample Output:1+2+3=6
It is a perfect number.
Code:
import java.util.*;
long n, sum=0;
n = sc.nextLong();
int i=1;
if(n % i == 0)
sum = sum + i;
67 | P a g e
i++;
if(sum==n)
else
Output:
68 | P a g e
23. Program 23
Execute a program to accept two numbers and find the greatest common divisor (GCD) of the
two numbers.
Sample Input: 25, 45
Sample Output: GCD of 25,45 is 5.
Code:
import java.util.*;
int x, y, gcd=1;
x = sc.nextInt();
y = sc.nextInt();
gcd = i;
System.out.println("The greatest common divisor of " + x + " and " + y + " is : " + gcd);
69 | P a g e
}
Output:
70 | P a g e
24. Program 24
Execute a program to accept a number and check whether the number is neon or not. A
number is said to be neon if sum of digits of its square is equal to the number itself.
Sample Input: 9
Sample Output: 9*9=81
8+1=9
9 is a neon number.
Code:
import java.util.*;
int sum = 0, n;
n = sc.nextInt();
int square = n * n;
while(square != 0)
71 | P a g e
if(n == sum)
else
Output:
72 | P a g e
25. Program 25
Execute a program in Java to enter a number and check whether the number is Armstrong. A
number is said to be Armstrong number if the sum of cubes of it’s digits is equal to the number
itself.
Sample Input: 153
Sample Output: 13 + 53 + 33 = 1+125+27=153.
153 is an Armstrong number.
Code:
import java.util.*;
n = sc.nextInt();
b = n;
while(n > 0)
a = n % 10;
n = n / 10;
73 | P a g e
if(sum == b)
else
Output:
74 | P a g e
26. Program 26
Execute a program in Java to enter a number and check whether this is a palindrome number.
A number is a palindrome number if the new number obtained after reversing the digits is
the same.
Sample Input: 272
Sample Output: 272. It is a palindrome number.
Code:
import java.util.*;
int num;
num = sc.nextInt();
original = Integer.toString(num);
if (original.equals(reverse))
else
75 | P a g e
System.out.println("Entered number isn't a palindrome.");
Output:
76 | P a g e
27. Program 27
Execute a program to enter ten different numbers. Display the greatest and smallest numbers
from the set of numbers input by the user.
Code:
import java.util.*;
System.out.println("Enter a number.");
int n = sc.nextInt();
n = sc.nextInt();
max = n;
min = n;
77 | P a g e
System.out.println("The greatest number is : " + max);
Output:
78 | P a g e
28. Program 28
Execute a program in Java to display the first n number of terms in the Fibonacci series. In
this series, if the first number is zero and the second number is one, the third number is the
sum of the first two numbers.
Code:
import java.util.*;
int n = sc.nextInt();
System.out.print("The first " + n + " terms of the fibonacci series are : " + a + " " + b + " ");
if(i%2 == 0)
a = sum;
else
79 | P a g e
b = sum;
Output:
80 | P a g e
29. Program 29
Execute a program to input a number and check whether the number is a Niven number or
not. A number is said Niven if it is divisible by the sum of its digits.
Sample Input: 126
Sample Output: 1+2+6=9
126/9=14 hence 126 is a Niven number.
Code:
import java.util.*;
int digitSum = 0;
while (num != 0)
num = num/10;
else
Output:
82 | P a g e
30. Program 30
A happy number is defined as a positive number that can replace the sum of squares of digits.
Repeat the process until the number is 1. If the number ends with 1, then it is called happy
number. Execute a program to input a number and check whether it is a happy number.
Sample Input: 31.
Sample Output: 3 + 1 = 10
10 = 1 + 0 = 1.
31 is a happy number.
Code:
import java.util.*;
while(num > 0)
rem = num%10;
num = num/10;
return sum;
83 | P a g e
{
result = isHappyNumber(result);
if(result == 1)
else
Output:
84 | P a g e
31. Program 31
(i) Execute a program to check whether a number is a prime number or not.
(ii) Accept a number to calculate and display 10 simultaneous prime numbers starting from
the number input by the user.
Code:
import java.util.*;
if (n % i == 0)
return false;
return true;
85 | P a g e
int i,m=0,flag=0;
int n = sc.nextInt();
m=n/2;
if(n==0||n==1)
else
for(i=2;i<=m;i++)
if(n%i==0)
flag=1;
break;
if(flag==0)
86 | P a g e
}
sc.close();
if (start < 2)
start = 2;
int numberOfPrimes = 0;
if (isPrime(number))
System.out.println(number);
numberOfPrimes++;
number++;
87 | P a g e
}
Output:
88 | P a g e
32. Program 32
Execute a program to generate the following patterns.
1 for
54321
5432
543
54
5
2 for
*
*#
*#*
*#*#
*#*#*
Code:
import java.util.*;
int n = sc.nextInt();
switch(n)
89 | P a g e
case 1:
int rows=5;
System.out.print(j+" ");
System.out.println();
break;
case 2:
int i, j, row=5;
if(j%2 == 0)
System.out.print("* ");
else
System.out.print("# ");
90 | P a g e
System.out.println();
break;
default:
Output:
91 | P a g e
33. Program 33
Execute a program to generate the following patterns according to user’s choice.
1 for
12345
22345
33345
44445
55555
2 for
13579
35791
57913
79135
91357
Code:
import java.util.*;
int n = sc.nextInt();
int a;
92 | P a g e
switch(n)
case 1:
int rows=5;
if(j==i)
System.out.print(j+" ");
else
System.out.print(j+" ");
System.out.println();
break;
case 2:
int i, j, k; rows = 5;
93 | P a g e
{
System.out.println();
break;
default:
94 | P a g e
Output:
95 | P a g e
34. Program 34
Execute a program using switch case to generate the following patterns.
1 for
54321
4321
321
21
1
2 for
666666
55555
4444
333
22
1
Code:
import java.util.*;
int n = sc.nextInt();
int a, i, k, j;
96 | P a g e
switch(n)
case 1:
System.out.print(" ");
k++;
System.out.print(j);
System.out.println();
break;
case 2:
rows = 6;
for(i=rows;i>=1;i--)
97 | P a g e
for(j=1;j<=i;j++)
System.out.println("");
break;
default:
Output:
98 | P a g e
35. Program 35
Execute a program to generate the following patterns using switch case.
1 for
1
12
123
1234
12345
1234
123
12
1
2 for
11111
222
3
222
11111
Code:
import java.util.*;
int n = sc.nextInt();
99 | P a g e
int a, c, i, z, j;
switch(n)
case 1:
int stopper=1;
stopper++;
else
stopper--;
System.out.println();
break;
case 2:
100 | P a g e
System.out.println("Enter size of the pattern: ");
int print = 0;
if(i<=middle)
range--;
print++;
else
range++;
print--;
System.out.print(print);
else
101 | P a g e
System.out.print(" ");
System.out.println();
break;
default:
Output:
102 | P a g e
36. Program 36
Execute a program to generate the following patterns using switch case.
1 for
1
11
121
1331
14641
2 for
54321
4321
321
21
1
12
123
1234
12345
Code:
import java.util.*;
int i, j, k;
int p=1;
int ch = sc.nextInt();
103 | P a g e
switch(ch)
case 1:
int num=1;
System.out.print(num + "\t");
System.out.println();
break;
case 2:
System.out.print(" ");
104 | P a g e
{
System.out.print(j);
System.out.println("");
p = p + 1;
System.out.print(" ");
System.out.print(k);
System.out.println();
break;
105 | P a g e
Output:
106 | P a g e
37. Program 37
Execute a program to input a number and check whether it is an automorphic number. A
number is said to be automorphic if it is contained in the last digits of the square of this
number.
Sample Input: 25
Sample Output: 625
It is an automorphic number.
Code:
import java.util.*;
int count=0;
while(temp>0)
temp=temp/10;
count++;
107 | P a g e
int lastDigit = (int) (square%(Math.pow(10, count)));
if(num == lastDigit)
else
Output:
108 | P a g e
38. Program 38
The International Standard Book Number (ISBN) is a unique numeric book identifier which is
printed on every book. The ISBN is based upon a 10-digit code.
The ISBN is legal if:
(1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 + 7xdigit7 + 8xdigit8 + 9xdigit9
+ 10xdigit10) is divisible by 11.
Sample Input: 1401601499
Sample Output: Legal ISBN code.
Execute a program to:
(i) Input the ISBN code 10-digit number.
(ii) If the ISBN is not a 10-digit number, then output the statement “Illegal ISBN code” and
terminate the program.
(iii) If the code is a 10-digit number, then extract the digits from the code and compute the
sum as explained above.
(iv) If the sum is divisible by 11, then print the message “Legal ISBN code”. If the sum is not
divisible by 11, then print “Illegal ISBN code”.
Code:
import java.util.*;
while (isbn != 0)
109 | P a g e
int d = (int)(isbn % 10);
count++;
sum = sum + d * m;
m--;
isbn = isbn/10;
if (count != 10)
else if (sum % 11 == 0)
else
110 | P a g e
Output:
111 | P a g e
39. Program 39
Execute a program using switch case to solve the following two patterns:
(i) S=1+(1*2) +2+(1*2*3) +3+…. +(1*2*3*…*10).
(ii) S=a-(a^3)/5+(a^5)/9-(a^7)/13…to n.
Code:
import java.util.*;
int c = sc.nextInt();
switch(c)
case 1:
112 | P a g e
prod = prod*j;
prod = 1;
break;
case 2:
System.out.println("Enter value of variable a and till how many terms you want the sum of the
series to be calculated.");
a = sc.nextInt();
double n = sc.nextInt();
count++;
if(count % 2 == 0)
else
j = j+4;
113 | P a g e
break;
default:
Output:
114 | P a g e
40. Program 40
Execute a program using switch case to solve the sum of pattern
(i) S=1/(a^2) +4/(a^5) +7/(a^8) +10/(a^11) +…+n
(ii) S=1+2/ (1*2) +1+2+3/ (1*2*3) +…. +1+2+3…+n (1*2*3…*n)
Code:
import java.util.*;
int c = sc.nextInt();
switch(c)
case 1:
double a, n, j = 2, sum=0;
System.out.println("Enter value of variable a and till how many terms you want the sum of the
series to be calculated.");
a=sc.nextInt();
n =sc.nextInt();
115 | P a g e
for (int i = 1; i < 3*n; i=i+3)
j = j+3;
break;
case 2:
System.out.println("Enter number of terms till which you want the sum of the series to be
calculated.");
n = sc.nextInt();
sum2 = sum2 + j;
prod = prod*j;
sum2=0; prod=1;
116 | P a g e
break;
default:
Output:
117 | P a g e
41. Program 41
Execute a program using switch case to solve the following pattern:
(i) S=1-(a/2) +3-(a/4) +5-(a/6) +…. +n
(ii) S=a/ (2^2) +(a^2)/ (2^3) +(a^3)/ (2^4) +…+n
Code:
import java.util.*;
int c = sc.nextInt();
switch(c)
case 1:
System.out.println("Enter value of variable a and number of terms till which you want the sum
of the series to be calculated.");
a = sc.nextDouble();
n = sc.nextDouble();
118 | P a g e
for (int i = 1; i<=n; i++)
count++;
if(count%2 == 0)
j = j+2;
else
break;
case 2:
System.out.println("Enter value of variable a and number of terms till which you want the sum
of the series to be calculated.");
a = sc.nextDouble();
n = sc.nextDouble();
119 | P a g e
sum2 = sum2 + Math.pow(a,i)/Math.pow(2,i+1);
break;
default:
Output:
120 | P a g e
42. Program 42
Execute a program using switch case to convert a decimal number to binary number and vice-
versa.
Code:
import java.util.*;
int i=0;
int ch=0;
ch = sc.nextInt();
int n;
switch(ch)
case 1:
n = sc.nextInt();
while(n > 0)
121 | P a g e
{
bin[i++] = n%2;
n = n/2;
System.out.print(bin[j]);
System.out.println();
break;
case 2:
n = sc.nextInt();
long decimal = 0;
int base = 1;
long t = n;
while (t > 0)
decimal += d * base;
base *= 2;
122 | P a g e
t /= 10;
break;
Output:
123 | P a g e
43. Program 43
Execute a program in Java to enter a natural number. Display all the possible combinations
of consecutive natural numbers which adds up to give the sum equal to the original number.
Sample Input: 15
Sample Output: 1 2 3 4 5
456
78
Code:
import java.util.*;
int N = sc.nextInt();
int start = 1;
int end = (N + 1) / 2;
int flag = 0;
System.out.println("The possible combinations of numbers that add up to " + N + " are: ");
int sum = 0;
124 | P a g e
{
sum = sum + i;
if (sum == N)
System.out.println();
flag=1;
break;
if (sum > N)
break;
sum = 0;
start++;
if(flag==0)
125 | P a g e
Output:
126 | P a g e
44. Program 44
Input a number between 2 to 5 (both inclusive). Print all possible combinations from 1 to that
number. Display the number of combinations at the end.
Sample Input: 3
Sample Output:
123
321
132
231
213
312
Number of Combinations=6
Code:
import java.util.*;
return;
127 | P a g e
int[] combination = new int[number];
if (index == combination.length)
printCombination(combination);
return 1;
int count = 0;
if (!used[i])
combination[index] = i + 1;
used[i] = true;
used[i] = false;
128 | P a g e
}
return count;
System.out.println();
Output:
129 | P a g e
45. Program 45
Write a program in Java to round off a number N up to D digits of decimal. N and D are
accepted from the console.
Sample Input:
N=127.67898
D=3
Sample Output:
The number is 127.679
Code:
import java.util.*;
System.out.println("Enter number to be rounded off and places till which it should be rounded
off.");
System.out.println(number);
130 | P a g e
Output:
131 | P a g e
46. Program 46
Write a program to display the given pattern for a word entered by the user.
Sample Input: BLUEJ
Sample Output:
BLUEJ
LUEJB
UEJBL
EJBLU
JBLUE
Code:
import java.util.*;
System.out.println("Enter a word.");
str = str.toUpperCase();
int n,i;
n=str.length();
for(i=0;i<n;i++)
System.out.print(str.substring(i,n));
System.out.print(str.substring(0,i)+"\n");
132 | P a g e
}
Output:
133 | P a g e
47. Program 47
Execute a program in JAVA to accept a word and perform the following tasks:
(i) Count the number of vowels and display them without repeating.
(ii) Change the case of each letter in the string.
(iii) Change all the ‘e’ with ‘*’.
Code:
import java.util.*;
String st;
char ch;
st = sc.nextLine();
st = st.toUpperCase();
l = st.length();
134 | P a g e
ch = st.charAt(i);
count++;
ch = st.charAt(i);
if ((ch=='A') &&(a==0))
a++;
System.out.println(ch);
else if ((ch=='E')&&(b==0))
b++;
System.out.println(ch);
else if ((ch=='I')&&(c==0))
c++;
System.out.println(ch);
135 | P a g e
}
else if ((ch=='O')&&(d==0))
d++;
System.out.println(ch);
else if ((ch=='U')&&(e==0))
e++;
System.out.println(ch);
ch = st.charAt(i);
if (ch == 'E')
ch = '*';
System.out.print(ch);
136 | P a g e
Output:
137 | P a g e
48. Program 48
Execute a program to accept a string, convert it to upper case, and count the number of
double sequences that exist in the string. Display the new string after removing word(s) which
have repeated characters.
Sample Input:
SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE.
Sample Output:
Number of words with repeated letters= 4.
SHE WAS THE WITH AN.
Code:
import java.util.*;
int printcount=0;
String st1="";
System.out.println("Enter a String");
st = sc.nextLine();
st = st.toUpperCase();
st = st + " ";
138 | P a g e
l = st.length();
ch = st.charAt(i);
ch1 = st.charAt(i+1);
if(ch==ch1)
count++;
ch = st.charAt(i);
l1 = st1.length();
if (st1.charAt(j) == st1.charAt(j+1))
printcount++;
if(printcount==0)
139 | P a g e
{
printcount=0;
st1="";
else
System.out.println();
System.out.println("The number of words with repeated letters in the original string is "
+count);
Output:
140 | P a g e
49. Program 49
Execute a program in JAVA to enter at least three words. Convert the first letter of each word
to upper case. Interchange the first and last letter of the string and print the new sentence.
Sample Input:
today is my sons birthday.
Sample Output:
Today Is My Sons Birthday.
yodaT sI yM sonS yirthdaB.
Code:
import java.util.*;
String s = "";
System.out.println("Enter a String");
s = sc.nextLine();
s = s + " ";
141 | P a g e
char c, c1;
int i, j;
int l = 0;
l1 = s.length();
c = s.charAt(i);
if (c == ' ')
l2 = st1.length();
c1 = st1.charAt(0);
c1 = Character.toUpperCase(c1);
st2 = st1.substring(1,l2);
st3 = c1 + st2;
st3 = "";
st1 = "";
else
st1 = st1 + c;
142 | P a g e
System.out.println(st4);
System.out.println("The word after interchanging the first and a last alphabet is: ");
while(str.hasMoreTokens())
str1 = str.nextToken();
l = str1.length();
char t = str1.charAt(0);
str2 = str1.substring(1,(l-1));
char f = str1.charAt(l-1);
str3 = f + str2 + t;
str3 = "";
Output:
143 | P a g e
50. Program 50
Execute a program to accept a sentence. Create a new sentence by replacing each consonant
with the previous letter. If the previous letter is a vowel, then replace it with the next letter
(i.e., the letter before ‘B’ is ‘A’ so ‘B’ must be replaced by ‘C’) and the other characters remain
the same.
Sample Input:
THE CAPITAL OF INDIA IS NEW DELHI.
Output:
SGE BAISAQ OG IMCIA MEV CEKGI.
Code:
import java.util.*;
char c;
char nextchar;
st = sc.nextLine();
st = st.toUpperCase();
l = st.length();
144 | P a g e
{
c = st.charAt(i);
nextchar = (char)(c-1);
if(nextchar == 'A' || nextchar == 'E' || nextchar == 'I' || nextchar == 'O' || nextchar == 'U')
nextchar = (char)(c+1);
st1 = st1 + c;
else
System.out.println(st1);
145 | P a g e
Output:
146 | P a g e
51. Program 51
Execute a program to accept a sentence and perform the following tasks:
(i) Display each word in opposite order.
(ii) Check whether the word is a palindrome or not.
(iii) Display the first and last characters of each letter.
Sample Input:
MADAM HONESTY IS THE BEST POLICY.
Sample Output:
MADAM YTSEOH SI EHT TESB YCILOP.
Palindrome Word: MADAM
First Letters: M, H, I, T, B, P.
Last Letters: M, Y, S, E, T, Y.
Code:
import java.util.*;
System.out.println("Enter a sentence.");
st = sc.nextLine();
147 | P a g e
l = st1.length();
c = st1.charAt(i);
if (c == ' ')
st2 = "";
else
st2 = c + st2;
System.out.println();
c1 = st1.charAt(i);
l2 = st3.length();
148 | P a g e
{
c2 = st3.charAt(j);
st4 = c2 + st4;
if (st4.equals(st3))
System.out.println(st4);
st3 = "";
st4 = "";
else
c1 = st1.charAt(i);
c2 = st3.charAt(0);
st3 = "";
149 | P a g e
else
System.out.println();
c1 = st1.charAt(i);
l2 = st3.length();
c2 = st3.charAt(l2-1);
st3 = "";
else
150 | P a g e
Output:
151 | P a g e
52. Program 52
Execute a program to accept a word and display the patterns using switch case.
(i) A
AB
ABC
ABCD
ABCDE
(ii) A B C D E
BCDE
CDE
DE
E
Code:
import java.util.*;
System.out.println("Enter 1 for the Pattern 1 and Enter 2 for the Pattern 2.");
c = sc.nextInt();
switch (c)
case 1:
152 | P a g e
r = 5;
System.out.print(" ");
System.out.println();
break;
case 2:
r = 5;
153 | P a g e
System.out.print(" ");
System.out.println();
break;
default:
System.out.println("Invalid Choice!");
Output:
154 | P a g e
53. Program 53
Execute a program to accept 10 different numbers in a single dimensional array. Now, accept
a number to be searched for in the array. If the number is found print “search successful”,
else print “search unsuccessful”.
Code:
import java.util.*;
int i, j, n, c=0;
arr[i] = sc.nextInt();
n = sc.nextInt();
if (arr[j] == n)
155 | P a g e
c++;
if (c>0)
System.out.println("Search Successful!");
else
System.out.println("Search Unsuccessful!");
Output:
156 | P a g e
54. Program 54
Execute a program to sort an array using selection sort technique.
Code:
import java.util.*;
size = sc.nextInt();
m[i] = sc.nextInt();
min = i;
157 | P a g e
{
min = j;
t = m[i];
m[i] = m[min];
m[min] = t;
System.out.println(m[i]);
158 | P a g e
Output:
159 | P a g e
55. Program 55
Execute a program to sort an array using bubble sort technique.
Code:
import java.util.*;
int i, j, t, size;
size = sc.nextInt();
m[i] = sc.nextInt();
160 | P a g e
if (m[j] > m[j+1])
t = m[j];
m[j] = m[j+1];
m[j+1] = t;
System.out.println("The numbers arranged in ascending order using Bubble Sort are: ");
System.out.println(m[i]);
161 | P a g e
Output:
162 | P a g e
56. Program 56
Execute a program to accept numbers in two different arrays. Then merge both the arrays
and print the result.
Code:
import java.util.*;
int i, n, m, p, pos;
int j=0;
n = sc.nextInt();
m = sc.nextInt();
p = m + n;
163 | P a g e
{
arr1[i] = sc.nextInt();
arr2[i] = sc.nextInt();
arr3[i] = arr1[i];
arr3[i] = arr2[j];
j++;
System.out.println(arr3[i]);
164 | P a g e
Output:
165 | P a g e
57. Program 57
Execute a program to accept elements of a single dimensional array and insert an element
input by the user into the position input by him.
Code:
import java.util.*;
size = sc.nextInt();
arr[i] = sc.nextInt();
ele = sc.nextInt();
166 | P a g e
pos = sc.nextInt();
arr[i+1] = arr[i];
arr[pos-1] = ele;
System.out.println(arr[i]);
Output:
167 | P a g e
58. Program 58
Execute a program to accept the elements of an array and delete the element in the position
input by the user.
Code:
import java.util.*;
int i, n, pos;
n = sc.nextInt();
arr[i] = sc.nextInt();
pos = sc.nextInt();
168 | P a g e
{
arr[i-1] = arr[i];
n--;
System.out.println(arr[i]);
Output:
169 | P a g e
59. Program 59
Execute a program to accept 15 names in a single dimensional array and display those names
which start with the letter entered by the user. Arrange the given array in alphabetical order.
Code:
import java.util.*;
char ch;
arr[i] = sc.nextLine();
System.out.println("Enter the letter for which the names starting with that letter should be
displayed: ");
ch = sc.next().charAt(0);
170 | P a g e
{
st = arr[i];
if (st.charAt(0)==ch)
System.out.println(arr[i]);
if (arr[j].compareToIgnoreCase(arr[j+1]) > 0)
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
System.out.println("The new array after rearranging words in alphabetical order is: ");
System.out.println(arr[i]);
171 | P a g e
}
Output:
172 | P a g e
60. Program 60
Execute a program to initialize the ‘Seven Wonders of The World’ and their locations in two
different arrays. Search for the name of the country input by user. If found display the country
along with its wonder. Else, display “Sorry not found!”
Seven Wonders: Chichen Itza, Christ The Redeemer, Taj Mahal, Great Wall of China, Machu
Picchu, Petra, Colosseum.
Locations: Mexico, Brazil, India, China, Peru, Jordan, Italy.
Input: Country Name: India
Output: India: Taj Mahal
Input: Country Name: USA
Output: Sorry not found!
Code:
import java.util.*;
String[] wonders = {"Chichen Itza", "Christ The Redeemer", "Taj Mahal", "Great Wall of
China", "Machu Picchu", "Petra", "Colosseum"};
int i;
173 | P a g e
{
if (locations[i].equalsIgnoreCase(country))
index = i;
break;
if (index != -1)
else
Output:
174 | P a g e
61. Program 61
Execute a program to accept an array in matrix form and display the sum of left and right
diagonal elements.
Sample Input: 1 2 3 4
1234
1234
1234
Sum of Left Diagonal= 10
Sum of Right Diagonal= 10
Code:
import java.util.*;
int i, j;
int m = sc.nextInt();
int n = sc.nextInt();
175 | P a g e
{
arr[i][j] = sc.nextInt();
System.out.println();
if(i == j)
if (i+j == n-1)
176 | P a g e
sumrd = sumrd + arr[i][j];
Output:
177 | P a g e
62. Program 62
Execute a program in JAVA to accept a matrix in array form from the user and find the sum
of outer and inner elements. Also, find the difference between the two sums.
Sample Input: 1 2 3 4 5
12345
12345
12345
Sum of Outer Elements: 42
Sum of Inner Elements: 18
Difference: 42-18=24.
Code:
import java.util.*;
int i, j;
int m = sc.nextInt();
int n = sc.nextInt();
178 | P a g e
{
arr[i][j] = sc.nextInt();
System.out.println();
int outerSum = 0;
int innerSum = 0;
if (i == 0 || i == m-1 || j == 0 || j == n-1)
179 | P a g e
{
else
180 | P a g e
Output:
181 | P a g e
63. Program 63
Execute a program to accept number in a double dimensional array and display the array after
removing the corner elements.
Code:
import java.util.*;
int i, j;
int m = sc.nextInt();
int n = sc.nextInt();
arr[i][j] = sc.nextInt();
182 | P a g e
}
System.out.println();
newArray[i-1][j-1] = arr[i][j];
183 | P a g e
for ( j=0; j<newArray[i].length; j++)
System.out.println();
Output:
184 | P a g e
64. Program 64
Execute a program in JAVA to accept the elements of an array and shift the bottom-most row
to the top and shift the other rows, one place downwards each.
Sample Input: 1 2 3 4
2345
3456
4567
Sample Output: 4 5 6 7
1234
2345
3456
Code:
import java.util.*;
int i, j;
int m = sc.nextInt();
int n = sc.nextInt();
185 | P a g e
{
arr[i][j] = sc.nextInt();
System.out.println();
arr[i] = arr[i-1];
arr[0] = temp;
186 | P a g e
for ( i=0; i<m; i++)
System.out.println();
Output:
187 | P a g e
65. Program 65
Execute a program in JAVA to accept the elements of Double-Dimensional Array
and compute the sum of each row and column. Then print the sum of each row along that
row and the sum of each column along the column.
Sample Input: 1 2 3 4
5678
9123
Sample Output: 1 2 3 4 10
5 6 7 8 26
9 1 2 3 15
15 9 12 15
Code:
import java.util.*;
row = row + 1;
col = col + 1;
188 | P a g e
System.out.println("Enter array elements: ");
arr[i][j] = sc.nextInt();
rSum += arr[i][j];
arr[i][col-1] = rSum;
rSum = 0;
System.out.println();
System.out.print(arr[i][j] + "\t");
System.out.println();
189 | P a g e
for (int i=0; i<col-1; i++)
cSum += arr[j][i];
arr[row-1][i] = cSum;
cSum = 0;
System.out.println();
break;
else
System.out.print(arr[i][j] + "\t");
System.out.println();
190 | P a g e
}
Output:
191 | P a g e
66. Program 66
Execute a program in Java to accept a double dimensional array in matrix form from the user.
The matrix should be a square matrix. Print the elements below the right diagonal and the
left diagonal of the matrix.
Sample Input: 1 2 5 6
3478
9 10 11 12
13 14 15 16
Sample Output:
The Elements below the Left Diagonal:
3
9 10
13 14 15
The Elements below the Right Diagonal:
8
11 12
14 15 16
Code:
import java.util.*;
int i, j;
int m = sc.nextInt();
int n = m;
192 | P a g e
int arr[][] = new int[m][n];
arr[i][j] = sc.nextInt();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
193 | P a g e
for ( i=0; i<m; i++)
System.out.println();
System.out.println();
System.out.println();
194 | P a g e
}
Output:
195 | P a g e
67. Program 67
Execute a program in JAVA to accept the elements of a matrix and print its mirror image.
Sample Input: 1 2 3 7 8
45609
Sample Output: 8 7 3 2 1
90654
Code:
import java.util.*;
int i, j;
int m = sc.nextInt();
int n = sc.nextInt();
arr[i][j] = sc.nextInt();
System.out.println();
transpose[i][n-1-j] = arr[i][j];
197 | P a g e
{
System.out.println();
Output:
198 | P a g e
68. Program 68
Execute a program to accept the elements of a matrix and print the matrix after shifting the
elements to the next closest prime number.
Code:
import java.util.*;
int i, j;
matrix[i][j] = sc.nextInt();
199 | P a g e
}
printMatrix(shiftedMatrix);
int i, j;
shiftedMatrix[i][j] = getNextPrime(matrix[i][j]);
return shiftedMatrix;
while (true)
200 | P a g e
{
number++;
if (isPrime(number))
return number;
int i;
if (number <= 1)
return false;
if (number%i == 0)
return false;
201 | P a g e
return true;
int i, j;
System.out.println();
202 | P a g e
Output:
203 | P a g e
69. Program 69
Write a program in JAVA to accept an integer array from the user in matrix form and convert
all the integers to their respective roman numerals.
Sample Input:
1 7 10
6 50 12
33 19 20
Sample Output:
I VII X
VI L XII
XXXIII XIX XX
Code:
import java.util.*;
int i, j;
204 | P a g e
{
matrix[i][j] = sc.nextInt();
System.out.println();
205 | P a g e
String thousands[] = { "", "M", "MM", "MMM"};
String hundreds[] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
String tens[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String ones[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return romanNumeral;
Output:
206 | P a g e
70. Program 70
Write a program in Java to accept an array of square matrix form from the user and check if
it is a magic square or not. A magic square is a square matrix divided into smaller squares
each containing a number, such that the figures in each vertical, horizontal, and diagonal row
add up to the same value (i.e., sum of all the rows, diagonals and columns is the same).
Sample Input:
276
951
438
Sample Output:
It is a magic square.
Code:
import java.util.*;
int i, j;
int m = sc.nextInt();
int n = sc.nextInt();
207 | P a g e
for( i=0; i<m; i++)
arr[i][j] = sc.nextInt();
System.out.println();
sum1 += arr[i][i];
sum2 += arr[i][m-1-i];
208 | P a g e
if(sum1 != sum2)
flag = true;
rowSum += arr[i][j];
colSum += arr[j][i];
flag = true;
if (!flag)
else
209 | P a g e
Output:
210 | P a g e
71. Program 71
Write a program to declare a matrix A [] [] of order m x n where ‘m’ is the number of row and
‘n’ is the number of columns. Allow the user to input integers into this matrix. Perform the
following tasks on the matrix:
(i) Display the input matrix.
(ii) Find the minimum and maximum value in the matrix and display them along with their
position.
(iii) Sort the elements of the matrix in ascending order using any standard sorting technique
and rearrange them in the matrix.
Code:
import java.util.*;
int i, j;
int m = sc.nextInt();
int n = sc.nextInt();
211 | P a g e
for( j=0; j<n; j++)
arr[i][j] = sc.nextInt();
System.out.println();
212 | P a g e
if (arr[i][j] < min)
min = arr[i][j];
minRow = i;
minColumn = j;
max = arr[i][j];
maxRow = i;
maxColumn = j;
int temp;
int q = arr.length;
int r = arr[0].length;
213 | P a g e
for ( j=0; j<q*r-1-i; ++j)
temp = arr[(j+1)/r][(j+1)%r];
arr[(j+1)/r][(j+1)%r] = arr[j/r][j%r];
arr[j/r][j%r] = temp;
System.out.println();
214 | P a g e
Output:
215 | P a g e
72. Program 72
Write a program to store few numbers in single dimensional array and search for a number
using binary search technique. Perform the task using recursive function.
Code:
import java.util.*;
class Prog_72
int mid;
mid = (l+u)/2;
if(l>u || f==1)
return (f);
else
if(v == arr[mid])
f = 1;
u = mid - 1;
l = mid + 1;
216 | P a g e
public static void main(String args[])
int n = sc.nextInt();
arr[i] = sc.nextInt();
int v = sc.nextInt();
int l=0;
int u=n-1;
int f=0;
if(result == 1)
System.out.println("Element found.");
else
217 | P a g e
}
Output:
218 | P a g e
73. Program 73
Smith number is a composite number whose sum of digits of prime factors is equal to sum of
digits of number. Write a program in JAVA to accept a number from the user and check if it is
a smith number or not.
Sample Input: 85
Sample Output: It is a smith number.
Code:
import java.util.Scanner;
int n = sc.nextInt();
if (n <= 0)
return;
if (n%i == 0)
219 | P a g e
{
isComposite = true;
break;
if (isComposite && n != 1)
int sumDigits = 0;
int t = n;
while (t != 0)
int d = t % 10;
sumDigits += d;
t /= 10;
int sumPrimeDigits = 0;
t = n;
while(t%i == 0)
t /= i;
220 | P a g e
int temp = i;
while (temp != 0)
sumPrimeDigits += d;
temp /= 10;
if(t > 2)
while (t != 0)
int d = t % 10;
sumPrimeDigits += d;
t /= 10;
if (sumPrimeDigits == sumDigits)
else
221 | P a g e
}
else
Output:
222 | P a g e
74. Program 74
A prime number is called circular if it remains prime after any cyclic combinations of its digits.
Write a program to input any number from the user and check if it is circular prime.
Code:
import java.util.*;
int c=0;
if (num%i == 0)
c++;
return c == 2;
int c=0;
223 | P a g e
while(num != 0)
c++;
num /= 10;
return c;
int n = sc.nextInt();
if (n <= 0)
System.out.println("Invalid input.");
return;
if (isPrime(n))
System.out.println(n);
224 | P a g e
int divisor = (int)(Math.pow(10, digitCount-1));
int n2 = n;
int t1 = n2 / divisor;
int t2 = n2 % divisor;
n2 = t2 * 10 + t1;
System.out.println(n2);
if (!isPrime(n2))
isCircularPrime = false;
break;
else
isCircularPrime = false;
if (isCircularPrime)
225 | P a g e
}
else
Output:
226 | P a g e
75. Program 75
Write a program in Java to enter an amount of money by the user and write down its
denomination in terms of rupee notes of 1000, 500, 200, 100, 50, 20, 10, 5 and 1.
Sample Input: 674
Sample Output:
500 x1
100 x1
50x1
20x1
1 x4
Total:674
Code:
import java.util.Scanner;
System.out.println("Invalid Amount.");
return;
System.out.println("Denomination: ");
227 | P a g e
int notes[] = {1000, 500, 200, 100, 50, 20, 10, 5 ,1};
int t = amt;
int c = t / notes[i];
if (c != 0)
t = t % notes[i];
Output:
228 | P a g e
76. Program 76
Write a program to declare a square matrix a[][] of order m x m where ‘m’ is the number of
rows and columns, such that m is greater than 2 and less than 10. Accept the value of ‘m’ as
user input. Display an appropriate message of invalid input. Allow the user to enter integer
numbers in the array. Perform the following tasks:
Display original matrix
Rotate the matrix 90 degree clockwise.
Find the sum of corner elements of the matrix.
Sample Input:
349
258
167
Sample output:
123
654
789
Sum of corner elements=20
Code:
import java.util.*;
int i, j;
int s = 0;
int m = sc.nextInt();
229 | P a g e
System.out.println("Enter number of columns");
int n = sc.nextInt();
arr[i][j] = sc.nextInt();
System.out.print(arr[i][j]+" ");
System.out.println();
int x;
230 | P a g e
for( i=0; i<m; i++)
x = m-1;
b[i][j] = arr[x][i];
x--;
System.out.print(b[i][j]+" ");
System.out.println();
231 | P a g e
Output:
232 | P a g e
77. Program 77
Write a program to accept a sentence which may be terminated by either ‘.‘ or ’?’ only. The
words are to be separated by a single blank space. Print an error message if the input does
not terminate with ‘.‘ or ‘?‘. You can assume that no word in the sentence exceeds 15
characters, so that you get a proper formatted output.
Perform the following tasks:
(i) Convert the first letter of each word to uppercase.
(ii) Find the number of vowels and consonants ¡n each word and display them with proper
headings along with the words.
Sample Input: Intelligence plus character is education.
Sample Output: Intelligence Plus Character Is Education
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Code:
import java.io.*;
class Prog_77
233 | P a g e
{
str=br.readLine();
l = str.length();
ch = str.charAt(l-1);
System.out.println("\nOutput:");
System.out.println("Invalid Input!");
else
p = vowels = cons = 0;
str = "";
ch = tmp.charAt(i);
234 | P a g e
word = tmp.substring(p,i);
ch1 = word.charAt(0);
word = Character.toUpperCase(ch1)+word.substring(1);
p = i + 1;
System.out.println("\n"+str);
System.out.print("\nWord");
for(j=15-4; j>=1;j--)
System.out.print(" ");
System.out.println("\tVowels\tConsonants");
p=0;
ch = str.charAt(i);
if("aeiouAEIOU".indexOf(ch) != -1)
vowels++;
235 | P a g e
else
cons++;
else
word = str.substring(p,i);
System.out.print(word);
System.out.print(" ");
p = i + 1;
vowels = cons = 0;
236 | P a g e
Output:
237 | P a g e
78. Program 78
Write a program to accept lower and upper limits of numbers and print those numbers which
do not have repetition of digits and the count of such numbers.
Example:
Lower limit= 290
Upper Limit= 300
Numbers are:
290,291,293,294,295,296,297,298
Total numbers=7
Code:
import java.util.*;
int count = 0;
if (hasUniqueDigits(num))
238 | P a g e
{
count++;
if (visited[digit])
return false;
visited[digit] = true;
num /= 10;
return true;
239 | P a g e
}
Output:
240 | P a g e
79. Program 79
Write a program to use Caeser Cipher rotation and encode the given string. In this technique,
the letter is replaced by the letter 13 places after it in the alphabets. Other characters remain
unchanged.
Example: Hello! How are you?
Output: Urryb! Ubj ner Ibh?
Code:
import java.util.*;
System.out.println("Invalid Length.");
return;
{
241 | P a g e
char ch = str.charAt(i);
if ((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))
sb.append((char)(ch + 13));
else if ((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))
sb.append((char)(ch - 13));
else
sb.append(ch);
System.out.println(cipher);
242 | P a g e
Output:
243 | P a g e
80. Program 80
Write a program to print the consecutive and repeating characters in a string.
Sample Input: Understanding Computer Science
Sample Output:
Consecutive Characters:
d
e
r
s
s
t
Repeating Characters:
c
d
e
i
n
r
s
t
u
Code:
import java.util.*;
String s;
244 | P a g e
s = sc.nextLine();
s = s.toLowerCase();
int j;
int i;
if ((int)s.charAt(i+1) -(int)s.charAt(i)==1)
System.out.println(s.charAt(i));
System.out.println(s.charAt(i+1));
int repetitions=0;
char ch = s.charAt(j);
if(ch == k)
245 | P a g e
{
repetitions++;
if(repetitions>1)
System.out.println(ch);
break;
Output:
246 | P a g e
81. Program 81
Print the following pattern:
Code:
import java.util.*;
size = sc.nextInt();
mid = size/2;
range = -1;
247 | P a g e
if(r<=mid)
range++;
else
range--;
if(r%2 == 0)
System.out.print("*\t");
else
System.out.print(" \t");
else
System.out.print("*\t");
else
248 | P a g e
System.out.print(" \t");
System.out.println();
Output:
249 | P a g e
82. Program 82
The names of the teams participating in a competition must be printed on a banner vertically,
to accommodate as many teams as possible in a single banner. Design a program to accept
the names of N teams, where 2<N<9 and display them in vertical order side by side using
horizontal tab (8 spaces).
Example:
Input:
Teams= 3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote
Output:
Code:
import java.util.*;
250 | P a g e
System.out.print("Enter the number of teams: ");
int n = sc.nextInt();
sc.nextLine();
if (n <= 2 || n >= 9)
System.out.println("Invalid Input!");
return;
int highLen = 0;
teams[i] = sc.nextLine();
highLen = teams[i].length();
System.out.println();
251 | P a g e
for (int j=0; j<n; j++)
if (i >= len)
System.out.print("\t");
else
System.out.print(teams[j].charAt(i) + "\t");
System.out.println();
252 | P a g e
Output:
253 | P a g e
83. Program 83
Write a program to accept two words and check whether they are anagram words or not.
Anagram words are those which have same length and rearrangement of letters of one word
gives the other word.
Example:
Input: Creative, Reactive
Output: They are anagram words.
Code:
import java.util.*;
str1 = sc.nextLine();
str1 = str1.toUpperCase();
str2 = sc.nextLine();
str2 = str2.toUpperCase();
p1 = str1.length();
254 | P a g e
p2 = str2.length();
if(p1 == p2)
chr1 = str1.charAt(j);
chr2 = str2.charAt(j);
as1 = (int)chr1;
as2 = (int)chr2;
if(as1 == i)
s1 = s1 + as1;
if(as2 == i)
s2 = s2 + as2;
255 | P a g e
}
else
System.out.println( str1 +" and " + str2 +" are not anagrams.");
else
Output:
256 | P a g e
84. Program 84
Write a program to declare a matrix A[][] of order (m*n) where ‘m’ is the number of rows and
‘n’ is the number of columns. Allow the user to input integers into this matrix. Perform the
following tasks on the matrix:
(i) Display the input matrix
(ii) Find the maximum and minimum value in the matrix and display them along with their
position
(iii) Sort the elements of the matrix in ascending order using any standard sorting technique
and rearrange them in the matrix.
(iv) Display the rearranged matrix.
Code:
import java.util.*;
int i,j;
int m = sc.nextInt();
int n = sc.nextInt();
257 | P a g e
{
arr[i][j] = sc.nextInt();
System.out.print(arr[i][j]+" ");
System.out.println();
258 | P a g e
{
min = arr[i][j];
minRow = i;
minColumn = j;
max = arr[i][j];
maxRow = i;
maxColumn = j;
int temp, k;
259 | P a g e
{
temp = arr[i][k];
arr[i][k] = arr[i][k+1];
arr[i][k+1] = temp;
System.out.println();
260 | P a g e
}
Output:
261 | P a g e
85. Program 85
Write a program to find out if a number entered by the user is a bouncy number. A bouncy
number is a positive number whose digits neither increase nor decrease. For example, 12345
is an increasing number and 54321 is a decreasing number. By definition, all numbers under
100 are non-bouncy, and 101 is the first bouncy number.
Sample Input: 25143
Sample Output: It is a bouncy number.
Code:
import java.util.*;
int n = sc.nextInt();
if (n < 100)
return;
int t = n;
262 | P a g e
while (t != 0)
int d = t % 10;
if (d > prev)
isIncreasing = false;
break;
prev = d;
t /= 10;
t = n;
prev = t % 10;
while (t != 0)
int d = t % 10;
if (d < prev)
isDecreasing = false;
break;
prev = d;
263 | P a g e
t /= 10;
else
Output:
264 | P a g e
86. Program 86
A new advanced Operating System, incorporating the latest hi-tech features has been
designed by Opera Computer Systems. The task of generating copy protection code to
prevent software piracy has been entrusted to the Security Department. The Security
Department has decided to have codes containing a jumbled combination of alternate
uppercase letters of the alphabet from ‘A’ to ‘K’ (namely among A, C, E, G, I, K). The code may
or may not be in the consecutive series of alphabets. Each code should not exceed 6
characters. Else, an appropriate error message must be displayed.
Write a program to input a code and its length. At the first instance of an error, display
“Invalid” stating the appropriate reason. In case of no error, display the message “Valid”.
Code:
import java.util.*;
return;
System.out.println("Enter Code:");
265 | P a g e
String code = sc.next();
char ch = code.charAt(i);
if (Character.isLowerCase(ch))
return;
return;
if (ch % 2 == 0)
return;
int count = 0;
266 | P a g e
if (ch == code.charAt(j))
count++;
if (count > 1)
return;
267 | P a g e
Output:
268 | P a g e