0% found this document useful (0 votes)
44 views269 pages

Ashutosh Behera - 12 B - IsC Computer Science Practical File

This document provides an index of 65 computer programs written by a student named Ashutosh Behera in Grade 12. The programs cover a range of topics including calculating triangle types, discounts, taxes, velocities, patterns, sorting arrays, matrix operations, and more. Each program is assigned a page number and includes a brief description. The index is intended to document the programs completed by the student for a computer practical course.

Uploaded by

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

Ashutosh Behera - 12 B - IsC Computer Science Practical File

This document provides an index of 65 computer programs written by a student named Ashutosh Behera in Grade 12. The programs cover a range of topics including calculating triangle types, discounts, taxes, velocities, patterns, sorting arrays, matrix operations, and more. Each program is assigned a page number and includes a brief description. The index is intended to document the programs completed by the student for a computer practical course.

Uploaded by

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

Department of Computer Science

Computer Practical Records

Name: Ashutosh Behera


Grade: XII
Section: B
INDEX
Program Page Teacher’s
Program Description
Number number signature
Input three sides of triangle and if
Program 1 triangle is possible then display type 7
of triangle or display not possible.
Compute and print the following
Program 2 festival discount and assured gifts for 9
amount of purchase in a clock
Compute and print the stream for
Program 3 admission in grade 11 based on the 12
marks in tenth.
Input consumer name, number and
Program 4 units consumed and calculate 15
amount to be paid according to
given tariff.
Compute and print the income tax to
be paid based on given table and if
Program 5 18
gender is female or age is 65 then
display wrong category.
For a courier, compute and print the
Program 6 cost of mail in ordinary and express 21
type according to given tariff.
Execute a program to compute and
Program 7 print the fine for returning books late 24
to the library by n days.
Execute menu driven program to find
Program 8 26
area of circle square and rectangle.
Menu driven program to find volume
Program 9 29
of cube, cuboid or sphere.
Menu driven program to find
Program 10 depreciated value of a car based on 32
number of years it has been used.
Calculate relative velocity of two
Program 11 trains and time taken to cross each 35
other by a menu driven program.
Menu-driven program to calculate
Program 12 the discounted price with name and 38
address.

1|Page
Menu-driven program to find area of
Program 13 equilateral isosceles or scalene 42
triangles.
Menu driven program to calculate
gross salary net salary and check
Program 14 45
whether an employee has to pay
Income Tax or not.
Menu Driven program for finding
Program 15 discounted price based on type of 49
room and number of days of stay.
Menu Driven program to perform
Program 16 arithmetic operation on numbers 53
input by user.
Program to calculate the sum of
positive even numbers and negative
Program 17 56
odd numbers and accept 0 to quit
and display the result.
Accept a number and print the
Program 18 58
factorial using do while loop.
Check whether a number input by
Program 19 60
the user is even or odd.
Accept a number and print the odd
Program 20 62
numbers below it.

Program 21 Print the given pattern. 64

Find whether a number input by the


Program 22 66
user is a perfect number.
Find GCD of two numbers input by
Program 23 68
the user.
Find whether a number input by the
Program 24 70
user is a neon number.
Find whether a number input by the
Program 25 72
user is an Armstrong number
Find whether a number input by the
Program 26 74
user is a palindrome number.
Find the maximum and minimum
Program 27 numbers from 10 different numbers 76
input by the user.
To display n number of terms of
Program 28 78
Fibonacci series.
To input a number and check
Program 29 80
whether it is a Niven number or not.

2|Page
To input and check whether a
Program 30 82
number is happy number or not.
To input and check whether a
Program 31 number is prime number or not and 84
display 10 prime number after it.
To print given patterns using switch
Program 32 88
case.
To print given pattern using switch
Program 33 91
case.
To print given pattern using switch
Program 34 95
case.
To print given pattern using switch
Program 35 98
case.
To print given pattern using switch
Program 36 102
case.
To input a number and check
Program 37 whether it is an automorphic number 106
or not.
To check whether the input code is a
Program 38 108
legal ISBN code.
To print the sum of pattern using
Program 39 111
switch case.
To print the sum of pattern using
Program 40 114
switch case.
To print the sum of pattern using
Program 41 117
switch case.
To convert a decimal number to
Program 42 binary number and vice-versa using 120
switch case.
To input a number and print a
pattern of consecutive natural
Program 43 123
numbers which add up to the
number itself.
To input a number between 1 to 5
Program 44 and display the required patterns 126
and number of patterns.
To round off a given number to
Program 45 certain number of digits as per user’s 129
choice.
To accept a word from the user and
Program 46 131
print the given pattern.

3|Page
Accept three words and perform the
Program 47 133
given tasks.
Accept a sentence and count and
Program 48 remove the words with repeating 137
letters.
To accept a sentence and pint the
Program 49 new sentence after performing the 140
given tasks.
To accept a sentence and change
Program 50 each consonant to its previous letter 143
provided that letter is not a vowel.
To accept a sentence and perform
Program 51 146
the given tasks.
To accept a word and print the
Program 52 151
patterns using switch case.
To accept 10 numbers in a single
Program 53 dimensional array and find a number 154
and print appropriate message.
To sort an array using selection sort
Program 54 156
technique.
To sort an array using bubble sort
Program 55 159
technique.
To merge two single dimensional
Program 56 162
arrays.
Program 57 To insert an element into an array. 165
To delete an element from the given
Program 58 array according to user input 167
position.
To accept 15 names and display the
names that start with the letter
Program 59 169
accepted and arrange given array in
alphabetical order.
To accept the seven wonders and
their respective countries in two
Program 60 different arrays and search for the 172
country in the array and display the
respective wonder.
To accept numbers in DDA and find
Program 61 174
sum of left and right diagonal.
To accept numbers in DDA and find
Program 62 177
sum of outer and inner elements.

4|Page
To accept the elements DDA and
Program 63 181
remove corner elements.
To accept elements of a matrix and
Program 64 arrange the rows of the array as per 184
in the question.
To accept the elements of a matrix
and print the sum of each column
Program 65 187
and row along with the elements of
that row and column.
To accept the elements of a matrix
and print the elements which are
Program 66 191
below the left and right diagonals of
the matrix.
To accept the elements of the matrix
Program 67 195
and print its mirror image.
To accept the elements of a matrix
and print the matrix after shifting
Program 68 198
each element to the next closest
prime number.
To accept integer numbers in a
Program 69 matrix and convert all elements into 203
roman numerals.
To accept elements of a matrix and
Program 70 206
check if it is a magic square.
To accept elements of matrix and
Program 71 display maximum and minimum 210
numbers. Then, sort the array.
Accept elements of single
dimensional array and search for
Program 72 number using binary search 215
technique. Perform this using
recursive function.
Accept a number and check if it is a
Program 73 218
smith number or not.
Accept a number and check if it is
Program 74 222
circular prime or not.
To accept an amount from user and
Program 75 print denomination in terms of rupee 226
notes.
Accept elements of a matrix, rotate it
Program 76 90 degrees clockwise, and find the 228
sum of corner elements.

5|Page
Accept a string and check its validity
Program 77 and then print number of vowels and 232
consonants in the given format.
To accept lower and upper limits of
Program 78 numbers and print those numbers 237
which have no repetition of digits.

Program 79 To use Caeser Cipher rotation. 240

To accept a string and perform the


Program 80 243
given tasks

Program 81 Print the pattern. 246

Print the names of teams in vertical


Program 82 249
manner
To check if words entered are
Program 83 253
anagram.
To accept the elements of array and
Program 84 256
perform the given tasks
To check whether a number is
Program 85 261
bouncy
To check the validity of the code
Program 86 264
input.

6|Page
PROGRAMS
1. Program 1
Execute a program to input three sides of a triangle and check whether the triangle is possible
or not. If possible, then display whether the triangle is scalene, isosceles, or equilateral.
Otherwise, display the triangle is not possible.

 Code:

import java.util.*;

public class Prog_1

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

double a, b, c;

System.out.println("Enter first side:");

a = sc.nextDouble();

System.out.println("Enter second side:");

b = sc.nextDouble();

System.out.println("Enter third side:");

c = sc.nextDouble();

if(a+b>c && b+c>a && a+c>b)

7|Page
{

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

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

System.out.println("Triangle is an equilateral triangle.");

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

System.out.println("Triangle is an isosceles triangle.");

else

System.out.println("Triangle is a scalene triangle.");

else

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

 Output:

8|Page
2. Program 2
A clock showroom has announced the following festival discount and assured gifts on the
purchase of items on the basis of cost of purchase. Execute a program to input total amount
of purchase and print the assured gift, discount, and final bill to be paid.

Total cost Discount Assured Gift


Less than or equal to Rs.2000 5% Wall Clock
From Rs.2001 to Rs.5000 10% School Bag
From Rs.5001 to Rs.10000 15% Electric Iron
More than Rs.10000 20% Wrist Watch

 Code:

import java.util.*;

public class Prog_2

9|Page
public static void main(String args[ ])

Scanner sc = new Scanner(System.in);

double Disc= 0, Amt= 0, TP= 0, DP= 0;

String Gift = "";

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

TP = sc.nextDouble();

if(TP<=2000)

Disc = 5.0;

Gift = "Wall Clock.";

if(TP>2000 && TP<=5000)

Disc = 10.0;

Gift = "School Bag.";

if(TP>5000 && TP<=10000)

Disc = 15.0;

Gift = "Electric Iron.";

}
10 | P a g e
if(TP>10000)

Disc = 20.0;

Gift = "Wrist Watch.";

DP = TP * Disc/100.0;

Amt= TP - DP;

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

System.out.println("The amount to be paid is: " +Amt);

System.out.println("Your gift is a: " +Gift);

 Output:

11 | P a g e
12 | P a g e
3. Program 3
An ICSE school displays a notice regarding admission for grade 11 for choosing different
stream according to marks obtained in English, Math, and Science in ICSE exam. Print the
appropriate stream allotted to the candidate. Execute a program to accept marks from the
user. Calculate the average and then print the stream for the candidate.

Marks Stream

If average of marks of English, math and Science is


Pure Science
greater than or equal to 80%

If average of marks of English and Science is greater


than or equal to 80% and the mark of math is greater Bio Science
than or equal to 60%

If average of marks of English, math and Science is less


Commerce
than or equal to 60%

 Code:

import java.util.*;

public class Prog_3

public static void main(String args[])

Scanner sc = new Scanner(System.in);

double eng, maths, sci, avg1 = 0.0, avg2 = 0.0;

System.out.println("Enter marks in English: ");

13 | P a g e
eng = sc.nextDouble();

System.out.println("Enter marks in Maths: ");

maths = sc.nextDouble();

System.out.println("Enter marks in Science: ");

sci = sc.nextDouble();

avg1 = (eng + maths + sci)/3.0;

avg2 = (eng + sci)/2.0;

if (avg1 >= 80.0)

System.out.println("Your alloted stream is Pure Science.");

else if (avg2 >= 80.0 && maths >= 60.0)

System.out.println("Your alloted stream is Bio Science.");

else if (avg1 <= 60.0)

System.out.println("Your alloted stream is Commerce.");

14 | P a g e
 Output:

15 | P a g e
4. Program 4
The electricity board charges from their consumers according to the units consumed. Execute
a program to input consumer name, consumer number and units consumed. Calculate and
print the amount to be paid according to units consumed.
The amount to be paid is as per the tariff given below:

Units Consumed Charges

Up to 100 units Rs.5.50 per unit

For next 200 units Rs.6.50 per unit

For next 300 units Rs.7.50 per unit

More than 600 units Rs.8.50 per unit

 Code:

import java.util.*;

public class Prog_4

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter Consumer Name: ");

String name = sc.nextLine();

System.out.println("Enter Consumer Number: ");

16 | P a g e
long number = sc.nextLong();

System.out.println("Enter Unit's Consumed: ");

int units = sc.nextInt();

double amt = 0.0;

if (units <= 100)

amt = units * 5.5;

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

amt = (100 * 5.5) + ((units - 100) * 6.5);

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

amt = (100 * 5.5) + (200 * 6.5) + ((units - 300) * 7.5);

else

amt = (100 * 5.5) + (200 * 6.5) + (300 * 7.5) + ((units - 600) * 8.5);

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

System.out.println("Consumer Number: " + number);

System.out.println("Units Consumed: " + units);

System.out.println("Bill Amount: " + amt);

