Isc Grade 12 Computer Project
Isc Grade 12 Computer Project
CERTIFICATE
else { if (a == b & b == c)
else if (a == b || b == c || c == a)
}
OUTPUT
PROGRAM 2
import java.util.*;
public class Prog2
{
public static void main (String args[])
OUTPUT
PROGRAM 4
import java.util.Scanner;
name = sc.nextLine();
num = sc.nextInt();
units = sc.nextInt();
)
Program 5
import java.util.*;
int age;
String gender;
double inc,
tax = 0.0;
gender = sc.nextLine();
age = sc.nextInt();
inc = sc.nextDouble();
if (inc> 800000)
}
Program 6
import java.util.*;
int weight,
o_charge = 0,
e_charge = 0;
char chr;
System.out.println ("Enter weight of parcel and \'O\' for Ordinary mail or \'E\'
for Express mail");
weight = sc.nextInt();
chr = sc.next().charAt(0);
o_charge = 50;
if (weight >= 101 && weight <= 500)
}
else
System.out.println ("Invalid choice");
}
Program 7
import java.util.*;
n = sc.nextInt();
if (n <= 5)
f = n * 0.40;
}
Program 8
import java.util.*;
char chr;
System.out.println ("To find area, enter: c for circle, s for square, and r for
rectangle");
chr = sc.next().charAt(0);
switch (chr)
{
case 'c':
System.out.println ("Enter the radius of the circle");
double r = sc.nextDouble();
carea = 3.14 * r * r;
System.out.println ("The area of circle is: " + carea);
break;
case 's':
System.out.println ("Enter the side of the square");
double s = sc.nextDouble();
sarea = s * s;
System.out.println ("The area of square is: " + sarea);
break;
case 'r':
double l = sc.nextDouble();
double b = sc.nextDouble();
rarea = l * b;
System.out.println ("The area of rectangle is: " + rarea);
break;
default:
System.out.println ("Invalid Choice");
}
Program 9
import java.util.*;
n = sc.nextInt();
switch(n)
{
case 1:
System.out.println ("Enter the side of the cube: ");
double s = sc.nextDouble();
cube = s * s * s;
System.out.println ("The volume of the cube is: " + cube);
break;
case 2:
break;
case 3:
System.out.println ("Enter the length, breadth and height of the cuboid: ");
double l = sc.nextDouble();
double b = sc.nextDouble();
double h = sc.nextDouble();
cuboid = l * b * h;
System.out.println ("The volume of the cuboid is: " + cuboid);
break;
default:
System.out.println ("Invalid Choice");
}
Program 10
import java.util.*;
price = sc.nextDouble();
year = sc.nextInt();
if (year == 1)
dprice = price - (0.1 * price);
if (year == 2)
dprice = price - (0.2 * price);
if (year == 3)
dprice = price - (0.3 * price);
if (year == 4)
dprice = price - (0.5 * price);
if (year > 4)
dprice = price - (0.6 * price);
PROGRAM 11
import java.util.*;
double v1 = sc.nextDouble();
double v2 = sc.nextDouble();
n = sc.nextInt();
switch (n)
{
case 1:
if (v1 >= v2)
vsame = v1 - v2;
else
vsame = v2 - v1;
break;
case 2:
vopp = v1 + v2;
System.out.println ("The relative velocity is: " + vopp);
break;
default:
System.out.println ("Invalid Choice");
}
PROGRAM 12
import java.util.*;
public class Prog12
name = sc.nextLine();
address = sc.nextLine();
price = sc.nextInt();
chr = sc.next().charAt(0);
switch (chr)
{
case 'L':
if (price<=25000)
dprice = price;
if (price>= 25001 && price <= 57000)
if (price >100000)
break;
case 'D':
if (price >100000)
break;
default:
PROGRAM 13
import java.util.*;
int n;
n = sc.nextInt();
switch (n)
{
case 1:
System.out.println ("Enter the side of the triangle: ");
int s = sc.nextInt();
area = (Math.sqrt(3.0) * s * s) / 4.0;
System.out.println ("The area is: " + area);
break;
case 2:
System.out.println ("Enter the two of the sides of the triangle: ");
int a = sc.nextInt();
int b = sc.nextInt();
area = (1.0/4.0) * b * Math.sqrt((4*b*b) - (a*a));
break;
case 3:
System.out.println ("Enter the three sides of the triangle: ");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
double sp = (x+y+z) / 2.0;
area = Math.sqrt(sp * (sp-x) * (sp-y) * (sp-z));
default:
System.out.println ("Wrong choice");
}
Program 14
import java.util.*;
public class Prog14
e_num = sc.nextInt();
age = sc.nextInt();
basic = sc.nextInt();
gen = sc.next().charAt(0);
d = (20.0/100.0) * basic;
hr = (40.0/100.0) * basic;
p = (12.0/100.0) * basic;
ef = (2.0/100.0) * basic;
g = basic + d + hr;
net = g - p - ef;
ge = Character.toUpperCase(ge);
else
System.out.println("Income tax payer");
if (gen == 'F')
else
}
Program 15
import java.util.*;
int days;
name = sc.nextLine();
n = sc.next();
System.out.println (“ Number of days stayed: ");
days = sc.nextInt();
switch (n)
{
case "S":
if (days <= 3)
amt = 2500 - ((10.0/100.0) *2500);
break;
case "D":
if (days <= 3)
amt = 3500 - ((10.0/100.0) *3500);
break;
case "SD":
if (days <= 3)
break;
default:
if (amt != 0.0)
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’.
PROGRAM:
ch = sc.nextInt();
System.out.println ("Enter two numbers: "); a = sc.nextInt();
b = sc.nextInt();
switch (ch)
{
case 1:
ans = a+b;
System.out.println ("The sum is: " + ans); break;
case 2:
ans = a-b;
43
default:
}}
OUTPUT:
44
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.
PROGRAM:
{
num = sc.nextInt();
if ((num % 2 == 0) && (num > 0)) even = even + num;
if ((num % 2 != 0) && (num < 0)) odd = odd + num;
if (num == 0)
break;
}
System.out.println ("Sum of positive even numbers: " + even);
System.out.println ("Sum of negative odd numbers: " + odd);
}}
45
OUTPUT:
46
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.
PROGRAM:
import java.util.*;
public class Program18
{public static void main (String args[])
{
Scanner sc = new Scanner (System.in); int i = 1, n, f = 1;
System.out.println ("Enter a number: "); n = sc.nextInt();
do
{
f = f * i;
i++; }
OUTPUT:
47
PROGRAM 19
Execute a program to check whether a number input by the user is
even or odd.
PROGRAM:
if (num % 2 == 0)
System.out.println ("The number is even"); else
System.out.println ("The number is odd");
}}
OUTPUT:
48
PROGRAM 20
Execute a program to accept a number and find the odd numbers
below it.
PROGRAM:
if (i % 2 != 0)
}}
OUTPUT:
49
PROGRAM 21
Execute a program to print the given pattern. 1
12
12 3
123 4 1234 5
PROGRAM:
int i, j;
for (i = 1; i <= 5; i++) {
OUTPUT:
50
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.
PROGRAM:
if (num%i == 0)
f = f + i; }
if (f == num)
System.out.println ("It is a perfect number"); else
System.out.println ("It is not a perfect number");
51
}}
OUTPUT:
52
PROGRAM 23
Execute a program to accept two numbers and find the greatest
common divisor (GCD) of the two numbers.
PROGRAM:
gcd = i; }
} else {
53
gcd = i; }
System.out.println ("the GCD of " +num1+ " and " +num2+ " is " +gcd); }
OUTPUT:
54
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.
PROGRAM:
sq = n * n; while (sq != 0) {
}
if (sum == n)
System.out.println ("It is a neon number");
55
else
OUTPUT:
56
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.
PROGRAM:
num = n;
while (num > 0) {
}
if (sum == n)
57
}}
OUTPUT:
58
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.
PROGRAM:
}
if (s == num)
System.out.println ("It is a palindrome number"); else
59
OUTPUT:
60
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.
PROGRAM:
max = num;
min = num;
for (i = 2; i <= 10; i++) {
}
System.out.println ("Greatest number: " + max); System.out.println
("Smallest number: " + min);
}
61
OUTPUT:
62
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.
PROGRAM:
if (n < 1)
System.out.println ("Invalid"); else if (n == 1) System.out.println (a);
else if (n == 2) System.out.println (a + " " + b); else
{
c = a + b;
63
}}
}}
OUTPUT:
64
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.
PROGRAM:
import java.util.*; public class Program29 {
num = n;
while (num > 0) {
}
if (n % sum == 0)
System.out.println ("It is a niven number"); else
65
OUTPUT:
66
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: 32 + 12=10 10=12+ 02= 1.
31 is a happy number.
PROGRAM:
s = n; do
{
n = s; s = 0; do
{
r = n % 10;
67
s = s + r*r;
n = n/10; }
}}
OUTPUT:
68
PROGRAM 31
1. (i) Execute a program to check whether a number is a prime
number or not.
2. (ii) Accept a number to calculate and display 10 simultaneous
prime numbers starting
PROGRAM:
if (n1%i == 0)
f = f + 1; }
if (f == 0)
System.out.println ("It is a prime number"); else
System.out.println ("It is not a prime number"); f = 0;
System.out.println ("Enter a number");
n2 = sc.nextInt();
69
if (i%j == 0)
f = f+1; }
}}
OUTPUT:
70
PROGRAM 32
Execute a program to generate the following patterns. 1 for
5432 1
543 2
54 3 54
5
2 for *
*#
*# * *#* # *#*# *
PROGRAM:
switch (ch) {
71
case 1:
for (i = 1; i <= 5; i++) {
System.out.println(); }
break;
case 2:
for (i = 1; i <= 5; i++) {
System.out.println (); }
break;
default:
System.out.println ("Invalid choice");
}}
72
OUTPUT:
73
PROGRAM 33
Execute a program to generate the following patterns according to
user’s choice. 1 for
1234 5
2234 5
PROGRAM:
switch (ch) {
case 1:
74
System.out.println (); }
break;
case 2:
for (i = 1; i <= 9; i = i + 2) {
}
break;
default:
System.out.println ("Invalid choice");
}}
75
OUTPUT:
76
PROGRAM 34
Execute a program using switch case to generate the following
patterns. 1 for
5432 1
432 1 32 1 21
1 2 for
22 1
PROGRAM:
switch (ch)
77
{
case 1:
if (j > i)
System.out.print (" "); else
System.out.print (j + " ");
System.out.println(); }
break;
case 2:
for (i = 6; i >= 1; i--) {
System.out.println(); }
break;
default:
System.out.println ("Invalid Choice");
}}
78
OUTPUT:
79
PROGRAM 35
Execute a program to generate the following patterns using switch
case. 1 for
1
12
1
2 for 1111 1
22 2 3
22 2
1 1 1 11
PROGRAM:
80
System.out.println ("Enter 1 or 2 to display a pattern: "); ch = sc.nextInt();
switch (ch)
{
case 1:
for (i = 1; i <= 5; i++) {
}
for (i = 4; i >= 1; i--) {
}
break;
case 2:
int k = 1;
for (i = 1; i <= 3; i++) {
81
}
k = 3;
for (i = 2; i >= 1; i--) {
}}
}}
OUTPUT:
82
PROGRAM 36
Execute a program to generate the following patterns using switch
case.
PROGRAM:
switch (ch) {
case 1:
for (i = 5; i > 1; i--) {
{
if (j > i)
System.out.println(); }
k = 1;
for (i = 5; i >= 1; i--) {
k++; }
k = 1;
System.out.println(); }
break;
case 2:
int n = 1;
for (i = 0; i < 5; i++) {
84
if (j == 0 || i == 0)
n = 1;
else
n = n * (i-j+1)/j; System.out.print (n + " ");
System.out.println(); }
break;
default:
System.out.println ("Invalid choice");
}}
OUTPUT:
85
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.
PROGRAM:
c++;
n = n / 10; }
86
}}
OUTPUT:
87
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:
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”.
PROGRAM:
n = isbn; while (n != 0) {
88
d++;
n = n / 10; }
if (d != 10)
System.out.println ("Illegal ISBN code"); else
{
d = 0;
n = isbn; while (n != 0) {
d = n % 10;
s = s + (d * c); c--;
n = n / 10;
}
if (s % 11 == 0)
System.out.println ("Legal ISBN code"); else
System.out.println ("Illegal ISBN code");
}}}
OUTPUT:
89
PROGRAM 39
Execute a program using switch case to solve the following two
patterns:
1. (i) S=1+(1*2)+2+(1*2*3)+3+....+(1*2*3*...*10).
2. (ii) S=a-(a^3)/5+(a^5)/9-(a^7)/13...to n.
PROGRAM:
switch (ch) {
case 1:
int f = 1;
System.out.println ("The series: 1 + (1*2) + 2 + (1*2*3) + ... +
(1*2*3*...*10)"); for (i = 1; i <= 9; i++)
{
f = f * (i+1);
s = s + i + f; }
90
case 2:
System.out.println ("The series: a - (a^3)/5 + (a^5)/9 - (a^7)/13... to n");
System.out.println ("Enter the value of a and n: ");
int a = sc.nextInt();
int n = sc.nextInt();
int p = 1, d = 1;
double sum = 0.0;
for (i = 1; i < n; i++)
{
if (i % 2 != 0)
sum = sum + Math.pow(a,p)/d; else
sum = sum - Math.pow(a,p)/d; p = p + 2;
d = d + 4;
}
System.out.println ("The sum: " + sum); break;
default:
System.out.println ("Wrong choice");
}}}
OUTPUT:
91
PROGRAM 40
Execute a program using switch case to solve the sum of pattern:
1. (i) S=1/(a^2)+4/(a^5)+7/(a^8)+10/(a^11)+...+n
2. (ii) S=1+2/(1*2)+1+2+3/(1*2*3)+....+1+2+3...+n(1*2*3...*n)
PROGRAM:
switch (ch) {
case 1:
System.out.println ("s = 1/(a^2) + 4/(a^5) + 7/(a^8) + ... + n ");
System.out.println ("Enter the value of a and number of terms: "); int a =
sc.nextInt();
n = sc.nextInt();
j = 1;
int k = 2;
for (i = 1; i <= n; i++)
{
92
s = s + j/Math.pow(a,k); j = j + 3;
k = k + 3;
break;
case 2:
j = j + i;
for (k = 1; k <= i+1; k++)
f = f * k;
s = (double)j + (double)(i+1)/(double)f;
}
System.out.println ("The sum: " + s); break;
default:
System.out.println ("Invalid choice");
}}
93
OUTPUT:
94
PROGRAM 41
Execute a program using switch case to solve the following
patterns:
1. (i) S=1-(a/2)+3-(a/4)+5-(a/6)+....+n
2. (ii) S=a/(2^2)+(a^2)/(2^3)+(a^3)/(2^4)+...+n
PROGRAM:
switch (ch) {
case 1:
System.out.println ("s = 1 - (a/2) + 3 - (a/4) + 5 - (a/6) + ... + n");
System.out.println ("Enter the number of terms and the value of a: "); n =
sc.nextInt();
a = sc.nextInt();
for (i = 1; i <= n; i++)
{
if (i % 2 != 0) s = s + i;
95
else
s = s - (double)a/(double)i; }
a = sc.nextInt();
for (i = 1; i <= n; i++) {
s = s + (Math.pow(a,i))/Math.pow(2,i+1); }
}}}
OUTPUT:
96
PROGRAM 42
Execute a program using switch case to convert a decimal number
to binary number and vice-versa.
PROGRAM:
case 1:
System.out.println ("Enter the decimal number: "); dcml = sc.nextInt();
n = dcml;
int s = 0;
while (n != 0)
{
d = n % 2;
s = s*10 + d; n = n/2;
97
}
while (s != 0) {
d = s % 10;
bnry = bnry * 10 + d; s = s/10;
}
System.out.println ("The binary equivalent: "+ bnry); break;
case 2:
System.out.println ("Enter the binary number: "); bnry = sc.nextInt();
n = bnry;
int i = 0;
while (n != 0)
{
d = n % 10;
dcml = dcml + d * (int)Math.pow(2,i); i++;
n = n / 10;
}
System.out.println ("The decimal equivalent: " + dcml); break;
default:
System.out.println ("Invalid choice");
}}
98
OUTPUT:
99
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.
45 6 78
s = 0;
for (k = i; k <= j; k++) s = s + k;
if (s == n)
{
PROGRAM:
100
System.out.println();
}}
}}
OUTPUT:
101
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.
13 2
23 1
21 3
31 2
Number of Combinations=6
PROGRAM:
102
if (num == 2) {
if (i != j) {
c++; }
} else {
if (num == 3) {
if (i != j && j != k && i != k) {
System.out.println (i + " " + j + " " + k);
c++; }
} else {
if (num == 4) {
103
c++; }
} else {
{
System.out.println (i + " " + j + " " + k + " " + l + " " + m); c++;
}}
}}
}}
}}
104
OUTPUT:
105
PROGRAM 45
Write a program in Java to round off a number N upto D digits of
decimal. N and D are accepted from the console.
PROGRAM:
106
else
newd = (deci / 10) + 1;
newnum = (int)num + ((double)newd/Math.pow(10.0,dplace));
System.out.println ("The rounded off number: " + newnum);
}}
OUTPUT:
107
PROGRAM 46
Write a program to display the given pattern for a word entered by
the user. Sample Input: BLUEJ
Sample Output:
B L UE J
L U E JB U E JB L E JB L U JB L U E
PROGRAM:
str = str.toUpperCase();
for (i = 0; i < str.length(); i++) {
System.out.print(str.substring(i) + str.substring(0,i));
System.out.println(); }
108
}}
OUTPUT:
109
PROGRAM 47
Execute a program in JAVA to accept three words and perform the
following tasks:
PROGRAM:
110
c[0]++;
if (ch == v[1]) c[1]++;
if (ch == v[2]) c[2]++;
if (ch == v[3]) c[3]++;
if (ch == v[4]) c[4]++;
}
System.out.println ("The vowels and their frequency are: "); for (i = 0; i < 5;
i++)
{
if (c[i] == 0)
continue;
else
System.out.println (v[i] + " " + c[i]);
}
for (i = 0; i < str.length(); i++) {
ch = str.charAt(i);
if (Character.isLowerCase(ch)) ch = Character.toUpperCase(ch); if
(Character.isUpperCase(ch)) ch = Character.toLowerCase(ch); str1 = str1 +
ch;
}
for (i = 0; i < str.length(); i++)
111
{
ch = str.charAt(i);
if (ch == 'e' || ch == 'E') ch = '*';
str2 = str2 + ch;
System.out.println (str2); }
OUTPUT:
112
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.
PROGRAM:
l = s1.length();
for (i = 0; i < l; i++) {
113
ch2 = word.charAt(j);
if (ch2 == word.charAt(j+1)) r1++;
}
if (r1 == 0)
s2 = s2 + word + " "; if (r1 > 0)
{
r2 = r2 + r1;
r1 = 0; }
word = ""; }
else
System.out.println (s2); }}
OUTPUT:
114
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.
PROGRAM:
115
}
if (wcount < 3)
System.out.println ("Less than three words"); else
{
for (i = 0; i < str.length(); i++) {
ch1 = str.charAt(i+1);
ch1 = Character.toUpperCase(ch1); str1 = str1 + " " + ch1;
i++;
}
else
str1 = str1 + ch1;
}
System.out.println (str1.trim()); str1 = str1.trim() + " ";
for (i = 0; i < str1.length(); i++) {
ch2 = word.charAt(0);
char ch3 = word.charAt(word.length()-1); for (j = 1; j < word.length()-1; j++)
116
{
word2 = word2 + word.charAt(j);
}
if (word.length() > 1)
str2 = str2 + " " + ch3 + word2 + ch2; else
str2 = str2 + " " + ch3;
word = "";
word2 = "";
}
else
word = word + ch1;
System.out.println (str2.trim()); }
}}
OUTPUT:
117
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. Sample Output:
SGE BAISAQ OG IMCIA MEV CEKGI.
PROGRAM:
ch = str.charAt(i);
118
if (ch == ' ')
str2 = str2 + ch;
else if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') str2 = str2
+ ch;
else
{
}}
System.out.println (str2.trim()); }
OUTPUT:
119
PROGRAM 51
Execute a program to accept a sentence and perform the following
tasks:
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.
PROGRAM:
import java.util.*;
public class Program51
{public static void main (String args[])
String s1, word1 = "", word2 = "", s2 = "", palin = "", firstl = "", lastl = "";
char ch1, ch2;
System.out.println ("Enter a string");
s1 = sc.nextLine();
ch1 = s1.charAt(i);
120
ch2 = word1.charAt(j);
if (word1.equals(word2))
palin = palin + word2 + " ";
firstl = firstl + word1.charAt(0) + ", ";
lastl = lastl + word1.charAt(word1.length()-1) + ", "; s2 = s2 + word2 + " ";
word1 = ""; word2 = "";
}
else
word1 = word1 + ch1;
}
System.out.println (s2);
System.out.println ("Palindrome words: " + palin); System.out.println ("First
Letters: " + firstl); System.out.println ("Last letters: " + lastl);
}}
OUTPUT:
121
PROGRAM 52
Execute a program to accept a word and display the patterns using
switch case.
(i)
(ii)
A AB AB C AB CD
A B C DE
A B C D E B C DE C DE
DE E
switch (ch) {
case 1:
k = 65;
for (i = 1; i <= 5; i++) {
122
{
if (j <= i)
{
System.out.print ((char)k + " "); k++;
}
else
System.out.print (" ");
}
System.out.println (); k = 65;
}
break;
case 2:
for (i = 1; i <= 5; i++) {
if (j >= i)
System.out.print ((char)(j+64) + " "); else
System.out.print (" ");
System.out.println(); }
break;
default:
System.out.println ("Wrong choice");
123
}}
OUTPUT:
124
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”.
PROGRAM:
arr[i] = sc.nextInt();
System.out.println ("Enter the number to be searched: "); num =
sc.nextInt();
for (i = 0; i < 10; i++)
{
if (arr[i] == num) {
k = 1;
break; }
}
125
if (k == 1)
System.out.println ("Search successful"); else
System.out.println ("Search unsuccessful");
}}
OUTPUT:
126
PROGRAM 54
Execute a program to sort an array using selection sort technique.
PROGRAM:
min = i;
for (j = i+1; j < n; j++) {
min = j; }
t = arr[i];
arr[i] = arr[min];
127
arr[min] = t; }
}}
OUTPUT:
128
PROGRAM 55
Execute a program to sort an array using bubble sort technique.
PROGRAM:
import java.util.*; public class Program55 {
t = arr[j];
arr[j] = arr[j+1]; arr[j+1] = t;
129
}}
}}
OUTPUT:
130
PROGRAM 56
Execute a program to accept numbers in two different arrays. Then
merge both the arrays and print the result.
PROGRAM:
131
p[i+ml] = n[i];
System.out.println ("The merged array is: "); for (i = 0; i < p.length; i++)
System.out.println (p[i]);
}}
OUTPUT:
132
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.
PROGRAM:
133
}}
OUTPUT:
134
PROGRAM 58
Execute a program to accept the elements of an array and delete
the element in the position input by the user.
PROGRAM:
}}
135
OUTPUT:
136
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.
PROGRAM:
{
for (j = 0; j < 14-i; j++) {
if (name[j].compareTo(name[j+1]) > 0) {
t = name[j];
name[j] = name[j+1]; name[j+1] = t;
137
}}
System.out.println ("The array in alphabetical order is: "); for (i = 0; i < 15;
i++)
{
}}
OUTPUT:
138
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!”
PROGRAM:
int i, pos = 0, k = 0;
String country;
{
if (country.equalsIgnoreCase(countries[i])) {
pos = i; k = 1; break;
139
}}
if (k == 1)
System.out.println ("Country: " + countries[pos] + " Wonder: " + wonders[i]);
else
System.out.println ("Sorry not found!");
}}
OUTPUT:
140
PROGRAM 61
Execute a program to accept an array in matrix form and display the
sum of left and right diagonal elements.
PROGRAM:
arr[i][j] = sc.nextInt(); }
141
}
for (i = 0; i < n; i++) lsum = lsum + arr[i][i]; j = 0;
for (i = n-1; i >= 0; i--) {
j++; }
OUTPUT:
142
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.
143
arr[i][j] = sc.nextInt(); }
}
for (i = 0; i < m; i++) {
}}
else
}
144
OUTPUT:
145
PROGRAM 63
Execute a program to accept number in a double dimensional array
and display the array after removing the corner elements.
PROGRAM:
arr[i][j] = sc.nextInt(); }
146
}
System.out.println ("Array after removing the corner elements: "); for (i = 0; i
< m; i++)
{
System.out.println(); }}}
OUTPUT:
147
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.
PROGRAM:
148
arr[i][j] = sc.nextInt(); }
}
int t[] = new int[n];
for (i = 0; i < n; i++) t[i] = arr[m-1][i];
for (i = m-1; i >= 1; i--) {
}}
149
OUTPUT:
150
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 567 8 912 3
Sample Output: 1 2 3 4 10 5 6 7 8 26
9 1 2 3 15 15 9 12 15
PROGRAM:
151
arr[i][j] = sc.nextInt(); }
}
System.out.println ("The new array is: "); for (i = 0; i < m; i++)
{
}
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {
152
csum = 0; }
}}
OUTPUT:
153
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.
9 10 11 12
13 14 15 16 Sample Output:
The Elements below the Left Diagonal: 3
9 10
13 14 15
PROGRAM:
154
n = sc.nextInt();
int arr[][] = new int [n][n]; System.out.println ("Enter the elements: "); for (i
= 0; i < n; i++)
{
arr[i][j] = sc.nextInt(); }
}
System.out.println ("The elements below the left diagonal are: "); for (i = 0; i
< n; i++)
{
if (i > j)
System.out.println(); }
System.out.println ("The elements below the right diagonal are: "); for (i = 0;
i < n; i++)
{
155
{
if ( i+j > n-1)
System.out.print (arr[i][j] + " ");
System.out.println(); }
}}
OUTPUT:
156
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
4560 9
Sample Output: 8 7 3 2 1 9065 4
PROGRAM:
arr[i][j] = sc.nextInt(); }
157
{
for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();
}
System.out.println ("The mirror image is: "); for (i = 0; i < m; i++)
{
}}
OUTPUT:
158
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.
PROGRAM:
int c = 0, a, b;
for (a = p+1;; a++) {
if (a % b == 0)
c++; }
if (c == 0) break;
c = 0;
return a; }
public static void main (String args[]) {
159
arr[i][j] = sc.nextInt(); }
}
System.out.println ("The new array is: "); for (i = 0; i < m; i++)
{
f = ob.prime(arr[i][j]);
System.out.println(); }
160
}}
OUTPUT:
161
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
PROGRAM:
String r = "";
int a;
String rnum[] = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C"}; int rval[] = {1,
4, 5, 9, 10, 40, 50, 90, 100};
for (a = rnum.length - 1; a >= 0; a--)
{
162
r = r + rnum[a]; }
return r; }
n = sc.nextInt();
int arr[][] = new int[m][n]; System.out.println ("Enter the elements: "); for (i
= 0; i < m; i++)
{
arr[i][j] = sc.nextInt(); }
}
for (i = 0; i < m; i++) {
163
System.out.println(); }
}}
OUTPUT:
164
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: 27 6
95 1
43 8
Sample Output:
It is a magic square
PROGRAM:
165
rsum[i] = 0;
csum[i] = 0; }
arr[i][j] = sc.nextInt(); }
}
for (i = 0; i < n; i++) // finding the sum of each row {
for (i = 0; i < n; i++) // finding the sum of left diagonal ldsum = ldsum +
arr[i][i];
j = n-1;
166
j--; }
for (i = 0; i < n-1; i++) {
m1 = false;
break; }
}
if (m1 == true) {
m2 = false;
break; }
}
if (m2 == true)
167
}
else
System.out.println ("It is not a magic square");
}}
OUTPUT:
168
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:
2. Find the minimum and maximum value in the matrix and display
them along with their position.
PROGRAM:
import java.util.*;
public class Program71
{public static void main (String args[])
arr[i][j] = sc.nextInt(); }
169
{
for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();
}
max = arr[0][0];
min = arr[0][0];
for (i = 0; i < m; i++) {
}
if (arr[i][j] < min) {
}}
System.out.println ("Minimum value: " + min + " at row " + (rmin+1) + " and
column " + (cmin+1));
System.out.println ("Maximum value: " + max + " at row " + (rmax+1) + "
and column " + (cmax+1));
170
t = arr[i][k];
arr[i][k] = arr[i][k+1]; arr[i][k+1] = t;
}}}}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();
}}}
OUTPUT:
171
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.
PROGRAM:
if (el == x[m]) f = 1;
if (el < x[m]) ub = m-1;
}}
172
}}
OUTPUT:
173
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.
PROGRAM:
{
int d, s = 0; while (n != 0) {
d = n % 10; s = s + d;
n = n / 10;
return (s); }
if (num % i == 0) {
if (i % j == 0)
f++; }
if (f == 0) {
f = 0; }}
if (fpsum == dsum)
System.out.println ("It is a smith number"); else
System.out.println ("It is not a smith number");
}}
OUTPUT:
175
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.
PROGRAM:
import java.util.*;
public class Program74
{public int circular(int number, int dn)
{
int s = 0, fd, digits;
fd = number / (int) Math.pow(10,dn-1); digits = number % (int)
Math.pow(10,dn-1); s = (digits * 10) + fd;
return (s);
}
public static void main (String args[]) {
dnum++;
176
n = n / 10; }
if (cprime % j == 0)
f++; }
if (f != 0) {
System.exit(0); }
else continue; f = 0;
}}
System.out.println ("It is a circular prime"); }}
OUTPUT:
177
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.
1 x4
PROGRAM:
178
}
for (i = 0; i < note.length; i++) {
if (noteval[i] != 0)
}}
OUTPUT:
179
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:
Sample Input:
34 9
25 8
16 7
Sample output:
12 3
65 4
78 9
Sum of corner elements=20
PROGRAM:
180
System.exit(0); }
int arr[][] = new int[m][m]; System.out.println ("Enter the elements: "); for (i
= 0; i < m; i++)
{
arr[i][j] = sc.nextInt(); }
}
System.out.println ("The matrix rotated 90 degrees clockwise: "); for (i = 0; i
< m; i++)
{
}
181
System.out.println(); }
OUTPUT:
182
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:
(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
PROGRAM:
Vowels Consonants 5 7
13
36
1154
public static void main (String args[]) {
183
str = sc.nextLine();
if (str.endsWith(".") == false && str.endsWith("?") == false) {
System.exit(0); }
ch = str.charAt(i+1);
ch = Character.toUpperCase(ch); str2 = str2 + " " + ch;
i++;
}
else
str2 = str2 + ch;
}
str2 = str2.trim();
System.out.println (str2);
str2 = str2 + " ";
System.out.println ("Words" + "\t" + "Vowels" + "\t" + "Consonants"); for (i
= 0; i < str2.length(); i++)
{
184
{
for (j = 0; j < word.length(); j++) {
char ch2 = word.charAt(j);
if (ch2 == 'A' || ch2 == 'E' || ch2 == 'I' || ch2 == 'O' || ch2 == 'U') vc++;
else if (ch2 == 'a' || ch2 == 'e' || ch2 == 'i' || ch2 == 'o' || ch2 == 'u') vc++;
else
cc++;
}
System.out.println (word + "\t" + vc + "\t" + cc); word = "";
vc = 0;
cc = 0;
}
else
word = word + ch;
} }}
OUTPUT:
185
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
PROGRAM:
import java.util.*;
public class Program78
{public boolean repetition(int n)
{
int a, b;
char c = num.charAt(a);
for (b = a+1; b < num.length(); b++) {
if (c == num.charAt(b)) {
186
}
public static void main (String args[]) {
System.out.println ("Enter the lower limit and the upper limit: "); n1 =
sc.nextInt();
n2 = sc.nextInt();
for (i = n1; i <= n2; i++)
{
rpt = ob.repetition(i); if (rpt == true)
{
c++; }}
System.out.println();
OUTPUT:
187
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?
PROGRAM:
char newc;
int cnum = (int) c;
if (cnum >= 65 && cnum <= 90) {
}
else if (cnum >= 97 && cnum <= 122) {
188
else
newc = c; return newc;
}
public static void main (String args[]) {
ch = str.charAt(i);
ch = ob.replace13(ch); System.out.print(ch);
}}
OUTPUT:
189
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
deinrstu
PROGRAM:
190
{
Scanner sc = new Scanner (System.in);
int i, j, f;
String str;
char ch1, ch2;
System.out.println ("Enter a sentence: ");
str = sc.nextLine();
str = str.toLowerCase();
System.out.println ("Consecutive characters: "); for (i = 0; i < str.length() - 1;
i++)
{
ch1 = str.charAt(i);
ch2 = str.charAt(i+1);
if (ch1 == ch2 || (int)ch1 + 1 == (int)ch2) {
System.out.println (ch1);
System.out.println (ch2); }
}
int rpt[] = new int[26]; for (i = 0; i < 26; i++) rpt[i] = 0;
for (i = 97; i <= 122; i++) {
191
rpt[i-97]++; }
}
System.out.println ("Repeating characters: "); for (i = 0; i < 26; i++)
{
if (rpt[i] > 1) {
f = i + 97;
System.out.println ((char)f); }
}}
OUTPUT:
192
PROGRAM 81
PROGRAM:
int i, j, t = 4;
for (i = 1; i <= 9; i = i + 2) {
if (j % 2 != 0) System.out.print ("*");
else
System.out.print ("\t" + "\t");
193
System.out.println();
t--; }
t = 1;
for (i = 7; i >= 1; i = i - 2) {
if (j % 2 != 0) System.out.print ("*");
else
System.out.print ("\t" + "\t");
} System.out.println(); t++;
}}
OUTPUT:
194
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:
PROGRAM:
if (n <= 2 || n >= 9) {
System.exit(0); }
team[i] = sc.nextLine();
l[i] = team[i].length(); }
maxl = l[i]; }
if (i < l[j])
System.out.print (team[j].charAt(i) + "\t"); else
System.out.print ("\t");
196
System.out.println(); }
}}
OUTPUT:
197
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
PROGRAM:
{
for (i = 0; i < word1.length(); i++) {
198
ch1 = word1.charAt(i);
for (j = 0; j < word2.length(); j++) {
break; }
}}
}}
OUTPUT:
199
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:
-2 0 4 5
1 3 6 -4
Largest number: 9
Row: 0
Column: 2
Smallest Number: -4 Row=2
Column=3
The rearranged matrix: -4 2 0 1
334 5
678 9
200
PROGRAM:
}}
201
System.out.println(); }
max = arr[0][0];
min = arr[0][0];
for (i = 0; i < m; i++) {
}
if (arr[i][j] < min) {
}}
System.out.println ("Minimum value: " + min + " at row " + (rmin+1) + " and
column " + (cmin+1));
System.out.println ("Maximum value: " + max + " at row " + (rmax+1) + "
and column " + (cmax+1));
202
{
if (sda[j] > sda[j+1]) {
t = sda[j];
sda[j] = sda[j+1]; sda[j+1] = t;
}}
}
k = 0;
for (i = 0; i < m; i++) {
arr[i][j] = sda[k];
k++; }
}
System.out.println ("The sorted array is: "); for (i = 0; i < m; i++)
{
}}
203
OUTPUT:
204
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.
PROGRAM:
System.exit(0); }
n = num;
prev = n % 10; while (n != 0) {
205
inc = false;
break; }
prev = d;
n = n / 10; }
n = num;
prev = n % 10; while (n != 0) {
dec = false;
break; }
prev = d;
n = n / 10; }
}}
206
OUTPUT:
207
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”.
Sample Data:
Input:
n=4
ABCE
Output: Invalid! Only alternate letters permitted! Sample Data:
Input:
n=7
Output: Length of string should not exceed 6 characters! Sample
Data:
Input:
n=3
ABCE
Output: Valid
Sample Data:
Input:
n=5
GEAIK
Output: Valid
208
PROGRAM:
System.out.println ("Enter the length of the code and the code: "); n =
sc.nextInt();
if (n > 6)
{
System.exit(0); }
code = sc.next();
for (i = 0; i < code.length(); i++) {
ch = code.charAt(i);
if (Character.isUpperCase(ch) == false) {
}}
209
ch = code.charAt(i);
ch2 = code.charAt(i+1);
if ((int) ch + 1 == (int) ch2) {
System.out.println ("Invalid! Only alternate letters permitted!"); flag = 1;
System.exit(0);
}}
if (flag == 0)
System.out.println ("Valid"); }