0% found this document useful (0 votes)
16 views155 pages

Isc Grade 12 Computer Project

The document is a project certificate for Mohammed Zayan Tariq from JSS International School, certifying the completion of a Grade XII Mathematics project in the year 2023-2024. It includes an index of various programming tasks and algorithms related to mathematics and computer science, covering topics such as triangle types, discounts, income tax calculations, and matrix operations. Each program is accompanied by its Java code implementation.

Uploaded by

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

Isc Grade 12 Computer Project

The document is a project certificate for Mohammed Zayan Tariq from JSS International School, certifying the completion of a Grade XII Mathematics project in the year 2023-2024. It includes an index of various programming tasks and algorithms related to mathematics and computer science, covering topics such as triangle types, discounts, income tax calculations, and matrix operations. Each program is accompanied by its Java code implementation.

Uploaded by

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

ISC GRADE 12 COMPUTER PRO

Mohammed Zayan 12 A – JSS INTERNATIONAL SCHOOL


JSS INTERNATIONAL SCHOOL

CERTIFICATE

This is to certify that Mst. Mohammed Zayan Tariq has


satisfactorily completed the project in Mathematics
prescribed by the ISC BOARD- Grade XII of this institution in
the year 2023 - 2024.

Signature of Teacher in – charge

Name of the Candidate: Mohammed Zayan Tariq

Date: 3rd January 2024


INDEX
Program Program Page Teacher’s
number description number signature
1. Input three sides of
triangle and if triangle is
possible then display type
of triangle or display not
possible
2. Compute and print the
following festival discount
and assured gifts for
amount of purchase in a
clock showroom.
3. Compute and print the
stream for admission in
grade 11 based on the
marks in tenth.
4. Input consumer name,
number and units
consumed and calculate
amount to be paid
according to given tariff
5. Compute and print the
income tax to be paid
based on given table and
if gender is female or age
is 65 then display wrong
category.
6. For a courier, compute
and print the cost of mail
in ordinary and express
type according to given
tariff.
7. Execute a program to
compute and print the
fine for returning books
late to the library by n
days.
8. Execute menu driven
program to find area of
circle square and
rectangle.
9. Menu driven program to
find volume of cube,
cuboid or sphere
10. Menu driven program to
find depreciated value of
a car based on number of
years it has been used.
11. Calculate relative velocity
of two trains and time
taken to cross each other
by a menu driven
program.
12. Menu-driven program to
calculate the discounted
price with name and
address.
13. Menu-driven program to
find area of equilateral
isosceles or scalene
triangles
14 Menu driven program to
calculate gross salary net
salary and check whether
an employee has to pay
Income Tax or not.
15 Menu Driven program for
finding discounted price
based on type of room
and number of days of
stay.
16. Menu Driven program to
perform arithmetic
operation on numbers
input by user.
17. Program to calculate the
sum of positive even
numbers and negative
odd numbers and accept
0 to quit and display the
result.
18. Accept a number and
print the factorial using
do while loop.
19. Check whether a number
input by the user is even
or odd.
20. Accept a number and
print the odd numbers
below it.

21. Print the pattern

22. Find whether a number


input by the user is a
perfect number.
23. Find GCD of two numbers
input by the user.
24. Find whether a number
input by the user is a
neon number.
25. Find whether a number
input by the user is an
Armstrong number
26. Find whether a number
input by the user is a
palindrome number.
27. Find the maximum and
minimum numbers from
10 different numbers
input by the user.
28 To display n number of
terms of Fibonacci series.
29. To input a number and
check whether it is a
niven number or not.
30. To input and check
whether a number is
happy number or not.
31. To input and check
whether a number is
prime number or not and
display 10 prime number
after it.
32. To print given patterns
using switch case.
33. To print given pattern
using switch case
34. To print given pattern
using switch case
35. To print given pattern
using switch case
36. To print given pattern
using switch case
37. To input a number and
check whether it is an
automorphic number or
not
38. To check whether the
input code is a legal ISBN
code.
39. To print the sum of
pattern using switch case
40. To print the sum of
pattern using switch case
41. To print the sum of
pattern using switch case
42. To convert a decimal
number to binary number
and vice-versa using
switch case.
43. To input a number and
print a pattern of
consecutive natural
numbers which add up to
the number itself.
44. To input a number
between 1 to 5 and
display the required
patterns and number of
patterns
45. To round off a given
number to certain
number of digits as per
user’s choice
46. To accept a word from
the user and print the
given pattern
47. Accept three words and
perform the given tasks.
48. Accept a sentence and
count and remove the
words with repeating
letters.
49. To accept a sentence and
pint the new sentence
after performing the
given tasks
50. To accept a sentence and
change each consonant to
its previous letter
provided that letter is not
a vowel.
51. To accept a sentence and
perform the given tasks.
52. To accept a word and
print the patterns using
switch case.
53. To accept 10 numbers in a
single dimensional array
and find a number and
print appropriate
message.
54. To sort an array using
selection sort technique.
55. To sort an array using
bubble sort technique.
56. To merge two single
dimensional arrays.
57. To insert an element into
an array.
58. To delete an element
from the given array
according to user input
position.
59 To accept 15 names and
display the names that
start with the letter
accepted and arrange
given array in alphabetical
order.
60. To accept the seven
wonders and their
respective countries in
two different arrays and
search for the country in
the array and display the
respective wonder.
61. To accept numbers in
DDA and find sum of left
and right diagonal.
62. To accept numbers in
DDA and find sum of
outer and inner
elements.
63. To accept the elements
DDA and remove corner
elements.
64. To accept elements of a
matrix and arrange the
rows of the array as per in
the question.
65. To accept the elements of
a matrix and print the
sum of each column and
row along with the
elements of that row and
column.
66. To accept the elements of
a matrix and print the
elements which are below
the left and right
diagonals of the matrix.
67. To accept the elements of
the matrix and print its
mirror image.
68. To accept the elements of
a matrix and print the
matrix after shifting each
element to the next
closest prime number.
69. To accept integer
numbers in a matrix and
convert all elements into
roman numerals.
70. To accept elements of a
matrix and check if it is a
magic square.
71. To accept elements of
matrix and display
maximum and minimum
numbers. Then, sort the
array.
72. Accept elements of single
dimensional array and
search for number using
binary search technique.
Perform this using
recursive function.
73. Accept a number and
check if it is a smith
number or not.
74. Accept a number and
check if it is circular prime
or not.
75. To accept an amount
from user and print
denomination in terms of
rupee notes.
76. Accept elements of a
matrix, rotate it 90
degree clockwise, and
find the sum of corner
elements.
77. Accept a string and check
its validity and then print
number of vowels and
consonants in the given
format.
78. To accept lower and
upper limits of numbers
and print those numbers
which have no repetition
of digits.
79. To use Caeser Cipher
rotation.
80. To accept a string and
perform the given tasks
81. Print the pattern
82. Print the names of teams
in vertical manner
83. To check if words entered
are anagram.
84. To accept the elements of
array and perform the
given tasks
85. To check whether a
number is bouncy
86. To check the validity of
the code input.
PROGRAM 1
import java.util.*;
public class Prog1
{
public static void main (String args[])
{
int a, b, c;
Scanner sc = new Scanner (System.in);
System.out.println ("Enter three sides of a triangle: ");
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
if ((a + b < c) || (b + c < a) || (a + c < b))

System.out.println ("Triangle is not possible");

else { if (a == b & b == c)

System.out.println ("The triangle is possible and an equilateral triangle");

else if (a == b || b == c || c == a)

System.out.println ("The triangle is possible and an isosceles triangle");

else System.out.println ("The triangle is possible and a scalene triangle");

}
OUTPUT
PROGRAM 2
import java.util.*;
public class Prog2
{
public static void main (String args[])

{ Scanner sc = new Scanner (System.in);

int c1; double c2 = 0.0;

System.out.println ("Enter the cost: ");


c1 = sc.nextInt();
if (c1 <= 2000)
{
c2 = c1 - (c1 * 5.0/100.0);
System.out.println ("Assured Gift = Wall Clock");
System.out.println ("Discount = 5%");
System.out.println ("Final cost = " + c2);
}
else if (c1 >= 2001 && c1 <= 5000)
{
c2 = c1 - (c1 * 10.0/100.0);
System.out.println ("Assured Gift = School Bag");
System.out.println ("Discount = 10%");
System.out.println ("Final cost = " + c2);
}
else if (c1 >= 5001 && c1 <= 10000)
{
c2 = c1 - (c1 * 15.0/100.0);
System.out.println ("Assured Gift = Electric Iron");
System.out.println ("Discount = 15%");
System.out.println ("Final cost = " + c2);
}
else { c2 = c1 - (c1 * 20.0/100.0);
System.out.println ("Assured Gift = Wrist Watch");
System.out.println ("Discount = 20%");
System.out.println ("Final cost = " + c2);
}
}
}
OUTPUT
PROGRAM 3
import java.util.*;
public class Program3
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
double english, maths, science;
System.out.println ("Enter the marks for english, math and science: ");
english = sc.nextDouble();
maths = sc.nextDouble();
science = sc.nextDouble();
System.out.print ("The stream is : ");
if (english >= 80.0 && maths >= 80.0 && science >= 80.0)
System.out.println ("Pure Science");
else if (english >= 80.0 && science >= 80.0 && maths >= 60)
System.out.println ("Bio Science");
else System.out.println ("Commerce");
}
}