17 | P a g e
 Output:

18 | P a g e
5. Program 5
Given below is the table by which the income tax of a person is calculated. If age is more than
65 years or gender is female, then display wrong category. If age is less than or equal to 65
then compute the income tax payable. Execute a program to input gender, age and taxable
income of a person and compute and print the tax to be paid.

Taxable income in Rupees Income Tax

If it does not exceed Rs.160000 Nil

If it is greater than Rs.160000 and less than (Taxable income - 160000) * 10%
or equal to Rs.500000

If it is greater than Rs.500000 and less than [(Taxable income - 500000) * 20%] + Rs.34000
or equal to 800000

If it is greater than 800000 [(Taxable income - 800000) * 30%] + Rs.94000

 Code:

import java.util.*;

public class Prog_5

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter Gender(Male/Female):");

String gender = sc.nextLine();

19 | P a g e
System.out.println("Enter Age: ");

int age = sc.nextInt();

System.out.println("Enter Taxable Income: ");

double ti = sc.nextDouble();

double tax = 0.0;

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

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

else

if (ti <= 160000)

tax = 0.0;

else if (ti <= 500000)

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

else if (ti <= 800000)

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

else

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

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

20 | P a g e
}

 Output:

21 | P a g e
6. Program 6
Execute a program to accept the weight of parcel and type of mail (O for ordinary mail and E
for express mail). Then calculate and print the charge of courier under each category as per
the table given below:

Weight of parcel in grams Charge in rupees (Ordinary Charge in rupees (Express


Mail) Mail)

Upto 100 grams Rs.50 Rs.80

From 101 grams to 500 grams Rs.40 per 100 grams Rs.70 per 100 grams

501 grams and above Rs.35 per 100 grams Rs.65 per 100 grams

 Code:

import java.util.*;

public class Prog_6

public static void main(String args[])

Scanner in = new Scanner(System.in);

System.out.println("Enter weight of parcel: ");

double wt = in.nextDouble();

System.out.println("Enter type of booking, 'O' for Ordinary Mail or 'E' for Express Mail : ");

char type = in.next().charAt(0);

double charge = 0;
22 | P a g e
if(type == 'O')

if (wt <= 100)

charge = 50;

else if (wt <= 500)

charge = 50.0 + ((wt-100)/100) * 40.0;

else if (wt > 500)

charge = 50.0 + (4*40.0) + ((wt-500)/100) * 35.0;

else

System.out.println("Invalid input for weight of package.");

else if(type == 'E')

if (wt <= 100)

charge = 80;

else if (wt <= 500)

charge = 80 + ((wt-100)/100) * 70.0;

else if (wt > 500)

charge = 80 + (4*70.0) + ((wt-500)/100) * 65.0;

else

System.out.println("Invalid input for weight of package.");

23 | P a g e
else

System.out.println("Invalid unput for type of mail.");

System.out.println("Parcel charges = " + charge);

 Output:

24 | P a g e
7. Program 7
Given below is a table for calculating fine to be paid when a book is returned late to the
library. Execute a program in Java (with relevant supporting material) to calculate assuming
that a book is returned n days late.

Number of days Fine

First five days 40 paise per day

Next five days 65 paise per day

Next ten days 80 paise per day

 Code:

import java.util.*;

class Prog_7

public static void main(String args[])

int n;

double fine;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the no. of days the book is returned late:");

n = sc.nextInt();

if(n<=5)

25 | P a g e
fine=0.4*n;

else if(n<=10)

fine=(0.4*5)+(n-5)*0.65;

else

fine=(0.4*5)+(0.65*5)+(n-10)*0.80;

System.out.println("Fine="+fine);

 Output:

26 | P a g e
8. Program 8
Execute a menu-driven program to find the area of circle, square, rectangle as given below:
(i) Area of circle= pie*radius*radius.
(ii) Area of square= side*side.
(iii) Area of rectangle=length*breadth.
The choice should be given as:
(i) c for area of circle
(ii) s for area of square
(iii) r for area of rectangle.

 Code:

import java.util.*;

public class Prog_8

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("Enter c to calculate area of circle");

System.out.println("Enter s to calculate area of square");

System.out.println("Enter r to calculate area of rectangle");

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

switch(choice)

case 'c':

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

27 | P a g e
double r = sc.nextDouble();

double ca = (22.0 / 7.0) * r * r;

System.out.println("Area of circle = " + ca);

break;

case 's':

System.out.println("Enter side of square: ");

double side = sc.nextDouble();

double sa = side * side;

System.out.println("Area of square = " + sa);

break;

case 'r':

System.out.println("Enter length of rectangle: ");

double l = sc.nextDouble();

System.out.println("Enter breadth of rectangle: ");

double b = sc.nextDouble();

double ra = l * b;

System.out.println("Area of rectangle = " + ra);

break;

default:

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

28 | P a g e
}

 Output:

29 | P a g e
9. Program 9
Execute a program using switch case to find the volume of cube, sphere or cuboid. For an
incorrect choice, display an appropriate error message.
(i) Volume of cube= side*side*side.
(ii) Volume of sphere= 4*pie*r^3/3.
(iii) Volume of cuboid= length*breadth*height.

 Code:

import java.util.*;

public class Prog_9

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("1. Volume of Cube.");

System.out.println("2. Volume of Sphere.");

System.out.println("3. Volume of Cuboid.");

int choice = sc.nextInt();

switch(choice)

case 1:

System.out.println("Enter side of cube: ");

double cs = sc.nextDouble();

double cv = Math.pow(cs, 3);


30 | P a g e
System.out.println("Volume of cube = " + cv);

break;

case 2:

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

double r = sc.nextDouble();

double sv = (4.0 / 3.0) * (22.0 / 7.0) * Math.pow(r, 3);

System.out.println("Volume of sphere = " + sv);

break;

case 3:

System.out.println("Enter length of cuboid: ");

double l = sc.nextDouble();

System.out.println("Enter breadth of cuboid: ");

double b = sc.nextDouble();

System.out.println("Enter height of cuboid: ");

double h = sc.nextDouble();

double vol = l * b * h;

System.out.println("Volume of cuboid = " + vol);

break;

default:

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

31 | P a g e
}

 Output:

32 | P a g e
10. Program 10
You want to buy an old car from ‘Sales and Purchase’, where you can get a car in its
depreciated price. The depreciation value of a car is calculated on its showroom price and the
number of years it has been used. The depreciated value of a car is calculated as per the tariff
given below:

No. of years used Rate of depreciation

1 10%

2 20%

3 30%

4 50%

Above 4 years 60%

 Code:

import java.util.*;

public class Prog_10

public static void main(String args[])

Scanner sc = new Scanner(System.in);

double price,dep = 0.0;

int year;

33 | P a g e
System.out.println("Enter showroom price:");

price = sc.nextDouble();

System.out.println("Enter numbers of years car is used ");

year = sc.nextInt();

if(year == 1)

dep = price*0.1;

else if(year == 2)

dep = price*0.2;

else if(year == 3)

dep = price*0.3;

else if(year == 4)

dep = price*0.5;

else if(year >= 5)

dep = price*0.6;

System.out.println("Original Price : " + price);

System.out.println("Depriciated Value : " + dep);

System.out.println("Amount to be paid : " + (price-dep));

34 | P a g e
 Output:

35 | P a g e
11. Program 11
The relative velocity of two trains travelling in opposite directions is calculated by adding their
velocities. In case the trains are travelling in the same direction, the relative velocity is the
difference between the velocities of the two trains. Execute a menu-driven program to input
velocity and length of the trains. Calculate the relative velocity and time taken to cross each
other by indicating 1 for same direction and two for opposite direction.

 Code:

import java.util.*;

public class Prog_11

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter your choice from the following: ");

System.out.println("1. Trains travelling in same direction.");

System.out.println("2. Trains travelling in opposite direction.");

int choice = sc.nextInt();

System.out.println("Enter first train velocity: ");

double speed1 = sc.nextDouble();

System.out.println("Enter first train length: ");

double len1 = sc.nextDouble();

System.out.println("Enter second train velocity: ");

double speed2 = sc.nextDouble();

System.out.println("Enter second train length: ");


36 | P a g e
double len2 = sc.nextDouble();

double rSpeed = 0.0;

switch(choice)

case 1:

rSpeed = Math.abs(speed1 - speed2);

break;

case 2:

rSpeed = speed1 + speed2;

break;

default:

System.out.println("Invalid input. Please select from 1 or 2.");

double time = (len1 + len2) / rSpeed;

System.out.println("The relative velocity of the trains is: " + rSpeed);

System.out.println("The time taken by the trains to cross each other is: " + time);

37 | P a g e
 Output:

38 | P a g e
12. Program 12
An electronic shop has announced the following essential discount on the purchase of certain
items. Execute a menu-driven program based on above criteria to input name, address,
amount of purchase (‘L’ for laptop and ‘D’ for desktop). Compute and print the net amount
to be paid with name and address.

Category Discount-Laptop Discount-Desktop PC

Up to Rs.25000 0.0% 5.0%

From Rs.25001 to Rs.57000 5.0% 7.5%

From Rs.57001 to Rs.100000 7.5% 10.0%

More than Rs.100000 10.0% 15.0%

 Code:

import java.util.*;

public class Prog_12

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

String name = sc.nextLine();

System.out.println("Enter your address: ");

String address = sc.nextLine();

39 | P a g e
System.out.println("Enter Amount of Purchase: ");

double amt = sc.nextDouble();

System.out.println("Enter Type of Purchase.");

System.out.println("'L'- Laptop or 'D'- Desktop: ");

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

type = Character.toUpperCase(type);

double disc = 0.0;

if(type == 'L')

if (amt <= 25000)

disc = 0;

else if (amt>25000 && amt <= 50000)

disc = 0.05 * amt;

else if (amt>50000 && amt <= 100000)

disc = 0.075 * amt;

else

disc = 0.1 * amt;

else if(type == 'D')

if (amt <= 25000)

disc = 0.05 * amt;

40 | P a g e
else if (amt>25000 && amt <= 50000)

disc = 0.075 * amt;

else if (amt>50000 && amt <= 100000)

disc = 0.1 * amt;

else

disc = 0.15 * amt;

else

System.out.println("Invalid input. Enter only L for Laptop or D for Desktop.");

double NetAmt = amt - disc;

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

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

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

41 | P a g e
 Output:

42 | P a g e
13. Program 13
Execute a menu-driven program to find area of equilateral, isosceles and scalene triangles.
(i) Area of equilateral triangle= sqrt (3) * a * a/4
(ii) Area of Isosceles triangle= ¼ * b * sqrt (4a^2 - b^2)
(iii) Area of scalene triangle= sqrt (s * (s-m) * (s-n) * (s-p))

 Code:

import java.util.*;

public class Prog_13

public static void main(String args[])

Scanner sc = new Scanner( System.in );

int c;

double a,s,p,q,r,b,s1,s2,s3,sp=0.0;

double area;

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

System.out.println("1) Area of equilateral triangle.");

System.out.println("2) Area of isosceles triangle.");

System.out.println("3) Area of scalene triangle.");

c = sc.nextInt();

switch(c)

case 1:

43 | P a g e
System.out.println("Enter side of the equilateral triangle:");

s = sc.nextDouble();

area = Math.sqrt(3*s*s)/4;

System.out.println("The area of the equilateral triangle is: "+area);

break;

case 2:

System.out.println("Enter the side and base of the isosceles triangle:");

a = sc.nextDouble();

b = sc.nextDouble();

area = b/4 * (Math.sqrt(4*a*a - b*b));

System.out.println("The area of the isosceles triangle is: "+area);

break;

case 3:

System.out.println("Enter the sides of the scalene triangle:");

s1 = sc.nextDouble();

s2 = sc.nextDouble();

s3 = sc.nextDouble();

sp = (s1 + s2 + s3)/2.0;

area = Math.sqrt(sp*(sp-s1)*(sp-s2)*(sp-s3));

System.out.println("The area of the scalene triangle is: "+area);

break;

default:

44 | P a g e
System.out.println("Invalid input. Input 1,2 or 3 to find the area of the respective triangle.");

 Output:

45 | P a g e
14. Program 14
Enter the record of the employees of a company as employee number, age and basic salary.
Gross Salary= Basic + DA + HRA.
Net Salary= Gross Salary - (PF+EPF)
Enter ‘M’ or ‘F’ to check the category to decide whether an employee has to pay Income Tax
Payer or not. Print employee number, gross salary, net salary and also an Income Tax payer
or not.
Execute a program to calculate monthly gross salary and net salary of each employee as
follows:

Dearness Allowance (DA) Nature

House Rent Allowance (HRA) 40% of basic salary

Provident Fund (PF) 12% of Basic Salary

Employee Provident Fund (EPF) 2% of Basic Salary

The program further checks whether an employee is an Income Tax payer or no, as under:

Category Annual Gross Salary

Male(M) Up to Rs.250000 Not an Income Tax payer

Female(F) Up to Rs. 300000 Not an Income Tax payer

 Code:

import java.util.*;

public class Prog_14

46 | P a g e
{

public static void main( String args[] )

Scanner sc = new Scanner(System.in);

System.out.println("Enter employee number: ");

long num = sc.nextLong();

System.out.println("Enter age of employee: ");

int age = sc.nextInt();

System.out.println("Enter basic pay of the employee: ");

double bs = sc.nextDouble();

double da, hra, pf, epf;

double gs=0.0 , ns=0.0;

da = (bs * 25.0)/100.0;

hra = (bs * 40.0)/100.0;

pf = (bs * 12.0)/100.0;

epf = (bs * 2.0)/100.0;

gs = bs + da + hra;

ns= gs - (pf + epf);

System.out.println("Employee number: " + num);

System.out.println("Employee age: " + age);

System.out.println("Employee's gross pay: " + gs);

System.out.println("Employee's net salary: " + ns);

47 | P a g e
System.out.println("Enter 'M' for male or 'F' for female: ");

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

if(c == 'M')

if(gs <= 250000)

System.out.println("The Employee is not an Income tax payer.");

else

System.out.println("The Employee is an Income tax payer.");

if(c == 'F')

if(gs <= 300000)

System.out.println("The Employee is not an Income tax payer.");

else

System.out.println("The Employee is an Income tax payer.");

48 | P a g e
 Output:

49 | P a g e
15. Program 15
A hotel is giving a seasonal discount on the total amount to be paid by the person staying.
Execute a program to input the name of the guest, category (‘S’ for Semi-Deluxe Room, ‘D’
for Deluxe Room and ‘SD’ for Super Deluxe Room) and number of days stayed in the hotel.
Calculate the discount and total amount to be paid. Print the bill along with the name.
The charges for different rooms are given below:

Category Tariff

Semi-Deluxe Room Rs.2500 per day

Deluxe Room Rs.3500 per day

Super Deluxe Room Rs.5000 per day

The discount will be given as per the following criteria:

No. of days stayed Discount

Up to 3 days 10%

3 days up to 5 days 15%

5 days up to 10 days 20%

More than 10 days 30%

50 | P a g e
 Code:

import java.util.*;

public class Prog_15

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter Guest Name: ");

String name = sc.nextLine();

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

String cat = sc.nextLine();

System.out.print("Enter number of days: ");

int days = sc.nextInt();

int dp=0, t=0;

double amt, disc, netAmt;

if (days <= 3)

dp = 10;

else if (days <= 5)

dp = 15;

else if (days <= 10)

dp = 20;

else

51 | P a g e
dp = 30;

if (cat.equalsIgnoreCase("S"))

t = 2500;

else if (cat.equalsIgnoreCase("D"))

t = 3500;

else if (cat.equalsIgnoreCase("SD"))

t = 5000;

else

System.out.println("Incorrect Room Categoty");

amt = t * days;

disc = amt * dp / 100.0;

netAmt = amt - disc;

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

System.out.println("Total Amount: " + amt);

System.out.println("Discount: " + disc);

System.out.println("Total Payable Amount: " + netAmt);

52 | P a g e
 Output:

53 | P a g e
16. Program 16
Execute a menu driven program to perform arithmetical operations on numbers input by the
user according to his choice. Addition will be indicated as ‘1’, subtraction will be indicated as
‘2’, multiplication will be indicated as ‘3’ and division will be indicated as ‘4’.

 Code:

import java.util.*;

public class Prog_16

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter the two numbers to perform arithmetic operations.");

System.out.println("Enter the first number : ");

double x = sc.nextInt();

System.out.println("Enter the second number : ");

double y = sc.nextInt();

System.out.println("Choose the operation you want to perform: ");

System.out.println("1)Addition.");

System.out.println("2)Subtraction.");

System.out.println("3)Multiplication.");

System.out.println("4)Division.");

int n = sc.nextInt();

switch(n)

54 | P a g e
{

case 1:

double sum;

sum = x + y;

System.out.println("The sum of the two numbers is : "+sum);

break;

case 2:

double sub;

sub = x - y;

System.out.println("The difference between the two number is : "+sub);

break;

case 3:

double mult;

mult = x * y;

System.out.println("The product of the two number is : "+mult);

break;

case 4:

double div;

div = x/y;

System.out.println("The quotient obtained from division of the two numbers is : "+div);

break;

default:

55 | P a g e
System.out.println("Invalid input. Enter either 1,2,3 or 4 for respective operation.");

 Output:

56 | P a g e
17. Program 17
Execute a program to calculate the sum of all positive even numbers and all negative odd
numbers. Write zero to quit the program and display the result.

 Code:

import java.util.*;

public class Prog_17

public static void main(String args[])

Scanner sc = new Scanner ( System.in);

int num=1, oddsum=0, evensum=0;

System.out.println("Enter the series of numbers for calculating sum of odd numbers and sum
of even numbers of the entered series. ");

System.out.println("Enter 0 to quit the program and for displaying the results.");

while(num!=0)

int a = sc.nextInt();

num = a;

if(num == 0)

break;

if(a%2 == 0)

57 | P a g e
{

if(a > 0)

evensum = evensum + a;

if(a%2 != 0)

if(a>0)

oddsum = oddsum + a;

System.out.println("Sum of even numbers = "+evensum);

System.out.println("Sum of odd numbers = "+oddsum);

 Output:

58 | P a g e
18. Program 18
Execute a program to accept value of n from a user and calculate and print the factorial of n
using do-while loop.

 Code:

import java.util.*;

public class Prog_18

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int fact = 1;

int i = 1;

System.out.println("Enter a number whose factorial is to be found: ");

int num = sc.nextInt();

do

fact = fact * i;

i++;

while( i <= num );

System.out.println("Factorial of " + num + " is: " + fact);

59 | P a g e
}

 Output:

60 | P a g e
19. Program 19
Execute a program to check whether a number input by the user is even or odd.

 Code:

import java.util.*;

class Prog_19

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number to check whether it is odd or even.");

long num = sc.nextLong();

if (num % 2 == 0)

System.out.println("The entered number is even.");

else

System.out.println("The entered number is odd.");

61 | P a g e
 Output:

62 | P a g e
20. Program 20
Execute a program to accept a number and find the odd numbers below it.

 Code:

import java.util.*;

public class Prog_20

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

long num = sc.nextLong();

System.out.print("List of odd numbers from 1 to "+num+": ");

for (long i=1; i<=num; i++)

if (i%2!=0)

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

63 | P a g e
 Output:

64 | P a g e
21. Program 21
Execute a program to print the given pattern.
1
12
123
1234
12345

 Code:

import java.util.*;

public class Prog_21

public static void main(String args[])

int i, j, number, n=5;

System.out.println("The given pattern is: ");

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

number=1;

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

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

number++;

System.out.println();

65 | P a g e
}

 Output:

66 | P a g e
22. Program 22
Execute a program to accept the number and to check whether it is a perfect number or not.
A perfect number is called so when the sum of factors (including 1 and excluding the number
itself) is same as the original number.
Sample Input:6
Sample Output:1+2+3=6
It is a perfect number.

 Code:

import java.util.*;

public class Prog_22

public static void main(String args[])

Scanner sc=new Scanner(System.in);

long n, sum=0;

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

n = sc.nextLong();

int i=1;

while(i <= n/2)

if(n % i == 0)

sum = sum + i;

67 | P a g e
i++;

if(sum==n)

System.out.println(n+" is a perfect number.");

else

System.out.println(n+" is not a perfect number.");

 Output:

68 | P a g e
23. Program 23
Execute a program to accept two numbers and find the greatest common divisor (GCD) of the
two numbers.
Sample Input: 25, 45
Sample Output: GCD of 25,45 is 5.

 Code:

import java.util.*;

public class Prog_23

public static void main(String args[])

Scanner sc = new Scanner( System.in );

int x, y, gcd=1;

System.out.println("Enter two numbers whose gcd is to be found.");

x = sc.nextInt();

y = sc.nextInt();

for(int i = 1; i <= x && i <= y; i++)

if(x%i==0 && y%i==0)

gcd = i;

System.out.println("The greatest common divisor of " + x + " and " + y + " is : " + gcd);

69 | P a g e
}

 Output:

70 | P a g e
24. Program 24
Execute a program to accept a number and check whether the number is neon or not. A
number is said to be neon if sum of digits of its square is equal to the number itself.
Sample Input: 9
Sample Output: 9*9=81
8+1=9
9 is a neon number.

 Code:

import java.util.*;

public class Prog_24

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int sum = 0, n;

System.out.println("Enter the number to check whether it is a neon number : ");

n = sc.nextInt();

int square = n * n;

while(square != 0)

int digit = square % 10;

sum = sum + digit;

square = square / 10;

71 | P a g e
if(n == sum)

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

else

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

 Output:

72 | P a g e
25. Program 25
Execute a program in Java to enter a number and check whether the number is Armstrong. A
number is said to be Armstrong number if the sum of cubes of it’s digits is equal to the number
itself.
Sample Input: 153
Sample Output: 13 + 53 + 33 = 1+125+27=153.
153 is an Armstrong number.

 Code:

import java.util.*;

public class Prog_25

public static void main(String args[])

int n, count = 0, a, b, sum = 0;

Scanner sc = new Scanner(System.in);

System.out.print("Enter any integer you want to check:");

n = sc.nextInt();

b = n;

while(n > 0)

a = n % 10;

sum = (int) (sum+Math.pow(a, 3));

n = n / 10;

73 | P a g e
if(sum == b)

System.out.println("Given number is Armstrong.");

else

System.out.println("Given number is not Armstrong.");

 Output:

74 | P a g e
26. Program 26
Execute a program in Java to enter a number and check whether this is a palindrome number.
A number is a palindrome number if the new number obtained after reversing the digits is
the same.
Sample Input: 272
Sample Output: 272. It is a palindrome number.

 Code:

import java.util.*;

public class Prog_26

public static void main(String args[])

String original, reverse = "";

int num;

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number to check if it is a Palindrome: ");

num = sc.nextInt();

original = Integer.toString(num);

int length = original.length();

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

reverse = original.charAt(i) + reverse;

if (original.equals(reverse))

System.out.println("Entered number is a palindrome.");

else
75 | P a g e
System.out.println("Entered number isn't a palindrome.");

 Output:

76 | P a g e
27. Program 27
Execute a program to enter ten different numbers. Display the greatest and smallest numbers
from the set of numbers input by the user.

 Code:

import java.util.*;

public class Prog_27

public static void main( String args[] )

Scanner sc = new Scanner ( System.in );

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

int n = sc.nextInt();

int min = n, max = n;

System.out.println("Enter 9 more numbers to find the greatest and smallest of 10 entered


numbers.");

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

n = sc.nextInt();

if(n > max)

max = n;

else if(n < min)

min = n;

77 | P a g e
System.out.println("The greatest number is : " + max);

System.out.println("The smallest number is : " + min);

 Output:

78 | P a g e
28. Program 28
Execute a program in Java to display the first n number of terms in the Fibonacci series. In
this series, if the first number is zero and the second number is one, the third number is the
sum of the first two numbers.

 Code:

import java.util.*;

public class Prog_28

public static void main( String args[])

Scanner sc = new Scanner( System.in );

int a=0, b=1;

System.out.println("Enter the number of terms of the fibonacci series you want to be


displayed.");

int n = sc.nextInt();

System.out.print("The first " + n + " terms of the fibonacci series are : " + a + " " + b + " ");

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

int sum = a+b;

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

if(i%2 == 0)

a = sum;

else

79 | P a g e
b = sum;

 Output:

80 | P a g e
29. Program 29
Execute a program to input a number and check whether the number is a Niven number or
not. A number is said Niven if it is divisible by the sum of its digits.
Sample Input: 126
Sample Output: 1+2+6=9
126/9=14 hence 126 is a Niven number.

 Code:

import java.util.*;

public class Prog_29

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

int num = sc.nextInt();

int orgNum = num;

int digitSum = 0;

while (num != 0)

int digit = num % 10;

num = num/10;

digitSum = digitSum + digit;

if (digitSum != 0 && orgNum % digitSum == 0)


81 | P a g e
System.out.println(orgNum + " is a Niven number");

else

System.out.println(orgNum + " is not a Niven number");

 Output:

82 | P a g e
30. Program 30
A happy number is defined as a positive number that can replace the sum of squares of digits.
Repeat the process until the number is 1. If the number ends with 1, then it is called happy
number. Execute a program to input a number and check whether it is a happy number.
Sample Input: 31.
Sample Output: 3 + 1 = 10
10 = 1 + 0 = 1.
31 is a happy number.

 Code:

import java.util.*;

public class Prog_30

public static int isHappyNumber(int num)

int rem = 0, sum = 0;

while(num > 0)

rem = num%10;

sum = sum + (rem*rem);

num = num/10;

return sum;

public static void main(String[] args)

83 | P a g e
{

Scanner sc = new Scanner( System.in );

System.out.println("Enter number to check whether it is a happy number.");

int num = sc.nextInt();

int result = num;

while(result != 1 && result != 4)

result = isHappyNumber(result);

if(result == 1)

System.out.println(num + " is a happy number");

else

System.out.println(num + " is not a happy number");

 Output:

84 | P a g e
31. Program 31
(i) Execute a program to check whether a number is a prime number or not.
(ii) Accept a number to calculate and display 10 simultaneous prime numbers starting from
the number input by the user.

 Code:

import java.util.*;

public class Prog_31

public static boolean isPrime(long n)

for (long i = 2; i <= Math.sqrt(n); i++)

if (n % i == 0)

return false;

return true;

public static void main(String args[])

Scanner sc = new Scanner( System.in );

//Part 1 - Check Whether number is prime or not

85 | P a g e
int i,m=0,flag=0;

System.out.println("Enter number to check whether it is a prime number.");

int n = sc.nextInt();

m=n/2;

if(n==0||n==1)

System.out.println(n+" is not a prime number.");

else

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

if(n%i==0)

System.out.println(n+" is not a prime number.");

flag=1;

break;

if(flag==0)

System.out.println(n+" is a prime number.");

86 | P a g e
}

//Part 2 - Display 10 prime numbers from after entered number.

System.out.println("Enter the starting point: ");

long start = sc.nextInt();

sc.close();

if (start < 2)

start = 2;

int numberOfPrimes = 0;

long number = start;

while (numberOfPrimes < 10)

if (isPrime(number))

System.out.println(number);

numberOfPrimes++;

number++;

87 | P a g e
}

 Output:

88 | P a g e
32. Program 32
Execute a program to generate the following patterns.
1 for
54321
5432
543
54
5
2 for
*
*#
*#*
*#*#
*#*#*

 Code:

import java.util.*;

public class Prog_32

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("1) Pattern 1");

System.out.println("2) Pattern 2");

int n = sc.nextInt();

switch(n)

89 | P a g e
case 1:

int rows=5;

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

for (int j = rows; j >= i; j--)

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

System.out.println();

break;

case 2:

int i, j, row=5;

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

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

if(j%2 == 0)

System.out.print("* ");

else

System.out.print("# ");

90 | P a g e
System.out.println();

break;

default:

System.out.println("Invalid input. Input 1 or 2 to print the respective pattern.");

 Output:

91 | P a g e
33. Program 33
Execute a program to generate the following patterns according to user’s choice.
1 for
12345
22345
33345
44445
55555

2 for
13579
35791
57913
79135
91357

 Code:

import java.util.*;

public class Prog_33

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("1) Pattern 1");

System.out.println("2) Pattern 2");

int n = sc.nextInt();

int a;

92 | P a g e
switch(n)

case 1:

int rows=5;

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

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

if(j==i)

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

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

else

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

System.out.println();

break;

case 2:

int i, j, k; rows = 5;

for(i=1; i< rows+1 ;i++)

93 | P a g e
{

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

System.out.print((2*j-1) + " ");

for(k=1; k < i ;k++)

System.out.print((2*k-1) + " ");

System.out.println();

break;

default:

System.out.println("Invalid input. Input 1 or 2 to print the respective pattern.");

94 | P a g e
 Output:

95 | P a g e
34. Program 34
Execute a program using switch case to generate the following patterns.
1 for
54321
4321
321
21
1
2 for
666666
55555
4444
333
22
1

 Code:

import java.util.*;

public class Prog_34

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("1) Pattern 1");

System.out.println("2) Pattern 2");

int n = sc.nextInt();

int a, i, k, j;

96 | P a g e
switch(n)

case 1:

int rows=5; k=1;

for ( i = 5; i >0; i--)

for (j = 1; j < k; j++)

System.out.print(" ");

k++;

for (j = i; j > 0; j--)

System.out.print(j);

System.out.println();

break;

case 2:

rows = 6;

for(i=rows;i>=1;i--)

97 | P a g e
for(j=1;j<=i;j++)

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

System.out.println("");

break;

default:

System.out.println("Invalid input. Input 1 or 2 to print the respective pattern.");

 Output:

98 | P a g e
35. Program 35
Execute a program to generate the following patterns using switch case.
1 for
1
12
123
1234
12345
1234
123
12
1
2 for
11111
222
3
222
11111

 Code:

import java.util.*;

public class Prog_35

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("1) Pattern 1");

System.out.println("2) Pattern 2");

int n = sc.nextInt();

99 | P a g e
int a, c, i, z, j;

switch(n)

case 1:

int stopper=1;

System.out.println("Enter size of the pattern: ");

int size1 = sc.nextInt();

for( i=1; i<=size1*2-1; i++)

if(i <= size1)

stopper++;

else

stopper--;

for( c=1; c<=size1; c++)

if(c < stopper)

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

System.out.println();

break;

case 2:

100 | P a g e
System.out.println("Enter size of the pattern: ");

int size2 = sc.nextInt();

int print = 0;

int middle = size2/2;

int range = size2/2 + 1;

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

if(i<=middle)

range--;

print++;

else

range++;

print--;

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

if(j>=(middle-range) && j<=(middle+range))

System.out.print(print);

else

101 | P a g e
System.out.print(" ");

System.out.println();

break;

default:

System.out.println("Invalid input. Input 1 or 2 to print the respective pattern.");

 Output:

102 | P a g e
36. Program 36
Execute a program to generate the following patterns using switch case.
1 for
1
11
121
1331
14641
2 for
54321
4321
321
21
1
12
123
1234
12345

 Code:

import java.util.*;

public class Prog_36

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int i, j, k;

int p=1;

System.out.println("Enter choice of pattern as 1 or 2.");

int ch = sc.nextInt();

103 | P a g e
switch(ch)

case 1:

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

int num=1;

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

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

num = num * (i - j) / (j + 1);

System.out.println();

break;

case 2:

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

for(k=1; k<=p; k++)

System.out.print(" ");

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

104 | P a g e
{

System.out.print(j);

System.out.println("");

p = p + 1;

for ( i=2; i<=5; i++)

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

System.out.print(" ");

for ( k=1; k<=i; k++)

System.out.print(k);

System.out.println();

break;

105 | P a g e
 Output:

106 | P a g e
37. Program 37
Execute a program to input a number and check whether it is an automorphic number. A
number is said to be automorphic if it is contained in the last digits of the square of this
number.
Sample Input: 25
Sample Output: 625
It is an automorphic number.

 Code:

import java.util.*;

public class Prog_37

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number to check: ");

int num = sc.nextInt();

int count=0;

double square = num*num;

int temp = num;

while(temp>0)

temp=temp/10;

count++;

107 | P a g e
int lastDigit = (int) (square%(Math.pow(10, count)));

if(num == lastDigit)

System.out.println(num+ " is an automorphic number.");

else

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

 Output:

108 | P a g e
38. Program 38
The International Standard Book Number (ISBN) is a unique numeric book identifier which is
printed on every book. The ISBN is based upon a 10-digit code.
The ISBN is legal if:
(1xdigit1 + 2xdigit2 + 3xdigit3 + 4xdigit4 + 5xdigit5 + 6xdigit6 + 7xdigit7 + 8xdigit8 + 9xdigit9
+ 10xdigit10) is divisible by 11.
Sample Input: 1401601499
Sample Output: Legal ISBN code.
Execute a program to:
(i) Input the ISBN code 10-digit number.
(ii) If the ISBN is not a 10-digit number, then output the statement “Illegal ISBN code” and
terminate the program.
(iii) If the code is a 10-digit number, then extract the digits from the code and compute the
sum as explained above.
(iv) If the sum is divisible by 11, then print the message “Legal ISBN code”. If the sum is not
divisible by 11, then print “Illegal ISBN code”.

 Code:

import java.util.*;

public class Prog_38

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

long isbn = sc.nextLong();

int sum = 0, count = 0, m = 10;

while (isbn != 0)

109 | P a g e
int d = (int)(isbn % 10);

count++;

sum = sum + d * m;

m--;

isbn = isbn/10;

if (count != 10)

System.out.println("Illegal ISBN code.");

else if (sum % 11 == 0)

System.out.println("Legal ISBN code.");

else

System.out.println("Illegal ISBN code.");

110 | P a g e
 Output:

111 | P a g e
39. Program 39
Execute a program using switch case to solve the following two patterns:
(i) S=1+(1*2) +2+(1*2*3) +3+…. +(1*2*3*…*10).
(ii) S=a-(a^3)/5+(a^5)/9-(a^7)/13…to n.

 Code:

import java.util.*;

public class Prog_39

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("1) Pattern 1");

System.out.println("2) Pattern 2");

int c = sc.nextInt();

switch(c)

case 1:

double a=10, prod = 1, sum=1;

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

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

112 | P a g e
prod = prod*j;

sum = sum + i + prod;

prod = 1;

System.out.println("The sum of the series is : " + sum);

break;

case 2:

System.out.println("Enter value of variable a and till how many terms you want the sum of the
series to be calculated.");

a = sc.nextInt();

double n = sc.nextInt();

double i, j=5, sum2 =a, count = 0;

for(i=2; i<2*n; i=i+2)

count++;

if(count % 2 == 0)

sum2 = sum2 + Math.pow(a, i+1)/j;

else

sum2 = sum2 - Math.pow(a, i+1)/j;

j = j+4;

System.out.println("The sum of the series is : " + sum2);

113 | P a g e
break;

default:

System.out.println("Invalid input. Input 1 or 2 to print the result of the respective series.");

 Output:

114 | P a g e
40. Program 40
Execute a program using switch case to solve the sum of pattern
(i) S=1/(a^2) +4/(a^5) +7/(a^8) +10/(a^11) +…+n
(ii) S=1+2/ (1*2) +1+2+3/ (1*2*3) +…. +1+2+3…+n (1*2*3…*n)

 Code:

import java.util.*;

public class Prog_40

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("1) Pattern 1");

System.out.println("2) Pattern 2");

int c = sc.nextInt();

switch(c)

case 1:

double a, n, j = 2, sum=0;

System.out.println("Enter value of variable a and till how many terms you want the sum of the
series to be calculated.");

a=sc.nextInt();

n =sc.nextInt();

115 | P a g e
for (int i = 1; i < 3*n; i=i+3)

sum = sum + i/Math.pow(a,j);

j = j+3;

System.out.println("The sum of the series is : " + sum);

break;

case 2:

System.out.println("Enter number of terms till which you want the sum of the series to be
calculated.");

n = sc.nextInt();

double i, sum2 =0, prod=1, mainsum=0;

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

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

sum2 = sum2 + j;

prod = prod*j;

mainsum = mainsum + sum2/prod;

sum2=0; prod=1;

System.out.println("The sum of the series is : " + mainsum);

116 | P a g e
break;

default:

System.out.println("Invalid input. Input 1 or 2 to print the result of the respective series.");

 Output:

117 | P a g e
41. Program 41
Execute a program using switch case to solve the following pattern:
(i) S=1-(a/2) +3-(a/4) +5-(a/6) +…. +n
(ii) S=a/ (2^2) +(a^2)/ (2^3) +(a^3)/ (2^4) +…+n

 Code:

import java.util.*;

public class Prog_41

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

System.out.println("1) Pattern 1");

System.out.println("2) Pattern 2");

int c = sc.nextInt();

switch(c)

case 1:

double a, n, j = 2, sum=0, count=0;

System.out.println("Enter value of variable a and number of terms till which you want the sum
of the series to be calculated.");

a = sc.nextDouble();

n = sc.nextDouble();

118 | P a g e
for (int i = 1; i<=n; i++)

count++;

if(count%2 == 0)

sum = sum - a/j;

j = j+2;

else

sum = sum +i;

System.out.println("The sum of the series is : " + sum);

break;

case 2:

System.out.println("Enter value of variable a and number of terms till which you want the sum
of the series to be calculated.");

a = sc.nextDouble();

n = sc.nextDouble();

double i, sum2 =0;

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

119 | P a g e
sum2 = sum2 + Math.pow(a,i)/Math.pow(2,i+1);

System.out.println("The sum of the series is : " + sum2);

break;

default:

System.out.println("Invalid input. Input 1 or 2 to print the result of the respective series.");

 Output:

120 | P a g e
42. Program 42
Execute a program using switch case to convert a decimal number to binary number and vice-
versa.

 Code:

import java.util.*;

public class Prog_42

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int bin[] = new int[100];

int i=0;

int ch=0;

System.out.println("Enter 1 to convert Decimal number to Binary . Enter 2 to convert Binary


to Decimal.");

ch = sc.nextInt();

int n;

switch(ch)

case 1:

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

n = sc.nextInt();

while(n > 0)

121 | P a g e
{

bin[i++] = n%2;

n = n/2;

System.out.print("Binary equivalent of the number is : ");

for(int j=i-1; j >= 0; j--)

System.out.print(bin[j]);

System.out.println();

break;

case 2:

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

n = sc.nextInt();

long decimal = 0;

int base = 1;

long t = n;

while (t > 0)

int d = (int)(t % 10);

decimal += d * base;

base *= 2;

122 | P a g e
t /= 10;

System.out.println("Decimal equivalent of the number is : " + decimal);

break;

 Output:

123 | P a g e
43. Program 43
Execute a program in Java to enter a natural number. Display all the possible combinations
of consecutive natural numbers which adds up to give the sum equal to the original number.
Sample Input: 15
Sample Output: 1 2 3 4 5
456
78

 Code:

import java.util.*;

public class Prog_43

public static void main (String[] args)

Scanner sc = new Scanner(System.in);

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

int N = sc.nextInt();

int start = 1;

int end = (N + 1) / 2;

int flag = 0;

System.out.println("The possible combinations of numbers that add up to " + N + " are: ");

while (start < end)

int sum = 0;

for (int i = start; i <= end; i++)

124 | P a g e
{

sum = sum + i;

if (sum == N)

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

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

System.out.println();

flag=1;

break;

if (sum > N)

break;

sum = 0;

start++;

if(flag==0)

System.out.println("No possible combinations.");

125 | P a g e
 Output:

126 | P a g e
44. Program 44
Input a number between 2 to 5 (both inclusive). Print all possible combinations from 1 to that
number. Display the number of combinations at the end.
Sample Input: 3
Sample Output:
123
321
132
231
213
312
Number of Combinations=6

 Code:

import java.util.*;

public class Prog_44

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number between 2 and 5: ");

int number = sc.nextInt();

if(number < 2 || number > 5)

System.out.println("Invalid number. Please enter a number between 2 and 5.");

return;

127 | P a g e
int[] combination = new int[number];

boolean[] used = new boolean[number];

System.out.println("All possible combinations: ");

int count = printCombinations(combination, used, 0);

System.out.println("Number of combinations: " + count);

public static int printCombinations(int[] combination, boolean[] used, int index)

if (index == combination.length)

printCombination(combination);

return 1;

int count = 0;

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

if (!used[i])

combination[index] = i + 1;

used[i] = true;

count += printCombinations(combination, used, index + 1);

used[i] = false;

128 | P a g e
}

return count;

public static void printCombination(int[] combination)

for (int num : combination)

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

System.out.println();

 Output:

129 | P a g e
45. Program 45
Write a program in Java to round off a number N up to D digits of decimal. N and D are
accepted from the console.
Sample Input:
N=127.67898
D=3
Sample Output:
The number is 127.679

 Code:

import java.util.*;

public class Prog_45

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter number to be rounded off and places till which it should be rounded
off.");

double number = sc.nextDouble();

double round = sc.nextDouble();

number = Math.round(number * Math.pow(10, round)) / Math.pow(10, round);

System.out.println(number);

130 | P a g e
 Output:

131 | P a g e
46. Program 46
Write a program to display the given pattern for a word entered by the user.
Sample Input: BLUEJ
Sample Output:
BLUEJ
LUEJB
UEJBL
EJBLU
JBLUE

 Code:

import java.util.*;

public class Prog_46

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

String str = sc.next();

str = str.toUpperCase();

int n,i;

n=str.length();

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

System.out.print(str.substring(i,n));

System.out.print(str.substring(0,i)+"\n");

132 | P a g e
}

 Output:

133 | P a g e
47. Program 47
Execute a program in JAVA to accept a word and perform the following tasks:
(i) Count the number of vowels and display them without repeating.
(ii) Change the case of each letter in the string.
(iii) Change all the ‘e’ with ‘*’.

 Code:

import java.util.*;

public class Prog_47

public static void main (String args[])

Scanner sc = new Scanner (System.in);

String st;

int i=0, count=0, l;

char ch;

int a=0, b=0, c=0, d=0, e=0;

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

st = sc.nextLine();

st = st.toUpperCase();

System.out.println("The word entered is: " + st);

l = st.length();

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

134 | P a g e
ch = st.charAt(i);

if (ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' )

count++;

System.out.println("The number of vowels in the entered sentence is: " + count);

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

ch = st.charAt(i);

if ((ch=='A') &&(a==0))

a++;

System.out.println(ch);

else if ((ch=='E')&&(b==0))

b++;

System.out.println(ch);

else if ((ch=='I')&&(c==0))

c++;

System.out.println(ch);

135 | P a g e
}

else if ((ch=='O')&&(d==0))

d++;

System.out.println(ch);

else if ((ch=='U')&&(e==0))

e++;

System.out.println(ch);

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

ch = st.charAt(i);

if (ch == 'E')

ch = '*';

System.out.print(ch);

136 | P a g e
 Output:

137 | P a g e
48. Program 48
Execute a program to accept a string, convert it to upper case, and count the number of
double sequences that exist in the string. Display the new string after removing word(s) which
have repeated characters.
Sample Input:
SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE.
Sample Output:
Number of words with repeated letters= 4.
SHE WAS THE WITH AN.

 Code:

import java.util.*;

public class Prog_48

public static void main (String args[])

Scanner sc = new Scanner (System.in);

char ch, ch1;

int l, l1, i, j, count=0;

int printcount=0;

String st, st2 ="";

String st1="";

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

st = sc.nextLine();

st = st.toUpperCase();

st = st + " ";

138 | P a g e
l = st.length();

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

ch = st.charAt(i);

ch1 = st.charAt(i+1);

if(ch==ch1)

count++;

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

ch = st.charAt(i);

if (ch == ' ')

l1 = st1.length();

for (j=0; j<=l1-2; j++)

if (st1.charAt(j) == st1.charAt(j+1))

printcount++;

if(printcount==0)

139 | P a g e
{

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

printcount=0;

st1="";

else

st1 = st1 + ch;

System.out.println();

System.out.println("The number of words with repeated letters in the original string is "
+count);

 Output:

140 | P a g e
49. Program 49
Execute a program in JAVA to enter at least three words. Convert the first letter of each word
to upper case. Interchange the first and last letter of the string and print the new sentence.
Sample Input:
today is my sons birthday.
Sample Output:
Today Is My Sons Birthday.
yodaT sI yM sonS yirthdaB.

 Code:

import java.util.*;

public class Prog_49

public static void main (String args[])

Scanner sc = new Scanner (System.in);

String s = "";

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

s = sc.nextLine();

s = s + " ";

String str1, str2, str3;

String st1 = "";

String st2 = "";

String st3 = "";

String st4 = "";

141 | P a g e
char c, c1;

int i, j;

int l = 0;

int l1, l2=0;

l1 = s.length();

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

c = s.charAt(i);

if (c == ' ')

l2 = st1.length();

c1 = st1.charAt(0);

c1 = Character.toUpperCase(c1);

st2 = st1.substring(1,l2);

st3 = c1 + st2;

st4 = st4 + st3 + " ";

st3 = "";

st1 = "";

else

st1 = st1 + c;

142 | P a g e
System.out.println(st4);

StringTokenizer str = new StringTokenizer(st4);

System.out.println("The word after interchanging the first and a last alphabet is: ");

while(str.hasMoreTokens())

str1 = str.nextToken();

l = str1.length();

char t = str1.charAt(0);

str2 = str1.substring(1,(l-1));

char f = str1.charAt(l-1);

str3 = f + str2 + t;

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

str3 = "";

 Output:

143 | P a g e
50. Program 50
Execute a program to accept a sentence. Create a new sentence by replacing each consonant
with the previous letter. If the previous letter is a vowel, then replace it with the next letter
(i.e., the letter before ‘B’ is ‘A’ so ‘B’ must be replaced by ‘C’) and the other characters remain
the same.
Sample Input:
THE CAPITAL OF INDIA IS NEW DELHI.
Output:
SGE BAISAQ OG IMCIA MEV CEKGI.

 Code:

import java.util.*;

public class Prog_50

public static void main (String args[])

Scanner sc = new Scanner(System.in);

String st, st1="";

int l, i=0, j=0;

char c;

char nextchar;

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

st = sc.nextLine();

st = st.toUpperCase();

l = st.length();

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

144 | P a g e
{

c = st.charAt(i);

nextchar = (char)(c-1);

if(nextchar == 'A' || nextchar == 'E' || nextchar == 'I' || nextchar == 'O' || nextchar == 'U')

nextchar = (char)(c+1);

if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')

st1 = st1 + c;

else

st1 = st1 + nextchar;

System.out.println(st1);

145 | P a g e
 Output:

146 | P a g e
51. Program 51
Execute a program to accept a sentence and perform the following tasks:
(i) Display each word in opposite order.
(ii) Check whether the word is a palindrome or not.
(iii) Display the first and last characters of each letter.
Sample Input:
MADAM HONESTY IS THE BEST POLICY.
Sample Output:
MADAM YTSEOH SI EHT TESB YCILOP.
Palindrome Word: MADAM
First Letters: M, H, I, T, B, P.
Last Letters: M, Y, S, E, T, Y.

 Code:

import java.util.*;

public class Prog_51

public static void main (String args[])

Scanner sc = new Scanner (System.in);

String st, st2="", st3="", st4="";

int l, l2, i=0, j=0;

char c, c1, c2;

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

st = sc.nextLine();

System.out.println("The original sentence is: \n" +st);

String st1 = st + " ";

147 | P a g e
l = st1.length();

System.out.println("The changed sentence is: ");

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

c = st1.charAt(i);

if (c == ' ')

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

st2 = "";

else

st2 = c + st2;

System.out.println();

System.out.println("The palindromes in the sentence are: ");

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

c1 = st1.charAt(i);

if (c1 == ' ')

l2 = st3.length();

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

148 | P a g e
{

c2 = st3.charAt(j);

st4 = c2 + st4;

if (st4.equals(st3))

System.out.println(st4);

st3 = "";

st4 = "";

else

st3 = st3 + c1;

System.out.println("First letters: ");

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

c1 = st1.charAt(i);

if (c1 == ' ')

c2 = st3.charAt(0);

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

st3 = "";

149 | P a g e
else

st3 = st3 + c1;

System.out.println();

System.out.println("Last letters: ");

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

c1 = st1.charAt(i);

if (c1 == ' ')

l2 = st3.length();

c2 = st3.charAt(l2-1);

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

st3 = "";

else

st3 = st3 + c1;

150 | P a g e
 Output:

151 | P a g e
52. Program 52
Execute a program to accept a word and display the patterns using switch case.
(i) A
AB
ABC
ABCD
ABCDE
(ii) A B C D E
BCDE
CDE
DE
E

 Code:

import java.util.*;

public class Prog_52

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int c, i=0, j=0, k=0, r=0;

System.out.println("Enter 1 for the Pattern 1 and Enter 2 for the Pattern 2.");

c = sc.nextInt();

switch (c)

case 1:

152 | P a g e
r = 5;

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

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

System.out.print(" ");

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

System.out.print((char)('A' + k) + " ");

System.out.println();

break;

case 2:

r = 5;

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

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

153 | P a g e
System.out.print(" ");

for ( k=i; k<r; k++)

System.out.print((char)('A' + k) + " ");

System.out.println();

break;

default:

System.out.println("Invalid Choice!");

 Output:

154 | P a g e
53. Program 53
Execute a program to accept 10 different numbers in a single dimensional array. Now, accept
a number to be searched for in the array. If the number is found print “search successful”,
else print “search unsuccessful”.

 Code:

import java.util.*;

public class Prog_53

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int i, j, n, c=0;

int arr[] = new int[10];

System.out.println("Enter 10 elements in the array.");

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

arr[i] = sc.nextInt();

System.out.println("Enter the element that has to be searched: ");

n = sc.nextInt();

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

if (arr[j] == n)

155 | P a g e
c++;

if (c>0)

System.out.println("Search Successful!");

else

System.out.println("Search Unsuccessful!");

 Output:

156 | P a g e
54. Program 54
Execute a program to sort an array using selection sort technique.

 Code:

import java.util.*;

public class Prog_54

public static void main ( String args[])

Scanner sc = new Scanner (System.in);

int i, j, t, min, size;

System.out.println("Enter the size of the SDA: ");

size = sc.nextInt();

int m[] = new int[size];

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

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

m[i] = sc.nextInt();

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

min = i;

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

157 | P a g e
{

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

min = j;

t = m[i];

m[i] = m[min];

m[min] = t;

System.out.println("The numbers arranged in ascending order are: ");

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

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

158 | P a g e
 Output:

159 | P a g e
55. Program 55
Execute a program to sort an array using bubble sort technique.

 Code:

import java.util.*;

public class Prog_55

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int i, j, t, size;

System.out.println("Enter the size of the SDA: ");

size = sc.nextInt();

int m[] = new int[size];

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

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

m[i] = sc.nextInt();

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

for (j=0; j<(size-1); j++)

160 | P a g e
if (m[j] > m[j+1])

t = m[j];

m[j] = m[j+1];

m[j+1] = t;

System.out.println("The numbers arranged in ascending order using Bubble Sort are: ");

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

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

161 | P a g e
 Output:

162 | P a g e
56. Program 56
Execute a program to accept numbers in two different arrays. Then merge both the arrays
and print the result.

 Code:

import java.util.*;

public class Prog_56

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int i, n, m, p, pos;

int j=0;

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

n = sc.nextInt();

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

m = sc.nextInt();

p = m + n;

int arr1[] = new int [n];

int arr2[] = new int [m];

int arr3[] = new int [p];

System.out.println("Enter elements in array 1: ");

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

163 | P a g e
{

arr1[i] = sc.nextInt();

System.out.println("Enter elements in array 2: ");

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

arr2[i] = sc.nextInt();

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

arr3[i] = arr1[i];

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

arr3[i] = arr2[j];

j++;

System.out.println("Array formed from merging Array 1 & 2: ");

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

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

164 | P a g e
 Output:

165 | P a g e
57. Program 57
Execute a program to accept elements of a single dimensional array and insert an element
input by the user into the position input by him.

 Code:

import java.util.*;

public class Prog_57

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int i, n, ele, pos, size;

System.out.println("Enter the size of the array: ");

size = sc.nextInt();

int arr[] = new int [size+1];

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

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

arr[i] = sc.nextInt();

System.out.println("Enter element to be inserted: ");

ele = sc.nextInt();

System.out.println("Enter position of insertion: ");

166 | P a g e
pos = sc.nextInt();

for( i=(size-1); i>=(pos-1); i--)

arr[i+1] = arr[i];

arr[pos-1] = ele;

System.out.println("Array after inserting element: ");

for (i=0; i<size+1; i++)

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

 Output:

167 | P a g e
58. Program 58
Execute a program to accept the elements of an array and delete the element in the position
input by the user.

 Code:

import java.util.*;

public class Prog_58

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int i, n, pos;

System.out.println("Enter the size of the array: ");

n = sc.nextInt();

int arr[] = new int [n];

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

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

arr[i] = sc.nextInt();

System.out.println("Enter the position of the element that has to be deleted.");

pos = sc.nextInt();

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

168 | P a g e
{

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

n--;

System.out.println("Array elements after deletion is: ");

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

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

 Output:

169 | P a g e
59. Program 59
Execute a program to accept 15 names in a single dimensional array and display those names
which start with the letter entered by the user. Arrange the given array in alphabetical order.

 Code:

import java.util.*;

public class Prog_59

public static void main (String args[])

Scanner sc = new Scanner (System.in);

int i=0, j=0;

char ch;

String st="", temp="";

String arr[] = new String [15];

System.out.println("Enter 15 names in the SDA: ");

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

arr[i] = sc.nextLine();

System.out.println("Enter the letter for which the names starting with that letter should be
displayed: ");

ch = sc.next().charAt(0);

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

170 | P a g e
{

st = arr[i];

if (st.charAt(0)==ch)

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

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

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

if (arr[j].compareToIgnoreCase(arr[j+1]) > 0)

temp = arr[j];

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

arr[j+1] = temp;

System.out.println("The new array after rearranging words in alphabetical order is: ");

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

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

171 | P a g e
}

 Output:

172 | P a g e
60. Program 60
Execute a program to initialize the ‘Seven Wonders of The World’ and their locations in two
different arrays. Search for the name of the country input by user. If found display the country
along with its wonder. Else, display “Sorry not found!”
Seven Wonders: Chichen Itza, Christ The Redeemer, Taj Mahal, Great Wall of China, Machu
Picchu, Petra, Colosseum.
Locations: Mexico, Brazil, India, China, Peru, Jordan, Italy.
Input: Country Name: India
Output: India: Taj Mahal
Input: Country Name: USA
Output: Sorry not found!

 Code:

import java.util.*;

public class Prog_60

public static void main (String args[])

Scanner sc = new Scanner (System.in);

String[] wonders = {"Chichen Itza", "Christ The Redeemer", "Taj Mahal", "Great Wall of
China", "Machu Picchu", "Petra", "Colosseum"};

String[] locations = {"Mexico", "Brazil", "India", "China", "Peru", "Jordan", "Italy"};

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

String country = sc.nextLine();

int index = -1;

int i;

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

173 | P a g e
{

if (locations[i].equalsIgnoreCase(country))

index = i;

break;

if (index != -1)

System.out.println(country + ": " + wonders[index]);

else

System.out.println("Sorry not found!");

 Output:

174 | P a g e
61. Program 61
Execute a program to accept an array in matrix form and display the sum of left and right
diagonal elements.
Sample Input: 1 2 3 4
1234
1234
1234
Sum of Left Diagonal= 10
Sum of Right Diagonal= 10

 Code:

import java.util.*;

public class Prog_61

public static void main(String args[])

int i, j;

int sumld=0, sumrd=0;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows in the array: ");

int m = sc.nextInt();

System.out.println("Enter number of columns in the array: ");

int n = sc.nextInt();

int arr [][] = new int [m][n];

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

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

175 | P a g e
{

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

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

System.out.println("The entered array: ");

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 == j)

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

if (i+j == n-1)

176 | P a g e
sumrd = sumrd + arr[i][j];

System.out.println("The sum of left diagonal in the array is: " +sumld);

System.out.println("The sum of left diagonal in the array is: " +sumrd);

 Output:

177 | P a g e
62. Program 62
Execute a program in JAVA to accept a matrix in array form from the user and find the sum
of outer and inner elements. Also, find the difference between the two sums.
Sample Input: 1 2 3 4 5
12345
12345
12345
Sum of Outer Elements: 42
Sum of Inner Elements: 18
Difference: 42-18=24.

 Code:

import java.util.*;

public class Prog_62

public static void main(String args[])

int i, j;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows in the array: ");

int m = sc.nextInt();

System.out.println("Enter number of columns in the array: ");

int n = sc.nextInt();

int arr [][] = new int [m][n];

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

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

178 | P a g e
{

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

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

System.out.println("The entered array: ");

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

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

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

System.out.println();

int outerSum = 0;

int innerSum = 0;

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

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

if (i == 0 || i == m-1 || j == 0 || j == n-1)

179 | P a g e
{

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

else

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

int difference = outerSum - innerSum;

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

System.out.println("Sum of inner elements: " + innerSum);

System.out.println("Difference: " + difference);

180 | P a g e
 Output:

181 | P a g e
63. Program 63
Execute a program to accept number in a double dimensional array and display the array after
removing the corner elements.

 Code:

import java.util.*;

public class Prog_63

public static void main(String args[])

int i, j;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows in the array: ");

int m = sc.nextInt();

System.out.println("Enter number of columns in the array: ");

int n = sc.nextInt();

int arr [][] = new int [m][n];

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

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

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

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

182 | P a g e
}

System.out.println("The entered array: ");

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

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

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

System.out.println();

int[][] newArray = new int[m-2][n-2];

for ( i=1; i<m-1; i++)

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

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

System.out.println("Array after removing the corner elements: ");

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

183 | P a g e
for ( j=0; j<newArray[i].length; j++)

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

System.out.println();

 Output:

184 | P a g e
64. Program 64
Execute a program in JAVA to accept the elements of an array and shift the bottom-most row
to the top and shift the other rows, one place downwards each.
Sample Input: 1 2 3 4
2345
3456
4567
Sample Output: 4 5 6 7
1234
2345
3456

 Code:

import java.util.*;

public class Prog_64

public static void main(String args[])

int i, j;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows in the array: ");

int m = sc.nextInt();

System.out.println("Enter number of columns in the array: ");

int n = sc.nextInt();

int arr [][] = new int [m][n];

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

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

185 | P a g e
{

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

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

System.out.println("The entered array: ");

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

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

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

System.out.println();

int[] temp = arr[m-1];

for ( i=m-1; i>0; i--)

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

arr[0] = temp;

System.out.println("Matrix after shifting rows: ");

186 | P a g e
for ( i=0; i<m; i++)

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

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

System.out.println();

 Output:

187 | P a g e
65. Program 65
Execute a program in JAVA to accept the elements of Double-Dimensional Array
and compute the sum of each row and column. Then print the sum of each row along that
row and the sum of each column along the column.
Sample Input: 1 2 3 4
5678
9123
Sample Output: 1 2 3 4 10
5 6 7 8 26
9 1 2 3 15
15 9 12 15

 Code:

import java.util.*;

public class Prog_65

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

int row = sc.nextInt();

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

int col = sc.nextInt();

int rSum=0, cSum=0;

row = row + 1;

col = col + 1;

int arr[][] = new int[row][col];

188 | P a g e
System.out.println("Enter array elements: ");

for (int i=0; i<row-1; i++)

System.out.println("Enter Row: "+ (i+1) + ": ");

for (int j=0; j<col-1; j++)

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

rSum += arr[i][j];

arr[i][col-1] = rSum;

rSum = 0;

System.out.println();

System.out.println("Input Array: ");

for (int i=0; i<row-1; i++)

for (int j=0; j<col-1; j++)

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

System.out.println();

189 | P a g e
for (int i=0; i<col-1; i++)

for (int j=0; j<row-1; j++)

cSum += arr[j][i];

arr[row-1][i] = cSum;

cSum = 0;

System.out.println();

System.out.println("Array with Sum:");

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

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

if( i == row-1 && j == col-1)

break;

else

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

System.out.println();

190 | P a g e
}

 Output:

191 | P a g e
66. Program 66
Execute a program in Java to accept a double dimensional array in matrix form from the user.
The matrix should be a square matrix. Print the elements below the right diagonal and the
left diagonal of the matrix.
Sample Input: 1 2 5 6
3478
9 10 11 12
13 14 15 16
Sample Output:
The Elements below the Left Diagonal:
3
9 10
13 14 15
The Elements below the Right Diagonal:
8
11 12
14 15 16

 Code:

import java.util.*;

public class Prog_66

public static void main(String args[])

int i, j;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows and columns of the array: ");

int m = sc.nextInt();

int n = m;

192 | P a g e
int arr[][] = new int[m][n];

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

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

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

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

System.out.println();

System.out.println("The entered array: ");

System.out.println();

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

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

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

System.out.println();

System.out.println();

System.out.println("Elements below the left diagonal: ");

193 | P a g e
for ( i=0; i<m; i++)

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

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

System.out.println();

System.out.println();

System.out.println("Elements below the right diagonal: ");

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

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

if((i+j)>=n && (i!=0))

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

System.out.println();

194 | P a g e
}

 Output:

195 | P a g e
67. Program 67
Execute a program in JAVA to accept the elements of a matrix and print its mirror image.
Sample Input: 1 2 3 7 8
45609
Sample Output: 8 7 3 2 1
90654

 Code:

import java.util.*;

public class Prog_67

public static void main(String args[])

int i, j;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows of the array: ");

int m = sc.nextInt();

System.out.println("Enter number of columns of the array: ");

int n = sc.nextInt();

int arr[][] = new int[m][n];

int transpose[][] = new int[m][n];

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

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

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


196 | P a g e
{

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

System.out.println("The entered array: ");

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

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

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

System.out.println();

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

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

transpose[i][n-1-j] = arr[i][j];

System.out.println("The Mirrored array: ");

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

197 | P a g e
{

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

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

System.out.println();

 Output:

198 | P a g e
68. Program 68
Execute a program to accept the elements of a matrix and print the matrix after shifting the
elements to the next closest prime number.

 Code:

import java.util.*;

public class Prog_68

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows in the array: ");

int rows = sc.nextInt();

System.out.println("Enter the number of columns in the array: ");

int columns = sc.nextInt();

int matrix[][] = new int[rows][columns];

int i, j;

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

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

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

matrix[i][j] = sc.nextInt();

199 | P a g e
}

int shiftedMatrix[][] = shiftToNextPrime(matrix);

System.out.println("Matrix after shifting to the next closest prime number: ");

printMatrix(shiftedMatrix);

public static int[][] shiftToNextPrime(int[][] matrix)

int i, j;

int shiftedMatrix[][] = new int[matrix.length][matrix[0].length];

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

for ( j=0; j<matrix[0].length; j++)

shiftedMatrix[i][j] = getNextPrime(matrix[i][j]);

return shiftedMatrix;

public static int getNextPrime(int number)

while (true)

200 | P a g e
{

number++;

if (isPrime(number))

return number;

public static boolean isPrime(int number)

int i;

if (number <= 1)

return false;

for ( i=2; i<=Math.sqrt(number); i++)

if (number%i == 0)

return false;

201 | P a g e
return true;

public static void printMatrix(int[][] matrix)

int i, j;

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

for ( j=0; j<matrix[0].length; j++)

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

System.out.println();

202 | P a g e
 Output:

203 | P a g e
69. Program 69
Write a program in JAVA to accept an integer array from the user in matrix form and convert
all the integers to their respective roman numerals.
Sample Input:
1 7 10
6 50 12
33 19 20
Sample Output:
I VII X
VI L XII
XXXIII XIX XX

 Code:

import java.util.*;

public class Prog_69

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of rows in the array: ");

int rows = sc.nextInt();

System.out.print("Enter the number of columns in the array: ");

int columns = sc.nextInt();

int matrix[][] = new int[rows][columns];

int i, j;

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

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

204 | P a g e
{

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

matrix[i][j] = sc.nextInt();

System.out.println("Matrix with Roman numerals:");

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

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

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

System.out.println();

public static String convertToRoman(int number)

if (number < 1 || number > 3999)

System.out.println("Number must be between 1 and 3999");

205 | P a g e
String thousands[] = { "", "M", "MM", "MMM"};

String hundreds[] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

String tens[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};

String ones[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

String romanNumeral = "";

romanNumeral += thousands[number / 1000];

romanNumeral += hundreds[(number % 1000) / 100];

romanNumeral += tens[(number % 100) / 10];

romanNumeral += ones[number % 10];

return romanNumeral;

 Output:

206 | P a g e
70. Program 70
Write a program in Java to accept an array of square matrix form from the user and check if
it is a magic square or not. A magic square is a square matrix divided into smaller squares
each containing a number, such that the figures in each vertical, horizontal, and diagonal row
add up to the same value (i.e., sum of all the rows, diagonals and columns is the same).
Sample Input:
276
951
438
Sample Output:
It is a magic square.

 Code:

import java.util.*;

public class Prog_70

public static void main(String args[])

int i, j;

boolean flag = false;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows in the array: ");

int m = sc.nextInt();

System.out.println("Enter number of columns in the array: ");

int n = sc.nextInt();

int arr[][] = new int[m][n];

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

207 | P a g e
for( i=0; i<m; i++)

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

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

System.out.println("The entered array: ");

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

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

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

System.out.println();

int sum1=0, sum2=0;

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

sum1 += arr[i][i];

sum2 += arr[i][m-1-i];

208 | P a g e
if(sum1 != sum2)

flag = true;

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

int rowSum=0, colSum=0;

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

rowSum += arr[i][j];

colSum += arr[j][i];

if (rowSum != colSum || colSum != sum1)

flag = true;

if (!flag)

System.out.println("Given matrix is a Magic Square!");

else

System.out.println("Given matrix is a not a Magic Square!");

209 | P a g e
 Output:

210 | P a g e
71. Program 71
Write a program to declare a matrix A [] [] of order m x n where ‘m’ is the number of row and
‘n’ is the number of columns. Allow the user to input integers into this matrix. Perform the
following tasks on the matrix:
(i) Display the input matrix.
(ii) Find the minimum and maximum value in the matrix and display them along with their
position.
(iii) Sort the elements of the matrix in ascending order using any standard sorting technique
and rearrange them in the matrix.

 Code:

import java.util.*;

public class Prog_71

public static void main(String args[])

int i, j;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows of the array: ");

int m = sc.nextInt();

System.out.println("Enter number of columns of the array: ");

int n = sc.nextInt();

int arr[][] = new int[m][n];

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

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

211 | P a g e
for( j=0; j<n; j++)

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

System.out.println("The entered array: ");

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

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

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

System.out.println();

int min = arr[0][0];

int max = arr[0][0];

int minRow = 0, minColumn = 0;

int maxRow = 0, maxColumn = 0;

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

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

212 | P a g e
if (arr[i][j] < min)

min = arr[i][j];

minRow = i;

minColumn = j;

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

max = arr[i][j];

maxRow = i;

maxColumn = j;

System.out.println("Minimum value: " + min + " at position (" + (minRow + 1) + "," +


(minColumn + 1) + ").");

System.out.println("Maximum value: " + max + " at position (" + (maxRow + 1) + "," +


(maxColumn + 1) + ").");

int temp;

int q = arr.length;

int r = arr[0].length;

for ( i=0; i<q*r-1; ++i)

213 | P a g e
for ( j=0; j<q*r-1-i; ++j)

if (arr[j/q][j%r] > arr[(j+1)/r][(j+1)%r])

temp = arr[(j+1)/r][(j+1)%r];

arr[(j+1)/r][(j+1)%r] = arr[j/r][j%r];

arr[j/r][j%r] = temp;

System.out.println("The Sorted array: ");

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

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

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

System.out.println();

214 | P a g e
 Output:

215 | P a g e
72. Program 72
Write a program to store few numbers in single dimensional array and search for a number
using binary search technique. Perform the task using recursive function.

 Code:

import java.util.*;

class Prog_72

public static int bin_search(int arr[],int l,int u, int v, int f)

int mid;

mid = (l+u)/2;

if(l>u || f==1)

return (f);

else

if(v == arr[mid])

f = 1;

if(v < arr[mid])

u = mid - 1;

if(v > arr[mid])

l = mid + 1;

return (bin_search (arr ,l,u,v,f));

216 | P a g e
public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of array: ");

int n = sc.nextInt();

int arr[] = new int[n];

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

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

arr[i] = sc.nextInt();

System.out.println("Enter element to be searched: ");

int v = sc.nextInt();

int l=0;

int u=n-1;

int f=0;

int result = bin_search(arr,l,u,v,f);

if(result == 1)

System.out.println("Element found.");

else

System.out.println("Element not found.");

217 | P a g e
}

 Output:

218 | P a g e
73. Program 73
Smith number is a composite number whose sum of digits of prime factors is equal to sum of
digits of number. Write a program in JAVA to accept a number from the user and check if it is
a smith number or not.
Sample Input: 85
Sample Output: It is a smith number.

 Code:

import java.util.Scanner;

public class Prog_73

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt();

if (n <= 0)

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

return;

boolean isComposite = false;

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

if (n%i == 0)
219 | P a g e
{

isComposite = true;

break;

if (isComposite && n != 1)

int sumDigits = 0;

int t = n;

while (t != 0)

int d = t % 10;

sumDigits += d;

t /= 10;

int sumPrimeDigits = 0;

t = n;

for(int i=2; i<t; i++)

while(t%i == 0)

t /= i;

220 | P a g e
int temp = i;

while (temp != 0)

int d = temp % 10;

sumPrimeDigits += d;

temp /= 10;

if(t > 2)

while (t != 0)

int d = t % 10;

sumPrimeDigits += d;

t /= 10;

if (sumPrimeDigits == sumDigits)

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

else

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

221 | P a g e
}

else

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

 Output:

222 | P a g e
74. Program 74
A prime number is called circular if it remains prime after any cyclic combinations of its digits.
Write a program to input any number from the user and check if it is circular prime.

 Code:

import java.util.*;

public class Prog_74

public static boolean isPrime(int num)

int c=0;

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

if (num%i == 0)

c++;

return c == 2;

public static int getDigitCount(int num)

int c=0;

223 | P a g e
while(num != 0)

c++;

num /= 10;

return c;

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter an integer to be checked: ");

int n = sc.nextInt();

if (n <= 0)

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

return;

boolean isCircularPrime = true;

if (isPrime(n))

System.out.println(n);

int digitCount = getDigitCount(n);

224 | P a g e
int divisor = (int)(Math.pow(10, digitCount-1));

int n2 = n;

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

int t1 = n2 / divisor;

int t2 = n2 % divisor;

n2 = t2 * 10 + t1;

System.out.println(n2);

if (!isPrime(n2))

isCircularPrime = false;

break;

else

isCircularPrime = false;

if (isCircularPrime)

System.out.println(n + " is a circular prime.");

225 | P a g e
}

else

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

 Output:

226 | P a g e
75. Program 75
Write a program in Java to enter an amount of money by the user and write down its
denomination in terms of rupee notes of 1000, 500, 200, 100, 50, 20, 10, 5 and 1.
Sample Input: 674
Sample Output:
500 x1
100 x1
50x1
20x1
1 x4
Total:674

 Code:

import java.util.Scanner;

public class Prog_75

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.print("Enter the amount: ");

int amt = sc.nextInt();

if (amt > 99999)

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

return;

System.out.println("Denomination: ");

227 | P a g e
int notes[] = {1000, 500, 200, 100, 50, 20, 10, 5 ,1};

int t = amt;

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

int c = t / notes[i];

if (c != 0)

System.out.println(notes[i] + "\t*\t" + c + "\t=\t" + (c * notes[i]));

t = t % notes[i];

System.out.println("Total: " +amt);

 Output:

228 | P a g e
76. Program 76
Write a program to declare a square matrix a[][] of order m x m where ‘m’ is the number of
rows and columns, such that m is greater than 2 and less than 10. Accept the value of ‘m’ as
user input. Display an appropriate message of invalid input. Allow the user to enter integer
numbers in the array. Perform the following tasks:
Display original matrix
Rotate the matrix 90 degree clockwise.
Find the sum of corner elements of the matrix.
Sample Input:
349
258
167
Sample output:
123
654
789
Sum of corner elements=20

 Code:

import java.util.*;

public class Prog_76

public static void main(String args[])

int i, j;

int s = 0;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows");

int m = sc.nextInt();

229 | P a g e
System.out.println("Enter number of columns");

int n = sc.nextInt();

int arr [][] = new int [m][n];

System.out.println("Enter Array numbers");

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

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

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

System.out.println("The entered array");

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

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

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

System.out.println();

int b[][] = new int[m][m];

int x;

230 | P a g e
for( i=0; i<m; i++)

x = m-1;

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

b[i][j] = arr[x][i];

x--;

System.out.println("The rotated array");

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

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

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

System.out.println();

int sum = arr[0][0] + arr[0][m-1] + arr[m-1][0] + arr[m-1][m-1];

System.out.println("Sum of the corner elements = " + sum);

231 | P a g e
 Output:

232 | P a g e
77. Program 77
Write a program to accept a sentence which may be terminated by either ‘.‘ or ’?’ only. The
words are to be separated by a single blank space. Print an error message if the input does
not terminate with ‘.‘ or ‘?‘. You can assume that no word in the sentence exceeds 15
characters, so that you get a proper formatted output.
Perform the following tasks:
(i) Convert the first letter of each word to uppercase.
(ii) Find the number of vowels and consonants ¡n each word and display them with proper
headings along with the words.
Sample Input: Intelligence plus character is education.
Sample Output: Intelligence Plus Character Is Education

Words Vowels Consonants

Intelligence 5 7

Plus 1 3

Character 3 6

Is 1 1

Education 5 4

 Code:

import java.io.*;

class Prog_77

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

233 | P a g e
{

int i, j, vowels, cons, p, l;

String str,word, tmp;

char ch, ch1;

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

System.out.println("Enter a paragraph: ");

str=br.readLine();

l = str.length();

ch = str.charAt(l-1);

System.out.println("\nOutput:");

if(ch != '.' && ch != '?')

System.out.println("Invalid Input!");

else

p = vowels = cons = 0;

tmp = str + " ";

str = "";

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

ch = tmp.charAt(i);

if(ch == ' ')

234 | P a g e
word = tmp.substring(p,i);

ch1 = word.charAt(0);

word = Character.toUpperCase(ch1)+word.substring(1);

str+= word + " ";

p = i + 1;

System.out.println("\n"+str);

System.out.print("\nWord");

for(j=15-4; j>=1;j--)

System.out.print(" ");

System.out.println("\tVowels\tConsonants");

p=0;

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

ch = str.charAt(i);

if( ch != ' ' && ch != '.' && ch != '?')

if((ch>=65 && ch <= 90) || (ch>=97 && ch<=122))

if("aeiouAEIOU".indexOf(ch) != -1)

vowels++;

235 | P a g e
else

cons++;

else

word = str.substring(p,i);

System.out.print(word);

for(j= 15 - word.length(); j>=1;j--)

System.out.print(" ");

System.out.println("\t "+vowels+"\t "+cons);

p = i + 1;

vowels = cons = 0;

236 | P a g e
 Output:

237 | P a g e
78. Program 78
Write a program to accept lower and upper limits of numbers and print those numbers which
do not have repetition of digits and the count of such numbers.
Example:
Lower limit= 290
Upper Limit= 300
Numbers are:
290,291,293,294,295,296,297,298
Total numbers=7

 Code:

import java.util.*;

public class Prog_78

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.print("Enter the lower limit: ");

int lowerLimit = sc.nextInt();

System.out.print("Enter the upper limit: ");

int upperLimit = sc.nextInt();

System.out.println("Numbers without repetition of digits:");

int count = 0;

for (int num = lowerLimit; num <= upperLimit; num++)

if (hasUniqueDigits(num))

238 | P a g e
{

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

count++;

System.out.println("\nTotal numbers: " + count);

public static boolean hasUniqueDigits(int num)

boolean[] visited = new boolean[10];

while (num > 0)

int digit = num % 10;

if (visited[digit])

return false;

visited[digit] = true;

num /= 10;

return true;

239 | P a g e
}

 Output:

240 | P a g e
79. Program 79
Write a program to use Caeser Cipher rotation and encode the given string. In this technique,
the letter is replaced by the letter 13 places after it in the alphabets. Other characters remain
unchanged.
Example: Hello! How are you?
Output: Urryb! Ubj ner Ibh?

 Code:

import java.util.*;

public class Prog_79

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter plain text:");

String str = sc.nextLine();

int len = str.length();

if (len <= 3 || len >= 100)

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

return;

StringBuffer sb = new StringBuffer();

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

{
241 | P a g e
char ch = str.charAt(i);

if ((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))

sb.append((char)(ch + 13));

else if ((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))

sb.append((char)(ch - 13));

else

sb.append(ch);

String cipher = sb.toString();

System.out.println("The cipher text is:");

System.out.println(cipher);

242 | P a g e
 Output:

243 | P a g e
80. Program 80
Write a program to print the consecutive and repeating characters in a string.
Sample Input: Understanding Computer Science
Sample Output:
Consecutive Characters:
d
e
r
s
s
t
Repeating Characters:
c
d
e
i
n
r
s
t
u

 Code:

import java.util.*;

public class Prog_80

public static void main(String args[])

Scanner sc = new Scanner(System.in);

String s;

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

244 | P a g e
s = sc.nextLine();

s = s.toLowerCase();

int len = s.length();

int j;

int i;

System.out.println("Consequtive Letters: " );

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

if ((int)s.charAt(i+1) -(int)s.charAt(i)==1)

System.out.println(s.charAt(i));

System.out.println(s.charAt(i+1));

System.out.println("Repeating Letters: " );

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

int repetitions=0;

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

char ch = s.charAt(j);

if(ch == k)

245 | P a g e
{

repetitions++;

if(repetitions>1)

System.out.println(ch);

break;

 Output:

246 | P a g e
81. Program 81
Print the following pattern:

 Code:

import java.util.*;

public class Prog_81

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int c, r, size, mid, range;

System.out.println("Enter the size of the pattern: ");

size = sc.nextInt();

mid = size/2;

range = -1;

for( r=0; r< size; r++)

247 | P a g e
if(r<=mid)

range++;

else

range--;

for( c=0; c<size; c++)

if(r%2 == 0)

if(c>=(mid-range) && c<=(mid+range) && c%2==0)

System.out.print("*\t");

else

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

else

if(c>=(mid-range) && c<=(mid+range) && c%2==1)

System.out.print("*\t");

else

248 | P a g e
System.out.print(" \t");

System.out.println();

 Output:

249 | P a g e
82. Program 82
The names of the teams participating in a competition must be printed on a banner vertically,
to accommodate as many teams as possible in a single banner. Design a program to accept
the names of N teams, where 2<N<9 and display them in vertical order side by side using
horizontal tab (8 spaces).
Example:
Input:
Teams= 3
Team 1: Emus
Team 2: Road Rols
Team 3: Coyote
Output:

 Code:

import java.util.*;

public class Prog_82

public static void main(String args[])

Scanner sc = new Scanner(System.in);

250 | P a g e
System.out.print("Enter the number of teams: ");

int n = sc.nextInt();

sc.nextLine();

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

System.out.println("Invalid Input!");

return;

String teams[] = new String[n];

int highLen = 0;

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

System.out.print("Team " + (i+1) + ": ");

teams[i] = sc.nextLine();

if (teams[i].length() > highLen)

highLen = teams[i].length();

System.out.println();

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

251 | P a g e
for (int j=0; j<n; j++)

int len = teams[j].length();

if (i >= len)

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

else

System.out.print(teams[j].charAt(i) + "\t");

System.out.println();

252 | P a g e
 Output:

253 | P a g e
83. Program 83
Write a program to accept two words and check whether they are anagram words or not.
Anagram words are those which have same length and rearrangement of letters of one word
gives the other word.
Example:
Input: Creative, Reactive
Output: They are anagram words.

 Code:

import java.util.*;

public class Prog_83

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int p1, p2, i, j, as1, as2, s1=0, s2=0;

String str1, str2, str3 =" " , str4 =" ";

char chr1, chr2;

System.out.println("Enter the first word");

str1 = sc.nextLine();

str1 = str1.toUpperCase();

System.out.println("Enter the Second word");

str2 = sc.nextLine();

str2 = str2.toUpperCase();

p1 = str1.length();

254 | P a g e
p2 = str2.length();

if(p1 == p2)

for(i=65; i<=90; i++)

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

chr1 = str1.charAt(j);

chr2 = str2.charAt(j);

as1 = (int)chr1;

as2 = (int)chr2;

if(as1 == i)

str3 = str3 + chr1;

s1 = s1 + as1;

if(as2 == i)

str4 = str4 + chr2;

s2 = s2 + as2;

255 | P a g e
}

if(str3.equals(str4) && (s1 == s2))

System.out.println( str1 +" and " + str2 +" are anagrams.");

else

System.out.println( str1 +" and " + str2 +" are not anagrams.");

else

System.out.println( "Wrong input. Re-enter words for anagram.");

 Output:

256 | P a g e
84. Program 84
Write a program to declare a matrix A[][] of order (m*n) where ‘m’ is the number of rows and
‘n’ is the number of columns. Allow the user to input integers into this matrix. Perform the
following tasks on the matrix:
(i) Display the input matrix
(ii) Find the maximum and minimum value in the matrix and display them along with their
position
(iii) Sort the elements of the matrix in ascending order using any standard sorting technique
and rearrange them in the matrix.
(iv) Display the rearranged matrix.

 Code:

import java.util.*;

public class Prog_84

public static void main(String args[])

int i,j;

Scanner sc = new Scanner(System.in);

System.out.println("Enter number of rows");

int m = sc.nextInt();

System.out.println("Enter number of columns");

int n = sc.nextInt();

int arr [][] = new int [m][n];

System.out.println("Enter Array numbers");

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

257 | P a g e
{

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

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

System.out.println("The Entered Array: ");

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

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

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

System.out.println();

int min = arr[0][0];

int max = arr[0][0];

int minRow = 0, minColumn = 0;

int maxRow = 0, maxColumn = 0;

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

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

258 | P a g e
{

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

min = arr[i][j];

minRow = i;

minColumn = j;

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

max = arr[i][j];

maxRow = i;

maxColumn = j;

System.out.println("Minimum value: " + min + " at position (" + (minRow + 1) + "," +


(minColumn + 1) + ")");

System.out.println("Maximum value: " + max + " at position (" + (maxRow + 1) + "," +


(maxColumn + 1) + ")");

int temp, k;

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

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

259 | P a g e
{

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

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

temp = arr[i][k];

arr[i][k] = arr[i][k+1];

arr[i][k+1] = temp;

System.out.println("The Sorted Array:");

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

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

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

System.out.println();

260 | P a g e
}

 Output:

261 | P a g e
85. Program 85
Write a program to find out if a number entered by the user is a bouncy number. A bouncy
number is a positive number whose digits neither increase nor decrease. For example, 12345
is an increasing number and 54321 is a decreasing number. By definition, all numbers under
100 are non-bouncy, and 101 is the first bouncy number.
Sample Input: 25143
Sample Output: It is a bouncy number.

 Code:

import java.util.*;

public class Prog_85

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt();

if (n < 100)

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

return;

int t = n;

boolean isIncreasing = true, isDecreasing = true;

int prev = t % 10;

262 | P a g e
while (t != 0)

int d = t % 10;

if (d > prev)

isIncreasing = false;

break;

prev = d;

t /= 10;

t = n;

prev = t % 10;

while (t != 0)

int d = t % 10;

if (d < prev)

isDecreasing = false;

break;

prev = d;

263 | P a g e
t /= 10;

if (!isIncreasing && !isDecreasing)

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

else

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

 Output:

264 | P a g e
86. Program 86
A new advanced Operating System, incorporating the latest hi-tech features has been
designed by Opera Computer Systems. The task of generating copy protection code to
prevent software piracy has been entrusted to the Security Department. The Security
Department has decided to have codes containing a jumbled combination of alternate
uppercase letters of the alphabet from ‘A’ to ‘K’ (namely among A, C, E, G, I, K). The code may
or may not be in the consecutive series of alphabets. Each code should not exceed 6
characters. Else, an appropriate error message must be displayed.
Write a program to input a code and its length. At the first instance of an error, display
“Invalid” stating the appropriate reason. In case of no error, display the message “Valid”.

 Code:

import java.util.*;

public class Prog_86

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter length of the code :");

int length = sc.nextInt();

if (length < 1 || length > 6)

System.out.println("Error! length of String should not exceed 6 characters!");

return;

System.out.println("Enter Code:");

265 | P a g e
String code = sc.next();

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

char ch = code.charAt(i);

if (Character.isLowerCase(ch))

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

return;

if (ch < 'A' || ch > 'K')

System.out.println("Invalid! Only letters between A and K are permitted!");

return;

if (ch % 2 == 0)

System.out.println("Invalid! Only alternate letters permitted!");

return;

int count = 0;

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

266 | P a g e
if (ch == code.charAt(j))

count++;

if (count > 1)

System.out.println("Invalid! Letter repetition is not permitted!");

return;

System.out.println("The code is valid.");

267 | P a g e
 Output:

268 | P a g e

You might also like