OUTPUT
PROGRAM 4

import java.util.Scanner;

public class Prog4

public static void main (String args[])

Scanner sc = new Scanner (System.in);


int units, num;
String name;
double amt = 0.0;
System.out.println ("Enter name, number and units consumed: ");

name = sc.nextLine();

num = sc.nextInt();

units = sc.nextInt();

if (units <= 100)

amt = 5.50 * units;

if (units > 100 && units <= 300)

amt = 550 + ((units - 100) * 6.50);

if (units > 300 && units <= 600)

amt = 550 + 1300 + ((units - 300) * 7.50);


if (units > 600)

amt = 550 + 1300 + 2250 + ((units - 600) * 8.50);

System.out.println ("The Name:”+name+ ”The number:”+num+);

System.out.println ("The amount is: " + amt);

)
Program 5

import java.util.*;

public class Prog5

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int age;

String gender;

double inc,

tax = 0.0;

System.out.println ("Enter gender, age and taxable income: ");

gender = sc.nextLine();

age = sc.nextInt();

inc = sc.nextDouble();

if (gender.equalsIgnoreCase("female") == true || age > 65)

System.out.println ("Wrong category");


else
{

if (inc > 160000 && inc <= 500000)

tax = (inc - 160000) * (10.0/100.0);

if (inc> 500000 && inc <= 800000)

tax = ((inc - 500000) * (20.0/100.0)) + 34000;

if (inc> 800000)

tax = ((inc - 800000) * (30.0/100.0)) + 94000;

System.out.println ("The tax is: " + tax);

}
Program 6

import java.util.*;

public class Prog6

public static void main (String args[])

Scanner sc = new Scanner (System.in);

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);

if (chr == 'o' || chr == 'O')

if (weight <= 100)

o_charge = 50;
if (weight >= 101 && weight <= 500)

o_charge = 50 + 40 * ((weight - 100)/100);

if (weight >= 501)

o_charge = 50 + 160 + 35 * ((weight - 500)/100);

System.out.println ("The charge is: Rs." + o_charge);

else if (chr == 'e' || chr == 'E')

if (weight <= 100)


e_charge = 80;
if (weight >= 101 && weight <= 500)
e_charge = 80 + 70 * ((weight - 100)/100);
if (weight >= 501)
e_charge = 80 + 280 + 65 * ((weight - 500)/100);

System.out.println ("The charge is: Rs." + e_charge);

}
else
System.out.println ("Invalid choice");

}
Program 7

import java.util.*;

public class Prog7

public static void main (String args[])

Scanner sc = new Scanner (System.in);


int n;
double f = 0.0;
System.out.println ("Enter the number of days late: ");

n = sc.nextInt();

if (n > 5 && n <= 10)


f = 2.00 + ((n-5) * 0.65);

if (n <= 5)
f = n * 0.40;

if (n > 10 && n <= 20)

f = 2.00 + 3.25 + ((n-10) * 0.80);

System.out.println ("The fine is: " + f);

}
Program 8

import java.util.*;

public class Prog8

public static void main (String args[])

Scanner sc = new Scanner (System.in);

double carea = 0.0, sarea = 0.0, rarea = 0.0;

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':

System.out.println ("Enter the length and breadth of the rectangle");

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.*;

public class Program9

public static void main (String args[])

Scanner sc = new Scanner (System.in);


double cube = 0.0, sphere = 0.0, cuboid = 0.0;
int n;
System.out.println ("To find volume, enter: 1 for cube, 2 for sphere, 3 for
cuboid");

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:

System.out.println ("Enter the radius of the sphere: ");


double r = sc.nextDouble();
sphere = 4 * 3.14 * r * r * r / 3;
System.out.println ("The volume of the sphere is: " + sphere);

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.*;

public class Prog10

public static void main (String args[])

Scanner sc = new Scanner (System.in);


int year;
double price, dprice = 0.0;
System.out.println ("Enter the price of car and number of years used: ");

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);

System.out.println ("The depreciated price is: " + dprice);

PROGRAM 11
import java.util.*;

public class Prog11

public static void main (String args[])

Scanner sc = new Scanner (System.in);


double vsame = 0.0, vopp = 0.0;
int n;
System.out.println ("Enter the velocity of the two trains");

double v1 = sc.nextDouble();

double v2 = sc.nextDouble();

System.out.println ("To find relative velocity, enter 1 if trains are travelling in


same direction, 2 if trains are travelling in opposite direction");

n = sc.nextInt();

switch (n)
{

case 1:
if (v1 >= v2)

vsame = v1 - v2;

else

vsame = v2 - v1;

System.out.println ("The relative velocity is: " + vsame);

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

public static void main(String args[])

Scanner sc = new Scanner (System.in);


String name, address;
char chr;
double price;

double dprice = 0.0;


System.out.println ("Enter name, address, and amount of purchase: ");

name = sc.nextLine();

address = sc.nextLine();

price = sc.nextInt();

System.out.println ("Enter L for laptop and D for Desktop");

chr = sc.next().charAt(0);
switch (chr)
{

case 'L':

if (price<=25000)

dprice = price;
if (price>= 25001 && price <= 57000)

dprice = price - ((5.0/100.0) * price);

if (price >= 57001 && price <= 100000)

dprice = price - ((7.5/100.0) * price);

if (price >100000)

dprice = price - ((15.0/100.0) * price);

break;

case 'D':

if (price <= 25000)

dprice = price - ((5.0/100.0) * price);

if (price >= 25001 && price <= 57000)

dprice = price - ((7.5/100.0) * price);

if (price >= 57001 && price <= 100000)

dprice = price - ((10.0/100.0) * price);

if (price >100000)

dprice = price - ((15.0/100.0) * price);

break;

default:

System.out.println ("Invalid choice");

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

System.out.println ("Address: " + address);


System.out.println ("Total amount: " + dprice);

PROGRAM 13

import java.util.*;

public class Prog13

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int n;

double area = 0.0;

System.out.println ("To find area of triangle, enter 1 for equilateral, 2 for


isosceles and 3 for scalene:");

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));

System.out.println ("The area is: " + area);

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));

System.out.println ("The area is: " + area);


break;

default:
System.out.println ("Wrong choice");

}
Program 14
import java.util.*;
public class Prog14

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int e_num, age, basic;

double d = 0.0, hr = 0.0, p = 0.0, ef = 0.0, g = 0.0, net = 0.0;


char ge;
System.out.println ("Enter employee number, age, basic salary and gender:
");

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);

System.out.println("The Employee Number: " + e_num);

System.out.println("Gross salary: " + g);

System.out.println("Net Salary: " + net);


if (gen == 'M')
{

if (gross <= 250000)


System.out.println("Not an income tax payer");

else
System.out.println("Income tax payer");

if (gen == 'F')

if (gross <= 300000)


System.out.println("Not an income tax payer");

else

System.out.println("Income tax payer");

}
Program 15
import java.util.*;

public class Prog15

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int days;

double amt = 0.0;

String name, ch;

System.out.println ("Enter your name");

name = sc.nextLine();

System.out.println ("Enter S for Semi-Deluxe room, D for Deluxe room, SD for


Super Deluxe room");

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);

if (days > 3 && days <= 5)

amt = 2500 - ((15.0/100.0) *2500);

if (days > 5 && days <= 10)

amt = 2500 - ((20.0/100.0) *2500);

if (days > 10)


amt = 2500 - ((30.0/100.0) * 2500);

break;

case "D":
if (days <= 3)
amt = 3500 - ((10.0/100.0) *3500);

if (days > 3 && days <= 5)

amt = 3500 - ((15.0/100.0) *3500);

if (days > 5 && days <= 10)

amt = 3500 - ((20.0/100.0) *3500);

if (days > 10)

amt = 3500 - ((30.0/100.0) * 3500);

break;

case "SD":
if (days <= 3)

amt = 5000 - ((10.0/100.0) * 5000);

if (days > 3 && days <= 5)


amt = 5000 - ((15.0/100.0) * 5000);

if (days > 5 && days <= 10)


amt = 5000 - ((20.0/100.0) * 5000);

if (days > 10)


amt = 5000 - ((30.0/100.0) * 5000);

break;

default:

System.out.println ("Invalid choice");

if (amt != 0.0)

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

System.out.println ("Amount to be paid: " + amt);

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:

import java.util.*; public class Program16 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int a, b, ch;

double ans = 0.0;

System.out.println ("Enter 1 for addition, 2 for subtraction, 3 for


multiplication and 4 for division: ");

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

System.out.println ("The difference is: " + ans); break;


case 3:
ans = a*b;

System.out.println ("The product is: " + ans); break;


case 4:
ans = ((double) a) / ((double) b); System.out.println ("The quotient is: " +
ans); break;

default:

System.out.println ("Invalid choice"); }

}}

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:

import java.util.*; public class Program17 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int num = 1, odd = 0, even = 0;


System.out.println ("Enter numbers: "); while (num != 0)

{
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++; }

while (i <= n);

System.out.println ("The factorial is: " +f); }}

OUTPUT:
47

PROGRAM 19
Execute a program to check whether a number input by the user is
even or odd.

PROGRAM:

import java.util.*; public class Program19 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int num;


System.out.println ("Enter a number: "); num = sc.nextInt();

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:

import java.util.*; public class Program20 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int n, i;


System.out.println ("Enter a number: "); n = sc.nextInt();

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

if (i % 2 != 0)

System.out.print (i + " "); }

}}

OUTPUT:

49

PROGRAM 21
Execute a program to print the given pattern. 1
12
12 3

123 4 1234 5
PROGRAM:

public class Program21 {

public static void main (String args[]) {

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

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

System.out.print (j + " "); }

System.out.println (); }}}

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:

import java.util.*; public class Program22 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int num, f = 0, i;


System.out.println ("Enter a number"); num = sc.nextInt();
for (i = 1; i < num; i++) {

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.

Sample Input: 25, 45


Sample Output: GCD of 25,45 is 5.

PROGRAM:

import java.util.*; public class Program23 {

public static void main (String args[]) {


Scanner sc = new Scanner (System.in);
int num1, num2, i, gcd = 0; System.out.println ("Enter two numbers"); num1
= sc.nextInt();

num2 = sc.nextInt(); if (num1 > num2)


{

for (i = 1; i < num1; i++) {

if (num1%i==0 && num2%i==0)

gcd = i; }

} else {

53

for (i = 1; i < num2; i++) {

if (num1%i==0 && num2%i==0)

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.

Sample Input: 9 Sample Output: 9*9=81

8+1=9 9 is a neon number.

PROGRAM:

import java.util.*; public class Program24 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int n, sq, d = 0, sum = 0;


System.out.println ("Enter a number"); n = sc.nextInt();

sq = n * n; while (sq != 0) {

d = sq%10; sum = sum + d; sq = sq/10;

}
if (sum == n)
System.out.println ("It is a neon number");

55

else

System.out.println ("It is not a neon number"); }

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.

Sample Input: 153


Sample Output: 13 + 53 + 33 = 1+125+27=153. 153 is an Armstrong
number.

PROGRAM:

import java.util.*; public class Program25 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int num, n, d, cube = 1, sum = 0;


System.out.println ("Enter a number"); n = sc.nextInt();

num = n;
while (num > 0) {

d = num%10 ; cube = d * d * d; sum = sum + cube; num = num/10;

}
if (sum == n)

57

System.out.println (n + " is an Armstrong number"); else


System.out.println (n + " is not an Armstrong number");

}}
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.

Sample Input: 272


Sample Output: 272. It is a palindrome number.

PROGRAM:

import java.util.*; public class Program26 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int num, d, s = 0;


System.out.println ("Enter a number: "); num = sc.nextInt();

int n = num; while (n != 0) {

d = n % 10; s = s*10 + d; n = n/10;

}
if (s == num)
System.out.println ("It is a palindrome number"); else

59

System.out.println ("It is not a palindrome number"); }


}

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:

import java.util.*; public class Program27 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int max, min, num, i;
System.out.println ("Enter 10 numbers: "); num = sc.nextInt();

max = num;
min = num;
for (i = 2; i <= 10; i++) {

num = sc.nextInt(); if (num > max) max = num;


if (num < min) min = num;

}
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:

import java.util.*; public class Program28 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int a = 0, b = 1, c = 0, n, i;
System.out.println ("Enter the number of terms: "); n = sc.nextInt();

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
{

System.out.print (a + " " + b + " "); for (i = 3; i <= n; i++)


{

c = a + b;

63

System.out.print (c + " "); a = b;


b = c;

}}

}}

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.

Sample Input: 126


Sample Output: 1+2+6=9
126/9=14 hence 126 is a niven number.

PROGRAM:
import java.util.*; public class Program29 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int n, num, d = 0, sum = 0;


System.out.println ("Enter a number"); n = sc.nextInt();

num = n;
while (num > 0) {

d = num % 10; sum = sum + d; num = num / 10;

}
if (n % sum == 0)
System.out.println ("It is a niven number"); else

65

System.out.println ("It is not a niven number"); }

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:

import java.util.*; public class Program30 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int n, s = 0, r;


System.out.println ("Enter a number: "); n = sc.nextInt();

s = n; do
{

n = s; s = 0; do
{

r = n % 10;

67

s = s + r*r;

n = n/10; }

while (n > 0); }

while(s > 9);


if (s == 1)
System.out.println ("It is a happy number"); else
System.out.println ("It is not a happy number");

}}

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

from the number input by the user.

PROGRAM:

import java.util.*; public class Program31 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int n1, n2, i, f = 0, j, l = 10;
System.out.println ("Enter a number to check if it is prime"); n1 =
sc.nextInt();
for (i = 2; i < n1; i++)
{

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

System.out.println ("The 10 prime numbers after it are: "); for (i = n2;


i<n2+l; i++)
{

for (j = 2; j < i; j++) {

if (i%j == 0)

f = f+1; }

if (f == 0) System.out.println (i); else


l = l + 1;
f = 0;

}}

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:

import java.util.*; public class Program32 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, ch;
System.out.println ("Enter 1 or 2 to display a pattern: "); ch = sc.nextInt();

switch (ch) {

71

case 1:
for (i = 1; i <= 5; i++) {

for (j = 5; j >= i; j--) {

System.out.print (j + " "); }

System.out.println(); }

break;
case 2:
for (i = 1; i <= 5; i++) {

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

if (j%2 == 0) System.out.print ("# "); else


System.out.print ("* ");

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

3334 5 4444 5 5555 5 2 for 1357 9 3579 1 5791 3 9135 7

PROGRAM:

import java.util.*; public class Program33 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, ch, k;
System.out.println ("Enter 1 or 2 to display a pattern: "); ch = sc.nextInt();

switch (ch) {
case 1:

74

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

for (j = 1; j <= 5; j++) {

if (j <= i) System.out.print (i + " "); else


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

System.out.println (); }

break;
case 2:
for (i = 1; i <= 9; i = i + 2) {

for ( j = i; j <= 9; j = j + 2) System.out.print (j + " "); for (k = 1; k < i; k = k


+ 2) System.out.print (k + " "); System.out.println();

}
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

66666 6 5555 5 444 4


33 3

22 1

PROGRAM:

import java.util.*; public class Program34 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int ch, i, j;
System.out.println ("Enter 1 or 2 to display a pattern: "); ch = sc.nextInt();

switch (ch)

77
{
case 1:

for (i = 5; i >= 1; i--) {

for (j = 5; j >= 1; j--) {

if (j > i)
System.out.print (" "); else
System.out.print (j + " ");

System.out.println(); }

break;
case 2:
for (i = 6; i >= 1; i--) {

for (j = i; j >= 1; j--) {

System.out.print (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

12 3 123 4 1234 5 123 4 12 3


12

1
2 for 1111 1

22 2 3

22 2
1 1 1 11

PROGRAM:

import java.util.*; public class Program35 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int i, j, ch;

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 (j = 1; j <= i; j++) System.out.print (j + " "); System.out.println();

}
for (i = 4; i >= 1; i--) {

for (j = 1; j <= i; j++) System.out.print (j + " "); System.out.println ();

}
break;
case 2:
int k = 1;
for (i = 1; i <= 3; i++) {

for (j = 1; j < i; j++) System.out.print (" "); for (j = 5; j >= k; j--)


System.out.print (i + " "); k = k+2; System.out.println();

81

}
k = 3;
for (i = 2; i >= 1; i--) {

for (j = 1; j < i; j++) System.out.print (" "); for (j = 5; j >= k; j--)


System.out.print (i + " "); k = k - 2; System.out.println();

}}

}}

OUTPUT:
82

PROGRAM 36
Execute a program to generate the following patterns using switch
case.

PROGRAM:

import java.util.*; public class Program36 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, k, ch;
System.out.println ("Enter 1 or 2 to display a pattern: "); ch = sc.nextInt();

switch (ch) {

case 1:
for (i = 5; i > 1; i--) {

for (j = 5; j >= 1; j--)


83

{
if (j > i)

System.out.print (" "); else


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

System.out.println(); }

k = 1;
for (i = 5; i >= 1; i--) {

for (j = 1; j < i; j++) System.out.print (" "); for (j = i; j <= 5; j++) {

System.out.print (k + " ");

k++; }

k = 1;

System.out.println(); }

break;
case 2:
int n = 1;
for (i = 0; i < 5; i++) {

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

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:

import java.util.*; public class Program37 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int c = 0, num, snum, r;


System.out.println ("Enter a number: "); num = sc.nextInt();

snum = num * num; int n = num;


while (n != 0)
{

c++;

n = n / 10; }

r = snum % (int)Math.pow(10,c); if (r == num)

86

System.out.println ("It is an automorphic number"); else


System.out.println ("It is not an automorphic number");

}}

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:

(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:

1. (i) Input the ISBN code 10-digit number.


2. (ii) If the ISBN is not a 10-digit number, then output the
statement “Illegal ISBN

code” and terminate the program.

3. (iii) If the code is a 10-digit number, then extract the digits


from the code and compute

the sum as explained above.

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:

import java.util.*; public class Program38 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int isbn, c = 10, s = 0, d = 0, n; System.out.println ("Enter the ISBN code: ");
isbn = sc.nextInt();

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:

import java.util.*; public class Program39 {

public static void main (String args[]) {


Scanner sc = new Scanner (System.in);
int i, s = 0;
System.out.println ("Enter 1 or 2 to find sum of series: "); int ch =
sc.nextInt();

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; }

System.out.println ("The sum: " + s); break;

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:

import java.util.*; public class Program40 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, ch, n;
double s = 0.0;
System.out.println ("Enter 1 or 2 to find sum of a series: "); ch = sc.nextInt();

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;

System.out.println ("The sum: " + s);

break;

case 2:

System.out.println ("s = 1 + 2/(1*2) + 1 + 2 + 3/(1*2*3) + ... + 1 + 2 + 3 ...


+ n/(1*2*3...*n) ");

System.out.println ("Enter the number of terms: "); n = sc.nextInt();


j = 0;
int f = 1;

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

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:

import java.util.*; public class Program41 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, ch, n, a;
double s = 0.0;
System.out.println ("Enter 1 or 2 to print sum of the series: "); ch =
sc.nextInt();

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; }

System.out.println ("The sum: " + s);


break;
case 2:
System.out.println ("s = a/(2^2) + (a^2)/(2^3) + (a^3)/(2^4) + ... + 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++) {

s = s + (Math.pow(a,i))/Math.pow(2,i+1); }

System.out.println ("The sum: " + s); break;


default:
System.out.println ("Wrong choice");

}}}

OUTPUT:

96

PROGRAM 42
Execute a program using switch case to convert a decimal number
to binary number and vice-versa.

PROGRAM:

import java.util.*; public class Program42 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);

int dcml = 0, bnry = 0, ch, d, n;

System.out.println ("Enter 1 to convert decimal number to binary number


and 2 to convert binary number to decimal: ");

ch = sc.nextInt(); switch (ch)


{

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.

Sample Input: 15 Sample Output: 1 2 3 4 5

45 6 78

import java.util.*; public class Program43 {

public static void main (String args[]) {


Scanner sc = new Scanner (System.in); int i, j, k, n, s, a;
System.out.println ("Enter a number: "); n = sc.nextInt();

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

for (j = i; j <= n; j++) {

s = 0;
for (k = i; k <= j; k++) s = s + k;
if (s == n)
{

PROGRAM:

100

for (a = i; a <= j; a++) {

System.out.print (a + " "); }

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.

Sample Input: 3 Sample Output: 12 3


32 1

13 2
23 1
21 3
31 2
Number of Combinations=6

PROGRAM:

import java.util.*; public class Program44 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, k, l, m, c = 0, num;
System.out.println ("Enter a number from 2 to 5(inclusive): "); num =
sc.nextInt();
for (i = 1; i <= num; i++)
{

for (j = 1; j <= num; j++) {

102

if (num == 2) {

if (i != j) {

System.out.println (i + " " + j);

c++; }

} else {

for (k = 1; k <= num; k++) {

if (num == 3) {

if (i != j && j != k && i != k) {
System.out.println (i + " " + j + " " + k);

c++; }

} else {

for (l = 1; l <= num; l++) {

if (num == 4) {

if (i != j && i != k && i != l && j != k && j != l && k != l) {

103

System.out.println (i + " " + j + " " + k + " " + l);

c++; }

} else {

for (m = 1; m <= num; m++) {

if (i != j && i != k && i != l && i != m && j != k && j != l && j!= m && k != l


&& k != m && l != m)

{
System.out.println (i + " " + j + " " + k + " " + l + " " + m); c++;

}}

}}

}}

}}

System.out.println ("The number of combinations: " + 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.

Sample Input: N=127.67898


D=3
Sample Output:
The number is 127.679

PROGRAM:

import java.util.*; public class Program45 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


double num, newnum, dplace, n;
int d, deci, newd;
System.out.println ("Enter a number: ");
num = sc.nextDouble();
System.out.println ("Enter the number of decimal places required: "); dplace
= sc.nextDouble();
n = num - (int)num;
deci = (int)(n * (Math.pow(10.0,dplace+1.0))); d = deci % 10;
if (d < 5)
newd = deci / 10;

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:

import java.util.*; public class Program46 {

public static void main (String args[]) {


Scanner sc = new Scanner (System.in); int i;
String str;
System.out.println ("Enter a word: "); str = sc.next();

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:

1. (i) Count the number of vowels and display them without


repeating.
2. (ii) Change the case of each letter in the string.
3. (iii) Change all the ‘e’ with ‘*’.

PROGRAM:

import java.util.*; public class Program47 {

public static void main (String args[]) {


Scanner sc = new Scanner (System.in); int i;
String str, str1 = "", str2 = "";
char ch;

int c[] = new int[5];


char v[] = {'a','e','i','o','u'};
for (i = 0; i < 5; i++)
c[i] = 0;
System.out.println ("Enter 3 words: "); str = sc.nextLine();
String vstr = str.toLowerCase();
for (i = 0; i < str.length(); i++)
{

ch = vstr.charAt(i); if (ch == v[0])

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:

import java.util.*; public class Program48 {

public static void main (String args[]) {


Scanner sc = new Scanner (System.in); int i, j, l, r1 = 0, r2 = 0;
String s1, s2 = "", word = "";
char ch1, ch2;

System.out.println ("Enter a string: "); s1 = sc.nextLine();


s1 = s1.toUpperCase();
s1 = s1 + " ";

l = s1.length();
for (i = 0; i < l; i++) {

113

ch1 = s1.charAt(i); if (ch1 == ' ')


{

for (j = 0; j < word.length() - 1; j++) {

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

word = word + ch1; }

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:

import java.util.*; public class Program49 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, wcount = 0;
String str, str1 = "", str2 = "", word = "", word2 = "";
char ch1, ch2;
System.out.println ("Enter a sentence with atleast three words: "); str =
sc.nextLine();
str = " " + str;
for (i = 0; i < str.length(); i++)
{

if (str.charAt(i) == ' ') wcount++;

115

}
if (wcount < 3)
System.out.println ("Less than three words"); else
{
for (i = 0; i < str.length(); i++) {

ch1 = str.charAt(i); if (ch1 == ' ')


{

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++) {

ch1 = str1.charAt(i); if (ch1 == ' ')


{

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:

import java.util.*; public class Program50 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int i;


String str, str2 = "";
char ch, ch2;

System.out.println ("Enter a sentence: "); str = sc.nextLine();


str = " " + str;
str = str.toUpperCase();

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

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
{

ch2 = (char) ((int) ch - 1);


if (ch2 == 'A' || ch2 == 'E' || ch2 == 'I' || ch2 == 'O' || ch2 == 'U') ch2 =
(char) ((int)ch + 1);
str2 = str2 + ch2;

}}

System.out.println (str2.trim()); }

OUTPUT:

119

PROGRAM 51
Execute a program to accept a sentence and perform the following
tasks:

1. (i) Display each word in opposite order.


2. (ii) Check whether the word is a palindrome or not.
3. (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.

PROGRAM:

import java.util.*;
public class Program51
{public static void main (String args[])

{Scanner sc = new Scanner (System.in); int i, j;

String s1, word1 = "", word2 = "", s2 = "", palin = "", firstl = "", lastl = "";
char ch1, ch2;
System.out.println ("Enter a string");
s1 = sc.nextLine();

s1 = s1.toUpperCase() + " "; for (i = 0; i < s1.length(); i++) {

ch1 = s1.charAt(i);

120

if (ch1 == ' ') {

for (j = word1.length() - 1; j >= 0; j--) {

ch2 = word1.charAt(j);

word2 = word2 + ch2; }

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

import java.util.*; public class Program52 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, k, ch;
System.out.println ("Enter 1 or 2 to display a pattern: "); ch = sc.nextInt();

switch (ch) {

case 1:
k = 65;
for (i = 1; i <= 5; i++) {

for (j = 5; j >= 1; j--)


PROGRAM:

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++) {

for (j = 1; j <= 5; j++) {

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:

import java.util.*; public class Program53 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int arr[] = new int [10];
int i, num, k = 0;
System.out.println ("Enter 10 numbers: "); for (i = 0; i < 10; i++)

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:

import java.util.*; public class Program54 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, min, t;
System.out.println ("Enter the number of elements: "); int n = sc.nextInt();

int arr[] = new int[n];


System.out.println ("Enter the elements: "); for (i = 0; i < n; i++)
arr[i] = sc.nextInt();
for (i = 0; i < n-1; i++)
{

min = i;
for (j = i+1; j < n; j++) {

if (arr[j] < arr[min])

min = j; }

t = arr[i];
arr[i] = arr[min];

127

arr[min] = t; }

System.out.println ("The sorted array: "); for (i = 0; i < n; i++)


System.out.print (arr[i] + " ");

}}

OUTPUT:

128

PROGRAM 55
Execute a program to sort an array using bubble sort technique.

PROGRAM:
import java.util.*; public class Program55 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, t;
System.out.println ("Enter the number of elements: "); int n = sc.nextInt();

int arr[] = new int[n];


System.out.println ("Enter the elements: "); for (i = 0; i < n; i++)
arr[i] = sc.nextInt();
for (i = 0; i < n-1; i++)
{

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

if (arr[j] > arr[j+1]) {

t = arr[j];
arr[j] = arr[j+1]; arr[j+1] = t;

129

}}

System.out.println ("The sorted array is: "); for (i = 0; i < n; i++)


System.out.print (arr[i] + " ");

}}

OUTPUT:
130

PROGRAM 56
Execute a program to accept numbers in two different arrays. Then
merge both the arrays and print the result.

PROGRAM:

import java.util.*; public class Program56 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, ml, nl;
System.out.println ("Enter the number of elements in array 1 and 2"); ml =
sc.nextInt();
nl = sc.nextInt();
int m[] = new int [ml];
int n[] = new int [nl];
int p[] = new int [ml+nl];
System.out.println ("Enter the elements of first array");
for (i = 0; i < ml; i++)
m[i] = sc.nextInt();
System.out.println ("Enter the elements of second array");
for (i = 0; i < nl; i++)
n[i] = sc.nextInt();
for (i = 0; i < ml; i++)
p[i] = m[i];
for (i = 0; i < nl; i++)

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:

import java.util.*; public class Program57 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, el, pos;
System.out.println ("Enter the number of array elements: ");
int n = sc.nextInt();
int arr[] = new int[n + 1];
System.out.println ("Enter the elements: ");
for (i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println ("Enter the element to be inserted and its position: "); el
= sc.nextInt();
pos = sc.nextInt();
for (i = n-1; i >= pos; i--)
arr[i+1] = arr[i];
arr[pos] = el;
System.out.println ("The new array after insertion is: ");
for (i = 0; i <= n; i++)
System.out.print (arr[i] + " ");

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:

import java.util.*; public class Program58 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, pos;
System.out.println ("Enter the number of elements: "); int n = sc.nextInt();

int arr[] = new int[n];


System.out.println ("Enter the elements: ");
for (i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println ("Enter the position to be deleted: "); pos = sc.nextInt();
for (i = pos+1; i < n; i++)
arr[i-1] = arr[i];
System.out.println ("The new array after deletion is: "); for (i = 0; i < n-1; i+
+)
System.out.print (arr[i] + " ");

}}

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:

import java.util.*; public class Program59 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int i, j;


String name[] = new String[15];
String t;
System.out.println ("Enter 15 names: "); for (i = 0; i < 15; i++)
name[i] = sc.next();
for (i = 0; i < 14; i++)

{
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++)
{

System.out.print (name[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!”

Input: Country Name India Output: India Taj Mahal

PROGRAM:

import java.util.*; public class Program60 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);

int i, pos = 0, k = 0;

String country;

String wonders[] = {"Chichen Itza", "Christ the Redeemer", "Taj Mahal",


"Great Wall of China", "Machu Picchu", "Petra", "Colosseum"};

String countries[] = {"Mexico", "Brazil", "India", "China", "Peru", "Jordan",


"Italy"}; System.out.println ("Enter the country's name: ");
country = sc.next();
for (i = 0; i < 7; i++)

{
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.

Sample Input: 1 2 3 4 123 4 123 4 123 4

Sum of Left Diagonal= 10 Sum of Right Diagonal= 10

PROGRAM:

import java.util.*; public class Program61 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int n, i, j, lsum = 0, rsum = 0;
System.out.println ("Enter the number of rows and columns: "); n =
sc.nextInt();
int arr[][] = new int[n][n];
System.out.println ("Enter the elements: ");
for (i = 0; i < n; i++)
{

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

arr[i][j] = sc.nextInt(); }

141

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


for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

}
for (i = 0; i < n; i++) lsum = lsum + arr[i][i]; j = 0;
for (i = n-1; i >= 0; i--) {

rsum = rsum + arr[i][j];

j++; }

System.out.println ("Sum of left diagonal: " + lsum);

System.out.println ("Sum of right diagonal: " +rsum); }}

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.

Sample Input: 1 2 3 4 5 1234 5 1234 5 1234 5

Sum of Outer Elements: 42 Sum of Inner Elements: 18 Difference:


42-18=24
PROGRAM:

import java.util.*; public class Program62 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, m, n, osum = 0, isum = 0;
System.out.println ("Enter the number of rows and columns: "); m =
sc.nextInt();
n = sc.nextInt();
int arr[][] = new int[m][n];
System.out.println ("Enter the elements: ");
for (i = 0; i < m; i++)
{

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

143

arr[i][j] = sc.nextInt(); }

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

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

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

for (j = 0; j < n; j++) {

if (i == 0 || j == 0 || i == m-1 || j == n-1) osum = osum + arr[i][j];


else
isum = isum + arr[i][j];

}}

System.out.println ("Sum of outer elements: " + osum); System.out.println


("Sum of inner elements: " + isum); if (osum > isum)
System.out.println ("Difference: " + (osum-isum));

else

System.out.println ("Difference: " + (isum-osum)); }

}
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:

import java.util.*; public class Program63 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, m, n;
System.out.println ("Enter the number of rows and columns: "); m =
sc.nextInt();
n = sc.nextInt();
int arr[][] = new int[m][n];
System.out.println ("Enter the elements: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)

arr[i][j] = sc.nextInt(); }

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

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println ();

146

}
System.out.println ("Array after removing the corner elements: "); for (i = 0; i
< m; i++)
{

for (j = 0; j < n; j++) {

if ((i == 0) && (j == 0 || j == n-1)) System.out.print (" ");


else if ((i == m-1) && (j == 0 || j == n-1)) System.out.print (" ");
else
System.out.print (arr[i][j] + " ");

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.

Sample Input: 1 2 3 4 234 5 345 6 456 7

Sample Output: 4 5 6 7 123 4 234 5 345 6

PROGRAM:

import java.util.*; public class Program64 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, m, n;
System.out.println ("Enter the number of rows and columns: "); m =
sc.nextInt();
n = sc.nextInt();
int arr[][] = new int[m][n];
System.out.println ("Enter the elements: ");
for (i = 0; i < m; i++)
{

148

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

arr[i][j] = sc.nextInt(); }

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

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

}
int t[] = new int[n];
for (i = 0; i < n; i++) t[i] = arr[m-1][i];
for (i = m-1; i >= 1; i--) {

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


arr[i][j] = arr[i-1][j]; }

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


arr[0][i] = t[i];
System.out.println ("The new array is: "); for (i = 0; i < m; i++)
{

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

}}

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:

import java.util.*; public class Program65 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, m, n, rsum = 0, csum = 0;
System.out.println ("Enter the number of rows and columns: "); m =
sc.nextInt();
n = sc.nextInt();
int arr[][] = new int[m][n];
System.out.println ("Enter the elements: ");
for (i = 0; i < m; i++)
{

151

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

arr[i][j] = sc.nextInt(); }

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

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

}
System.out.println ("The new array is: "); for (i = 0; i < m; i++)
{

for (j = 0; j < n; j++) {

System.out.print (arr[i][j] + " ");

rsum = rsum + arr[i][j]; }

System.out.print (rsum); rsum = 0; System.out.println();

}
for (j = 0; j < n; j++) {
for (i = 0; i < m; i++) {

csum = csum + arr[i][j]; }

System.out.print (csum + " ");

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.

Sample Input : 1 2 5 6 347 8

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

PROGRAM:

import java.util.*; public class Program66 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i , j, n;
System.out.println ("Enter the number of rows and columns: ");

154

n = sc.nextInt();
int arr[][] = new int [n][n]; System.out.println ("Enter the elements: "); for (i
= 0; i < n; i++)
{

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

arr[i][j] = sc.nextInt(); }

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

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

}
System.out.println ("The elements below the left diagonal are: "); for (i = 0; i
< n; i++)
{

for (j = 0; j < n; j++) {

if (i > j)

System.out.print (arr[i][j] + " "); }

System.out.println(); }
System.out.println ("The elements below the right diagonal are: "); for (i = 0;
i < n; i++)
{

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

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:

import java.util.*; public class Program67 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, m, n;
System.out.println ("Enter the number of rows and columns: "); m =
sc.nextInt();
n = sc.nextInt();
int arr[][] = new int[m][n];
System.out.println ("Enter the elements: ");
for (i = 0; i < m; i++)
{

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

arr[i][j] = sc.nextInt(); }

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

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++)
{

for (j = n-1; j >= 0; j--) System.out.print (arr[i][j] + " "); System.out.println();

}}

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:

import java.util.*; public class Program68 {

public int prime (int p) {

int c = 0, a, b;
for (a = p+1;; a++) {

for (b = 2; b < a; b++) {

if (a % b == 0)

c++; }

if (c == 0) break;
c = 0;

return a; }
public static void main (String args[]) {

Scanner sc = new Scanner (System.in);

159

Program68 ob = new Program68();


int i, j, m, n, f;
System.out.println ("Enter the number of rows and columns: "); m =
sc.nextInt();
n = sc.nextInt();
int arr[][] = new int[m][n];
System.out.println ("Enter the elements: ");
for (i = 0; i < m; i++)
{

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

arr[i][j] = sc.nextInt(); }

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

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

}
System.out.println ("The new array is: "); for (i = 0; i < m; i++)
{

for (j = 0; j < n; j++) {

f = ob.prime(arr[i][j]);

System.out.print (f + " "); }

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

VI L XII XXXIII XIX XX

PROGRAM:

import java.util.*; public class Program69 {

public String roman (int num) {

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--)
{

while (num >= rval[a]) {


num = num - rval[a];

162

r = r + rnum[a]; }

return r; }

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, m, n;
Program69 ob = new Program69();
System.out.println ("Enter the number of rows and columns: "); m =
sc.nextInt();

n = sc.nextInt();
int arr[][] = new int[m][n]; System.out.println ("Enter the elements: "); for (i
= 0; i < m; i++)
{

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

arr[i][j] = sc.nextInt(); }

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

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

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

163

for (j = 0; j < n; j++) {

System.out.print (ob.roman(arr[i][j]) + " "); }

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:

import java.util.*; public class Program70 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, n, ldsum = 0, rdsum = 0;
boolean m1 = false, m2 = false;
System.out.println ("Enter the number of rows and columns: "); n =
sc.nextInt();
int arr[][] = new int[n][n]; int rsum[] = new int[n]; int csum[] = new int[n];
for (i = 0; i < n; i++)

165

rsum[i] = 0;

csum[i] = 0; }

System.out.println ("Enter the elements: "); for (i = 0; i < n; i++)


{

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

arr[i][j] = sc.nextInt(); }

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

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

}
for (i = 0; i < n; i++) // finding the sum of each row {

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

rsum[i] = rsum[i] + arr[i][j]; }

for (j = 0; j < n; j++) // finding the sum of each column {

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

csum[i] = csum[i] + arr[i][j]; }

for (i = 0; i < n; i++) // finding the sum of left diagonal ldsum = ldsum +
arr[i][i];
j = n-1;

166

for (i = 0; i < n; i++) // finding the sum of right diagonal {

rdsum = rdsum + arr[i][j];

j--; }
for (i = 0; i < n-1; i++) {

if (rsum[i] == rsum[i+1]) m1 = true;


else
{

m1 = false;

break; }

}
if (m1 == true) {

for (i = 0; i < n-1; i++) {

if (csum[i] == rsum[i] && rsum [i] == ldsum && rsum[i] == rdsum) m2 =


true;
else
{

m2 = false;

break; }

}
if (m2 == true)

167

System.out.println ("It is a magic square"); else


System.out.println ("It is not a magic square");

}
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:

1. Display the input matrix.

2. Find the minimum and maximum value in the matrix and display
them along with their position.

3. Sort the elements of the matrix in ascending order using any


standard sorting technique and rearrange them in the matrix.

PROGRAM:

import java.util.*;
public class Program71
{public static void main (String args[])

{Scanner sc = new Scanner (System.in);


int i, j, m, n, rmin = 0, cmin = 0, rmax = 0, cmax = 0, max ,min, t, k;
System.out.println ("Enter the number of rows and columns: ");
m = sc.nextInt();
n = sc.nextInt();
int arr[][] = new int[m][n];
System.out.println ("Enter the elements: ");
for (i = 0; i < m; i++)
{

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

arr[i][j] = sc.nextInt(); }

System.out.println ("The orginal array: "); for (i = 0; i < m; i++)

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++) {

for (j = 0; j < n; j++) {

if (arr[i][j] > max) {

max = arr[i][j]; rmax = i; cmax = j;

}
if (arr[i][j] < min) {

min = arr[i][j]; rmin = i;


cmin = j;

}}

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

System.out.println ("The sorted array is: "); for (i = 0; i < m; i++)


{
for (j = 0; j < n-1; j++) {

for (k = 0; k < n-j-1; k++) {

if (arr[i][k] > arr[i][k+1]) {

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:

import java.util.*; public class Program72 {


public int binary(int x[], int lb, int ub, int el, int f) {

int m = (lb+ub)/2; if (lb > ub || f == 1) return f;


else
{

if (el == x[m]) f = 1;
if (el < x[m]) ub = m-1;

if (el > x[m])


lb = m+1;
return(binary(x, lb, ub, el, f));

}}

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);

172

int i, num, l = 0, u, n, flag = 0, check;


Program72 ob = new Program72(); System.out.println ("Enter the size of the
array: "); n = sc.nextInt();

int arr[] = new int[n];


u = n-1;
System.out.println ("Enter the elements: ");
for (i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println ("Enter the number to be searched: "); num =
sc.nextInt();
check = ob.binary(arr, l, u, num, flag);
if (check == 1)
System.out.println ("The element is present");
else
System.out.println ("The element is not present");

}}

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:

import java.util.*; public class Program73 {public int sum (int n)

{
int d, s = 0; while (n != 0) {

d = n % 10; s = s + d;
n = n / 10;

return (s); }

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); Program73 ob = new Program73 ();


int num, f = 0, i, j, dsum = 0, fpsum = 0; System.out.println ("Enter a
number: ");
174

num = sc.nextInt(); dsum = ob.sum(num); for (i = 2; i < num; i++) {

if (num % i == 0) {

for (j = 2; j < i; j++) {

if (i % j == 0)

f++; }

if (f == 0) {

fpsum = fpsum + ob.sum(i); }

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[]) {

Scanner sc = new Scanner (System.in); Program74 ob = new Program74();


int i, num, cprime, f = 0, dnum = 0, n, j; System.out.println ("Enter a
number: "); num = sc.nextInt();

cprime = num; n = num; while (n != 0) {

dnum++;

// finding the number of digits

176

n = n / 10; }

for (i = 1; i <= dnum; i++) {

cprime = ob.circular(cprime, dnum); {

for (j = 2; j < cprime; j++) {

if (cprime % j == 0)

f++; }

if (f != 0) {

System.out.println ("It is not a circular prime");

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.

Sample Input: 674 Sample Output:

500 x1 100 x1 50x1 20x1

1 x4

PROGRAM:

import java.util.*; public class Program75 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int note[] = {1000, 500, 200, 100, 50, 20, 10, 5, 1}; int noteval[] = {0, 0, 0,
0, 0, 0, 0, 0, 0};
int amt, i;
System.out.println ("Enter the amount of money"); amt = sc.nextInt();
for (i = 0; i < note.length; i++)
{

while (amt >= note[i]) {

178

amt = amt - note[i];


noteval[i]++; }

}
for (i = 0; i < note.length; i++) {

if (noteval[i] != 0)

System.out.println (note[i] + " x " + noteval[i]); }

}}

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:

1. (i) Display original matrix


2. (ii) Rotate the matrix 90 degree clockwise.
3. (iii) Find the sum of corner elements of the matrix.

Sample Input:
34 9
25 8
16 7
Sample output:
12 3
65 4
78 9
Sum of corner elements=20

PROGRAM:

import java.util.*; public class Program76 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, s = 0, m;
System.out.println ("Enter the number of rows and columns of the square
matrix: "); m = sc.nextInt();

180

if (m <= 2 || m >= 10) {

System.out.println ("Invalid input");

System.exit(0); }

int arr[][] = new int[m][m]; System.out.println ("Enter the elements: "); for (i
= 0; i < m; i++)
{

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

arr[i][j] = sc.nextInt(); }

System.out.println ("The original array: "); for (i = 0; i < m; i++)


{

for (j = 0; j < m; j++) System.out.print (arr[i][j] + " "); System.out.println();

}
System.out.println ("The matrix rotated 90 degrees clockwise: "); for (i = 0; i
< m; i++)
{

for (j = m - 1; j >= 0; j--) {

System.out.print (arr[j][i] + " ");


if ((i == 0 || i == m-1) && (j == 0 || j == m-1)) s = s + arr[i][j];

}
181

System.out.println(); }

System.out.println ("The sum of corner elements: " + s); }

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:

(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

Words Intelligence Plus Character Is Education

PROGRAM:

import java.util.*; public class Program77 {

Vowels Consonants 5 7
13
36

1154
public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int i, j, vc = 0, cc = 0;


String str, str2 = "", word = "";
char ch;

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

183

str = sc.nextLine();
if (str.endsWith(".") == false && str.endsWith("?") == false) {

System.out.println("Invalid input. Does not end with \'.\' or \'?\'");

System.exit(0); }

str = " " + str;


for (i = 0; i < str.length() - 1; i++) {

ch = str.charAt(i); if (ch == ' ')


{

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++)
{

ch = str2.charAt(i); if (ch == ' ')

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;

boolean flag = true;


String num = Integer.toString(n); for (a = 0; a < num.length(); a++) {

char c = num.charAt(a);
for (b = a+1; b < num.length(); b++) {

if (c == num.charAt(b)) {

flag = false; break;

186

}}} return flag;

}
public static void main (String args[]) {

Scanner sc = new Scanner (System.in); Program78 ob = new Program78();


int i, n1, n2, c = 0;
boolean rpt;

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)
{

System.out.print (i + ", ");

c++; }}

System.out.println();

System.out.println ("Number of unique numbers: " + c); }}

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:

import java.util.*; public class Program79 {

public char replace13(char c) {

char newc;
int cnum = (int) c;
if (cnum >= 65 && cnum <= 90) {

if (cnum + 13 <= 90)


newc = (char) (cnum + 13);
else
newc = (char) (64 + (cnum - 77));

}
else if (cnum >= 97 && cnum <= 122) {

if (cnum + 13 <= 122) newc = (char) (cnum + 13); else

188

newc = (char) (96 + (cnum - 109)); }

else
newc = c; return newc;
}
public static void main (String args[]) {

Scanner sc = new Scanner (System.in); Program79 ob = new Program79();


int i;
char ch;

System.out.println ("Enter a sentence: "); String str = sc.nextLine();


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

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:

import java.util.*; public class Program80 {

public static void main (String args[])

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++) {

for (j = 0; j < str.length(); j++) {

ch1 = str.charAt(j); if ((int) ch1 == 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

Print the following pattern:

PROGRAM 81
PROGRAM:

public class Program81 {

public static void main (String args[]) {

int i, j, t = 4;
for (i = 1; i <= 9; i = i + 2) {

for (j = 1; j <= t; j++) System.out.print("\t"); for (j = 1; j <= i; j++) {

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) {

for (j = 1; j <= t; j++) System.out.print("\t"); for (j = 1; j <= i; j++) {

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:

import java.util.*; public class Program82 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int n, i, j, maxl = 0;
System.out.println ("Enter the number of teams: "); n = sc.nextInt();
195

if (n <= 2 || n >= 9) {

System.out.println ("Invalid input");

System.exit(0); }

String team[] = new String [n];


int l[] = new int [n];
System.out.println ("Enter the team names one by one: "); sc.nextLine();
for (i = 0; i < n; i++)
{

team[i] = sc.nextLine();

l[i] = team[i].length(); }

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

if (l[i] > maxl)

maxl = l[i]; }

for (i = 0; i < maxl; i++) {

for (j = 0; j < n; j++) {

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:

import java.util.*; public class Program83 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int i, j;


String word1, word2, word3 = "";
char ch1, ch2;

System.out.println ("Enter two words: ");


word1 = sc.next();
word2 = sc.next();
if (word1.length() != word2.length()) System.out.println ("They are not
anagram words"); else

{
for (i = 0; i < word1.length(); i++) {
198

ch1 = word1.charAt(i);
for (j = 0; j < word2.length(); j++) {

ch2 = word2.charAt(j); if (ch1 == ch2)


{

word3 = word3 + ch1;

break; }

}}

if (word3.length() == word1.length()) System.out.println ("They are anagram


words"); else
System.out.println ("They are not anagram words"); }

}}

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:

1. Display the input matrix


2. Find the maximum and minimum value in the matrix and
display them along with
their position

3. Sort the elements of the matrix in ascending order using any


standard sorting

technique and rearrange them in the matrix.

Display the rearranged matrix. Sample Input:


M=3 N=4

Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-4. Output:


Original Matrix:
879 3

-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:

import java.util.*; public class Program84 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in);


int i, j, m, n, rmin = 0, cmin = 0, rmax = 0, cmax = 0, max ,min, t, k = 0;
System.out.println ("Enter the number of rows and columns: ");
m = sc.nextInt();
n = sc.nextInt();
int arr[][] = new int[m][n];
int sda[] = new int [m*n];
System.out.println ("Enter the elements: ");
for (i = 0; i < m; i++)
{

for (j = 0; j < n; j++) {


arr[i][j] = sc.nextInt(); sda[k] = arr[i][j]; k++;

}}

System.out.println ("The orginal array: "); for (i = 0; i < m; i++)


{

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " ");

201

System.out.println(); }

max = arr[0][0];
min = arr[0][0];
for (i = 0; i < m; i++) {

for (j = 0; j < n; j++) {

if (arr[i][j] > max) {

max = arr[i][j]; rmax = i; cmax = j;

}
if (arr[i][j] < min) {

min = arr[i][j]; rmin = i;


cmin = j;

}}

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));

for (i = 0; i < (m*n) - 1; i++) {

for (j = 0; j < (m*n) - 1 - i; j++)

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++) {

for (j = 0; j < n; j++) {

arr[i][j] = sda[k];

k++; }

}
System.out.println ("The sorted array is: "); for (i = 0; i < m; i++)
{

for (j = 0; j < n; j++) System.out.print (arr[i][j] + " "); System.out.println();

}}

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.

Sample Input: 25143


Sample Output: It is a bouncy number.

PROGRAM:

import java.util.*; public class Program85 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int num, n, prev, d;


boolean inc = true, dec = true; System.out.println ("Enter a number: "); num
= sc.nextInt();
if (num < 100) {

System.out.println ("It is not a bouncy number");

System.exit(0); }

n = num;
prev = n % 10; while (n != 0) {

205

d = n % 10; if (d > prev) {

inc = false;

break; }

prev = d;

n = n / 10; }

n = num;
prev = n % 10; while (n != 0) {

d = n % 10; if (d < prev) {

dec = false;

break; }

prev = d;

n = n / 10; }

if (inc == false && dec == false) System.out.println ("It is a bouncy


number"); else
System.out.println ("It is not a bouncy number");

}}

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:

import java.util.*; public class Program86 {

public static void main (String args[]) {

Scanner sc = new Scanner (System.in); int n, i, flag = 0;


String code;
char ch, ch2;

System.out.println ("Enter the length of the code and the code: "); n =
sc.nextInt();
if (n > 6)
{

System.out.println ("Invalid! Length of string should not exceed 6


characters!");

System.exit(0); }

code = sc.next();
for (i = 0; i < code.length(); i++) {

ch = code.charAt(i);
if (Character.isUpperCase(ch) == false) {

System.out.println ("Invalid! Only uppercase letters permitted"); flag = 1;


System.exit(0);

}}

209

for (i = 0; i < code.length() - 1; i++) {

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"); }

You might also like