Professional Documents
Culture Documents
Session: 2022-23
Class : XII B
Roll No. : 25
Senior Principal Mrs. Jyoti Kashyap and Principal Shivani Singh who gave me the golden
opportunity to do this wonderful project on the topic (Java Programs), which also helped me
in doing a lot of Research and i came to know about so many new things I am really thankful
to them.
Secondly I would also like to thank my parents and friends who helped me a lot in finalizing
15 Rotate the matrix 90ᵒ clockwise and corner element sum 48-51
Test your program for the following data and some random data:
Example 1
INPUT : M = 3
4 16 12
8 2 14
4 1 3
OUTPUT :
ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3
12 16 4
14 2 8
3 1 6
Example 2
INPUT : M = 22
PROGRAM
import java.util.Scanner;
int i=0,j=0,m=0,row=0,col=0;
int arr[][],mirror[][];
System.out.print("M = ");
m = sc.nextInt();
else
arr[i][ j] = sc.nextInt();
System.out.println("ORIGINAL MATRIX");
System.out.print(arr[i][ j] + "\t");
System.out.println();
row = 0;
col = m - 1;
row = 0;
col--;
row++;
System.out.println("MIRROR MATRIX");
System.out.print(mirror[i][ j] + "\t");
System.out.println();
ALGORITHM
STEP 1 : Declare a square matrix A[][] of order (M X M) where 'M' is the number of rows and the number of
columns such that M must be greater than 2 and less than 20.
OUTPUT
INPUT : M = 3
4 16 12
8 2 14
4 1 3
OUTPUT :
ORIGINAL MATRIX
4 16 12
8 2 14
4 1 3
12 16 4
14 2 8
3 1 6
Program 2
A palindrome is a word that may be read the same way in either direction. Accept a sentence in UPPER CASE
which is terminated by either ".", "?", or "!". Each word of the sentence is separated by a single blank space.
Test your program with the sample data and some random data:
Example 1
Example 2
Example 3
PROGRAM
import java.util.Scanner;
String sen="",wd="",wd1="",palindromeWords="";
int i=0,len=0,count=0;
System.out.println("Enter a sentence");
sen = sc.nextLine();
sen=sen+" ";
sen=sen.toLowerCase();
len=sen.length();
for(i=0;i< len;i++)
ch=sen.charAt(i);
if(ch==' ')
if(wd.equals(wd1)==true)
palindromeWords+=wd1+" ";
count++;
wd1="";
wd="";
else
wd=wd+ch;
wd1=ch+wd1;
if(count>0)
System.out.println(palindromeWords);
else
ALGORITHM
STEP 1 : Accept a sentence in UPPER CASE which is terminated by either ".", "?", or "!".
OUTPUT
OUTPUT :
The input contains two positive integers m and n where m < 3000 and n < 3000. Display the number of
prime palindrome integers in the specified range along with their values in the format specified below:
Test your program with the sample data and some random data:
Example 1
INPUT:
m=100
n = 1000
OUTPUT:
THE PRIME PALINDROME INTEGERS ARE:
101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929
FREQUENCY OF PRIME PALINDROME INTEGERS : 15
PROGRAM
import java.util.Scanner;
int c = 0,i=0;
if(n % i == 0)
c++;
if(c > 2)
return false;
else
{
return true;
temp=n;
while(temp>0)
temp=temp/10;
if(rev==n)
return true;
else
return false;
int i=0,m=0,n=0,count=0;
System.out.print("m = ");
m =sc.nextInt();
System.out.print("n = ");
n = sc.nextInt();
{
System.out.println("OUT OF RANGE.");
return;
if(count == 0)
System.out.print(i);
else
count++;
System.out.println();
ALGORITHM
STEP 2 : Determine how many prime-palindrome integers are there in the range between m and n (both
inclusive) and output them.
STEP 3 : The input should contains two positive integers m and n where m < 3000 and n < 3000
STEP 4 : Display the number of prime palindrome integers in the specified range along with their values in the
format specified
OUTPUT
INPUT:
m = 100
n = 5000
OUTPUT: OUT OF RANGE
Program 4
Write a program to accept a sentence as input. The words in the string are to be separated by a blank. Each
word must be in upper case. The sentence is terminated by either '.','!' or '?'. Perform the following tasks:
Test your program with the sample data and some random data:
Example 1:
OUTPUT:
Length: 6
Rearranged Sentence:
Example 2:
OUTPUT:
Length: 4
PROGRAM
import java.util.Scanner;
import java.util.StringTokenizer;
int i=0,j=0,count=0,len=0;
String sen="",temp="",newsen="";
String wd[];
System.out.print("Sentence: ");
sen =sc.nextLine();
sen = sen.trim();
System.out.println("INVALID SENTENCE!");
return;
sen = sen.toUpperCase();
len = st.countTokens();
wd = new String[len];
wd[i] = st.nextToken();
wd[ j + 1] = temp;
ALGORITHM
STEP 3 : Each word must be in upper case. The sentence is terminated by either '.','!' or '?'.
OUTPUT
OUTPUT:
Length: 4
Find the maximum and minimum value in the matrix and display them along with their position.
Sort the elements of the matrix in ascending order using any standard sorting technique and rearrange
them in the matrix.
Example 1
INPUT:
M=3
N=4
OUTPUT:
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row=2
Column=3
Rearranged matrix:
-4 -2 0 1
3 3 4 5
6 7 8
PROGRAM
import java.util.Scanner;
public class ArraySort
int m = in.nextInt();
int n = in.nextInt();
if (m <= 2
|| m >= 10
|| n <= 2
|| n >= 10) {
return;
a[i][ j] = in.nextInt();
System.out.println("ORIGINAL MATRIX");
System.out.println();
int t = a[i][k];
a[i][k] = a[i][k+1];
a[i][k+1] = t;
System.out.println();
ALGORITHM
STEP 1 : Declare a matrix A [][] of order (MXN) where 'M' is the number of rows and 'N' is the number of
columns such that both M and N must be greater than 2 and less than 20.
STEP 4 : Find the maximum and minimum value in the matrix and display them along with their position.
STEP 5 : Sort the elements of the matrix in ascending order using any standard sorting technique and rearrange
them in the matrix.
OUTPUT
N=4
OUTPUT:
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row=2
Column=3
Rearranged matrix:
-4 -2 0 1
3 3 4 5
6 7 8
Program 6
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.
Find the number of vowels and consonants in each word and display them with proper headings along with
the words.
Example 1
OUTPUT:
Intelligence Plus Character Is Education
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 3
OUTPUT:
Invalid Input.
PROGRAM
import java.util.Scanner;
{
String sen="",wd="",upper="";
int i=0,len=0,vowelsFrequency=0,consonantsFrequency=0;
System.out.print("String: ");
sen = sc.nextLine();
sen = sen.trim();
len = sen.length();
System.out.println("Invalid input.");
else
System.out.println("Word\tVowels\tConsonants");
ch = sen.charAt(i);
wd=Character.toUpperCase(wd.charAt(0))+wd.substring(1).toLowerCase();
System.out.println(wd+"\t"+vowelsFrequency+"\t"+consonantsFrequency);
wd = "";
vowelsFrequency=0;
consonantsFrequency=0;
else
wd += ch;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
vowelsFrequency++;
else
consonantsFrequency++;
ALGORITHM
STEP 1 : Accept a sentence which may be terminated by either ‘.’ Or ‘?’ only.
STEP 3 : Print an error message if the input does not terminate with ‘.’ Or ‘?’
STEP 4 : Assume that no word in the sentence exceeds 15 characters, so that you get a proper formatted
output.
STEP 6 : Find the number of vowels and consonants in each word and display them with proper headings along
with the words.
STEP 7 : End
OUTPUT
OUTPUT:
Invalid Input
Program 7
A composite Magic number is a positive integer which is composite as well as a magic number.
Composite number: A composite number is a number which has more than two factors. For example: 10
Factors are: 1,2,5,10
Magic number: A Magic number is a number in which the eventual sum of the digit d is equal to 1. For
example: 28 = 2+8=10= 1+0=1
Accept two positive integers m and n, where m is less than n as user input. Display the number of composite
magic integers that are in the range between m and n (both inclusive) and output them along with frequency,
in the format specified below:
Example 1:
INPUT:
m = 10
n = 100
OUTPUT:
Example 2:
INPUT:
m = 1200
n = 1300
OUTPUT:
Example 3:
INPUT:
m = 120
n = 99
OUTPUT:
INVALID INPUT
PROGRAM
import java.util.Scanner;
int m = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
System.out.println("Invalid input");
return;
int count = 0;
if (i % j == 0) {
isComposite = true;
break;
if (isComposite && i != 1) {
int num = i;
int sum = 0;
while (num != 0) {
num /= 10;
sum += d;
num = sum;
}
if (num == 1) {
count++;
System.out.println();
ALGORITHM
STEP 1 : Accept two positive integers m and n, where m is less than n as user input.
STEP 4 : Display the number of composite magic integers that are in the range between m and n (both inclusive)
STEP 6 : End
OUTPUT
INPUT:
m = 120
n = 99
OUTPUT:
INVALID INPUT
Program 8
Write a program to declare a square matrix A[][] of order MXM where M is an positive integer and
represents row and column. M should be greater than 2 and less than 10.Accept the value of M from user.
Display an appropriate message for invalid input.
Check if the given matrix is symmetric or not. If the element of the ith row and jth column is same as
element of the jth row and ith column.
Find the sum of the left and right diagonal of the matrix and display them
Example 1
INPUT : M = 3
1 2 3
2 4 5
3 5 6
OUTPUT :
ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
Example 2
INPUT : M = 4
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
OUTPUT :
ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
Example 3
INPUT : M = 22
OUTPUT : THE MATRIX SIZE IS OUT OF RANGE
PROGRAM
import java.util.Scanner;
class Q2_2014
int m,a[][],rd,ld,z,k,x,t;
int i,j;
System.out.print("M=");
m=Sc.nextInt();
a=new int[m][m];
for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[i][ j]=Sc.nextInt();
display();
chk();
else
int i,j;
System.out.println("\nOriginal Matrix");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
System.out.println();
int i,j;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
if(a[i][ j] == a[ j][i])
z = z+1;
if(z ==(m*m))
else
sum();
int i,j;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
if(i==j)
ld+=a[i][ j];
System.out.println(ld);
for(i=0;i<m;i++)
for(j=0;j<m;j++)
if((i+j)==m-1)
rd+=a[i][ j];
System.out.print(rd);
}
class SymMatrix
ob.inp();
ALGORITHM
STEP 2 : M is an positive integer and represents row and column. M should be greater than 2 and less than 10.
STEP 6 : Check if the given matrix is symmetric or not. If the element of the ith row and jth column is same as
element of the jth row and ith column.
STEP 7 : Find the sum of the left and right diagonal of the matrix and display them
OUTPUT
INPUT : M = 4
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
OUTPUT :
ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
1. Accept the sentence and reduce all the extra blank space between two words to a single blank space.
2. Accept a word from the user which is part of the sentence along with its position number and delete the
word and display the sentence.
Test your program with the sample data and some random data.
Example 1
WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6
Example 2
WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 4
Example 3
PROGRAM
import java.util.Scanner;
int i=0,count=1,len=0,pos=0;
String sen="",newsen="",wd="",wd1="";
len = sen.length();
System.out.println("INVALID INPUT.");
return;
sen = sen.toUpperCase();
wd = sc.next();
wd = wd.toUpperCase();
pos = sc.nextInt();
for(i=0;i< len;i++)
ch=sen.charAt(i);
if(ch==' '||ch=='.'||ch=='?'||ch=='!')
/*do nothing*/
else
newsen=newsen+wd1+ch;
wd1="";
count++;
else
{
wd1=wd1+ch;
System.out.println("NEW SENTENCE:"+newsen);
ALGORITHM
STEP 2 : Iterate the array and check the word not equal to the given word.
STEP 3 : Concatenate the word into a new string array name as a new string.
OUTPUT
WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 4
Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is necessary to make
the last digit equal to ten; this is done by writing the last digit of the code as X. To verify an ISBN, calculate
10 times the first digit, plus 9 times the second digit, plus 8 times the third and so on until we add 1 time the
last digit. If the final number leaves no remainder when divided by 11, the code is a valid ISBN.
For example:
Design a program to accept a ten digit code from the user. For an invalid input, display an appropriate
message. Verify the code for its validity in the format specified below:
Test your program with sample data and some random data.
Example 1
OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE
PROGRAM
import java.util.Scanner;
while (isbn != 0) {
count++;
sum += d * m;
m--;
isbn /= 10;
if (count != 10) {
System.out.println("Illegal ISBN");
else if (sum % 11 == 0) {
System.out.println("Legal ISBN");
else {
System.out.println("Illegal ISBN");
ALGORITHM
STEP 3 : Verify the code for its validity in the format specified
OUTPUT
OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE
Program 11
A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost
digit is removed and replaced at the end of the remaining string of digits, the generated number is still
prime. The process is repeated until the original number is reached again.
Example:
131
311
113
Hence, 131 is a circular prime.
Test your program with the sample data and some random data:
Example 1
INPUT :N = 197
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME
Example 2
INPUT :N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME
Example 3
INPUT :N = 29
OUTPUT:
29
92
29 IS NOT A CIRCULAR PRIME
PROGRAM
import java.util.Scanner;
int num;
num = sc.nextInt();
num_of_digits++;
divisor_part *=10;
divisor_part /=10;
do{
int factorCount = 0;
else{
if( n %i == 0 ){
return false;
return true;
}
public static int circulate_func( int n, int divisor_part ){
ALGORITHM
STEP 4 : Chop the digits and check whether they are prime
STEP 6 : Chop the digits again and check whether they are prime numbers
STEP 7 : If original and modified and shifted numbers are prime, the number is circular prime number
STEP 8 : End
OUTPUT
INPUT :
N = 29
OUTPUT:
29
92
29 IS NOT A CIRCULAR PRIME
Program 12
Write a program to declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3 and less
than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:
Sort the non-boundary elements in ascending order using any standard sorting technique and rearrange
them in the matrix.
Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix.
Test your program with the sample data and some random data:
Example 1
INPUT :M = 4
OUTPUT:
ORIGINAL MATRIX
REARRANGED MATRIX
DIAGONAL ELEMENTS
9 5
3 6
8 13
7 8
PROGRAM
import java.util.Scanner;
int m = in.nextInt();
return;
a[i][ j] = in.nextInt();
/*
* should be enetered
*/
if (a[i][ j] < 0) {
System.out.println("INVALID INPUT");
return;
System.out.println("ORIGINAL MATRIX");
printMatrix(a, m);
sortNonBoundaryMatrix(a, m);
System.out.println("REARRANGED MATRIX");
printMatrix(a, m);
computePrintDiagonalSum(a, m);
/*
*/
int k = 0;
int t = b[ j];
b[ j] = b[ j+1];
b[ j+1] = t;
k = 0;
a[i][ j] = b[k++];
}
int sum = 0;
System.out.println("DIAGONAL ELEMENTS");
if (i == j || i + j == m - 1) {
System.out.print(a[i][ j] + "\t");
else {
System.out.print("\t");
System.out.println();
System.out.print(a[i][ j] + "\t");
System.out.println();
}
ALGORITHM
STEP 1 : Declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3
STEP 2 : Sort the non-boundary elements in ascending order using any standard sorting technique and
STEP 4 : Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix.
STEP 5 : End
OUTPUT
INPUT :M = 4
OUTPUT:
ORIGINAL MATRIX
REARRANGED MATRIX
Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they
occur in the sentence.
Test your program with the sample data and some random data:
Example 1
PROGRAM
import java.util.Scanner;
return true;
else
return false;
{
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return true;
else
return false;
int count=0,i=0,len=0;
String sen="",wd="",vowels="",nonVowels="";
System.out.print("Sentence: ");
sen = sc.nextLine();
sen = sen.trim();
sen = sen.toUpperCase();
len = sen.length();
System.out.println("INVALID INPUT");
return;
ch = sen.charAt(i);
if(ch==' '||ch=='.'||ch=='?'||ch==','||ch=='!')
{
if(wd.length() > 0 && WordCheck(wd)==true)
count++;
else
wd = "";
else
wd += ch;
System.out.println(vowels + nonVowels);
ALGORITHM
STEP 1 : A sentence which may be terminated by either '.', '?' or '!' only
STEP 2 : Words may be separated by more than one blank space and are in UPPER CASE
STEP 3 : Find the number of words beginning and ending with a vowel
STEP 4 : Place the words which begin and end with a vowel at the beginning, followed by the remaining words
as they occur in the sentence
STEP 6 : End
OUTPUT
Write a program to accept the numbers M and N from the user and print the smallest required number
whose sum of all its digits is equal to N. Also, print the total number of digits present in the required
number. The program should check for the validity of the inputs and display an appropriate message for an
invalid input.
Test your program with the sample data and some random data:
Example 1
INPUT :
M = 100
N = 11
OUTPUT :
The required number = 119
Total number of digits = 3
Example 2
INPUT :
M = 1500
N = 25
OUTPUT :
The required number = 1699
Total number of digits = 4
Example 4
INPUT :
M = 112
N = 130
OUTPUT :
INVALID INPUT
PROGRAM
import java.util.Scanner;
System.out.print("Enter m: ");
int m = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
System.out.println("Invalid Input");
return;
int sum = 0;
count = 0;
int t = i;
while (t != 0) {
int d = t % 10;
sum += d;
t /= 10;
count++;
if (sum == n) {
number = i;
break;
if (number == -1) {
else {
System.out.println("The required number = " + number);
ALGORITHM
STEP 1 : Enter two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100
STEP 4 : Find the smallest integer that is greater than M and whose digits add up to n
STEP 5 : Find the total number of digits present in the required number.
STEP 7 : End
OUTPUT
INPUT :
M = 99
N = 11
OUTPUT :
INVALID INPUT
Program 15
Write a program to declare a square matrix A[][] of order M×M where ‘M’ is the number of rows and the
number of rows and the number of columns, such that M must be greater than 2 and less than 10. Accept the
value M as user input. Display an appropriate message for an invalid input. Allow the user to input integers
into the matrix. Perform the following tasks:
1 2 3 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3
Find the sum of the elements of the four corners of the matrix.
Example 1
INPUT :
M = 3
OUTPUT :
ORIGINAL MATRIX
PROJECT
import java.util.*;
class Rotate90
{
public static void main(String arr[])
int m,a,b;
m=sc.nextInt();
for(a=0;a<m;a++)
for(b=0;b<m;b++)
matrix[a][b]=sc.nextInt();
for(a=0;a<m;a++)
for(b=0;b<m;b++)
System.out.print(matrix[a][b]+" ");
System.out.println();
for(a=0;a<m;a++)
for(b=0;b<m;b++)
System.out.print(matrix[a][b]+" ");
System.out.println();
ALGORITHM
STEP 2 : M is the number of rows and the number of rows and the number of columns, such that M must be
greater than 2 and less 10
STEP 8 : Find the sum of the elements of the four corners of the matrix.
STEP 9 : End
OUTPUT
INPUT :
M = 4
OUTPUT :
Example 1
Enter sentences:
TO BE OR NOT
OUTPUT:
WORD FREQUENCY
TO 1
BE 1
OR 1
NOT 1
PROGRAM
import java.util.*;
int n = in.nextInt();
in.nextLine();
if (n < 1 || n > 3) {
System.out.println("Invalid Entry");
return;
}
System.out.println("Enter sentences.");
ipStr = ipStr.toUpperCase();
System.out.println();
int idx = 0;
int j = 0;
if (wordArr[ j].equals(word)) {
wordFreq[ j]++;
break;
if (j == idx) {
wordArr[idx] = word;
wordFreq[idx]++;
idx++;
wordFreq[ j+1] = t;
String temp = wordArr[ j];
System.out.println("Word\tFrequency");
ALGORITHM
STEP 2 : The words are to be separated with single blank space and are in upper case
STEP 3 : A sentence may be terminated either with a full stop (.) or question mark (?).
STEP 4 : Enter the number of sentences, if it exceeds the limit show a message.
OUTPUT
OUTPUT:
WORD FREQUENCY
THIS 2
A 1
STRING 1
PROGRAM 1
EASY 1
YES 1
Program 17
A class Employee contains employee details and another class Salary calculates the employee’s net salary.
The details of the two classes are given below.
Data Members-:
Member Functions-:
Data Members-:
Member Functions-:
⮚ void calculate( ) - calculates the net salary according to the following rules-:
DA=10% of basic
HRA=15% of basic
Salary=basic+DA+HRA
PF=8% of Salary
Display the details of the class Employee and the net salary
Specify the class Employee giving the details of the functions using the concept of inheritance and also
specify the class in which object is created and functions are called in the main function
PROGRAM
import java.util.Scanner;
class Employee
Employee()
empno=0;
empname=null;
empdesig=null;
empno=a;
empname=b;
empdesig=c;
void display()
System.out.println("Employee name:"+empname);
System.out.println("Employee number:"+empno);
System.out.println("Employee designation:"+empdesig);
float basic;
super(n,name,d);
basic=b;
void calculate()
{
double da=0.1*basic,hra=0.15*basic,gross=basic+da+hra,pf=0.08*gross,netpay=gross-pf;
System.out.println("\nEmployee Details");
System.out.println("----------------");
super.display();
System.out.println("\nPayment Details");
System.out.println("----------------");
int n=ob.nextInt();
String d=ob.next();
call.calculate();
ALGORITHM
STEP 4 : Then similarly create a child class Salary and then use extends keyword and write the parent class
name
STEP 5 : Input the data members and perform the member functions as directed
OUTPUT
Enter employee name:abc
Employee Details
----------------
Employee name:abc
Employee number:12345
Employee designation:officer
Payment Details
----------------
Basic=50000.0
DA=5000.0
HRA=7500.0
Salary Pay=62500.0
Net Salary=57500.0
Program 18
Design a class Prime.
Data Members-:
Member Functions-:
PROGRAM
import java.util.Scanner;
private int n;
n = in.nextInt();
if (n == 0 || n == 1)
isPrime = false;
else {
if (n % i == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println("Prime Number");
else
obj.input();obj.checkprime(); }}
ALGORITHM
STEP 3 : Define a constructor check to to check if the no is prime or not by calling a function isPrime(int)
STEP 4 : Define a constructor is prime which returns true/false for prime using recursive technique
OUTPUT
Enter a number
23
23 is a prime number
Program 19
Design a class Stack-:
Data Members-:
⮚ int top
Member Functions-:
⮚ void pushvalues(int v) – to fill the stack if it is empty otherwise display message – “overflow”
⮚ int popvalues() – to delete value from the stack and return it, if possible otherwise display –
“underflow” and return -999
⮚ void display()
PROGRAM
import java.util.*;
class stack
int arr[];
int size;
int top;
stack(int s)
arr=new int[50];
size=s;
top=-1;
void pushvalue(int v)
if(top<(size-1))
arr[++top]=v;
else
System.out.println("Stack is full");
}
int popvalue()
if(top==-1)
{System.out.println("UNDERFLOW");
return-999;}
else
return arr[top--];
void display()
if(top==-1)
System.out.println("Stack is Empty");
else
for(int i=top;i>=0;i--)
System.out.println(arr[i]);
ALGORITHM
STEP 4 : Member function void pushvalues ( ) to insert the values in srack if empty
OUTPUT
enter size
50
enter 1 for popvalues and 2 for pushvalues
22
65
78
78
65
22
Program 20
Design a class SQ
Data Members-:
⮚ int front
⮚ int rear
Member Functions-:
⮚ void addrear(int v) – to add value at rear if possible otherwise display “Queue full at the rear end…
OVERFLOW”
⮚ int deletefront() – to delete value from front if possible otherwise display “Queue
empty…UNDERFLOW”
PROGRAM
import java.util.*;
class SQ
int size;
SQ(int s)
size=s;
front=rear=-1;
void addRear(int v)
if(rear<size-1)
arr[++rear]=v;
if(front==-1)front=0;
else
System.out.println("QUEUE IS FULL");
int deletefront()
return arr[front++];}
else
{System.out.println("Queue is empty...UNDERFLOW");
return -999;
void display()
if(front>rear)
else
for(int i=front;i<=rear;i++)
{System.out.println(arr[i]);}
ALGORITHM
STEP 2 : Add value at rear if possible otherwise display “Queue full at the rear end… OVERFLOW”
STEP 3 : Delete value from front if possible otherwise display “Queue empty…UNDERFLOW
ENTER A LIMIT: 3
VALUES:
Queue is empty….UNDERFLOW
Program 21
Write a program to input a natural number less than 1000 and display it in words. Test your program on the
sample data and some random data.
Examples
Input: 29
Input: 17001
Input: 119
Input: 500
PROGRAM
import java.io.*;
import java.lang;
int n;
System.out.println("\nINPUT:\t");
n=Integer.parseInt(inp.readLine());
String str=",str1=",str2=";
int hund=0,tens=0,units=0,i=0;
if(n<1 || n>999)
else
while(n!=0)
if(i==0)
units=n%10;
else if(i==1)
tens=n%10;
else if(i==2)
hund=n%10;
i++;
n=n/10;
if(hund>0)
if(tens>1)
str1= word2(tens);
if(tens==1)
str2= word3(units);
else
str2=word1(units);
if((!str.equals("))&&(!str1.equals(") || !str2.equals()))
if(!str1.equals(""))
if(!str2.equals(""))
str=str+ str2;
System.out.println("OUTPUT:\t"+str);
}//EndElse
}
String word1(int x)
String s="";
switch(x)
case 1:
s="ONE";
break;
case 2:
s="TWO";
break;
case 3:
s="THREE";
break;
case 4:
s="FOUR";
break;
case 5:
s="FIVE";
break;
case 6:
s="SIX";
break;
case 7:
s="SEVEN";
break;
case 8:
s="EIGHT";
break;
case 9:
s="NINE";
break;
return s;
String word2(int x)
String s=";
switch(x)
case 2:
s="TWENTY";
break;
case 3:
s="THIRTY";
break;
case 4:
s="FOURTY";
break;
case 5:
s="FIFTY";
break;
case 6:
s="SIXTY";
break;
case 7:
s="SEVENTY";
break;
case 8:
s="EIGHTY";
break;
case 9:
s="NINETY";
break;
return s;
String word3(int x)
String s="";
switch(x)
case 0:
s="TEN";
break;
case 1:
s="ELEVEN";
break;
case 2:
s="TWELVE";
break;
case 3:
s="THIRTEEN";
break;
case 4:
s="FOURTEEN";
break;
case 5:
s="FIFTEEN";
break;
case 6:
s="SIXTEEN";
break;
case 7:
s="SEVENTEEN";
break;
case 8:
s="EIGHTEN";
break;
case 9:
s="NINETEEN";
break;
return s;
obj.inputNumber();
obj.extract();
ALGORITHM
OUTPUT
Input: 29
Display an appropriate message if the size is not satisfying the given condition.
Define a string array of the inputted size and fill it with sentences row-wise. Change the sentence of the odd
rows with an encryption of two characters ahead of the original character.
Also change the sentence of the even rows by storing the sentence in reverse order. Display the encrypted
sentences as per the sample data given below.
Test your program on the sample data and some random data.
Example 1
INPUT: n = 4
IT IS CLOUDY.
IT MAY RAIN.
IT IS COOL.
OUTPUT:
KV KU ENQWFA.
COOL IS IT.
PROGRAM
import java.util.*;
class sentence
String str[],s;
int i,n;
String input()
return sc.nextLine();
}
void takeString()
n=sc.nextInt();
if(n<1||n>9)
System.out.println("INVALID ENTRY:");
return;
str=new String[n];
for(i=0;i<n;i++)
s=input();
if(s.charAt(s.length()-1)!='.')
i--;
continue;
str[i]=s;
for(i=0;i< n;i++)
s=str[i];
if(i%2==0)
encrypt(s);
else
reverse(s);
}
}
void encrypt(String s)
int i,len;
char ch;
String st="";
len=s.length();
for(i=0;i< len;i++)
ch=s.charAt(i);
ch=(char)(ch+2);
ch=(char)((64+ch-90));
ch=(char)((96+ch-122));
st=st+ch;
System.out.println(st);
void reverse(String s)
int i;
String s1;
s=s.trim();
s=s.substring(0,s.length()-1);
while(true)
i=s.lastIndexOf(" ");
if(i<0)
break;
s1=s.substring(i).trim();
s=s.substring(0,i).trim();
System.out.println(s+".");
ob.takeString();
ALGORITHM
STEP 1 : Enter a String array of size 'n' where 'n' is greater than 1 and less than 10, stores single sentences (each
sentence ends with a full stop) in each row of the array.
STEP 3 : Display an appropriate message if the size is not satisfying the given condition.
STEP 4 : Define a string array of the inputted size and fill it with sentences row-wise. STEP5:Change the
sentence of the odd rows with an encryption of two characters ahead of the original character.
STEP 6 : Change the sentence of the even rows by storing the sentence in reverse order.
STEP 7 : Display the encrypted sentences as per the sample data given below.
OUTPUT
INPUT: n = 4
IT IS CLOUDY.
IT MAY RAIN.
IT IS COOL
Program 23
Design a program which accepts your date of birth in dd mm yyyy format. Check whether the date entered is
valid or not.
If it is valid, display "VALID DATE", also compute and display the day number of the year for the date of birth.
If it is invalid, display "INVALID DATE" and then terminate the program.
Example 1
05
01
2010
5
Example 3
34
06
2010
PROGRAM
import java.util.*;
int day_num;
int yr;
int N;
do
{
System.out.println("Enter a day number (between 1 and 366)");
day_num = in.nextInt();
do
yr = in.nextInt();
if(Integer.toString(yr).length()!=4)
System.out.println("INVALID YEAR:");
while(Integer.toString(yr).length()!=4);
do
N = in.nextInt();
date.setDate(day_num,yr,N);
System.out.println("OUTPUT:");
date.dateSpecifier();
in.close();
class DaysCalculation
{
this.dayNumber = dayNumber;
this.year = year;
this.dayAfter = dayAfter;
int m = 0,k=1;
k = 1;
m++;
k++;
String prefix;
k = 1;
m++;
year++;
m = 0;
k++;
}
if(year%400==0)
leap=true;
else if (year%100==0)
leap=false;
else if (year%4==0)
leap=true;
else
leap=false;
return leap;
String[] months =
{"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","December"};
int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};
ALGORITHM
STEP 4 : Display the day number of the year for the date of birth.
03
04
2010
93
Program 24
A bank intends to design a program to display the denomination of an input amount, upto 5 digits. The
available denominations with the bank are of rupees 1000,500,100,50,20,10,5,2 and 1.
Design a program to accept the amount from the user and display the break-up in descending order of
denominations. (i.e. preference should be given to the highest denomination available) along with the total
number of notes. [Note: only the denomination used should be displayed]. Also print the amount in words
according to the digits.
Example 1
INPUT: 14836
DENOMINATION:
1000 X 14 =14000
500 X 1 =500
100 X 3 =300
50 X 1 =50
5 X 1 =5
1 X 1 =1
Example 2
INPUT: 235001
PROGRAM
import java.io.*;
{
int denomination[] = {1000, 500, 100, 50, 20, 10, 5, 2, 1};
String denomination1[] = {" Zero"," One", " Two", " Three", " Four", " Five", " Six",
amount=Integer.parseInt(br.readLine());
// Checking for the entered amount not more then 100000
{
return;
}
{
rev=rev*10+temp%10;
temp=temp/10;
}
while(rev!=0)
{
for(i=0;i<10;i++)
{
if(rem==i)
System.out.print(denomiation1[i]);
}
}
i = 0;
System.out.print("\n\nDENOMINATION:\n");
System.out.print("============:\n");
{
rev=amount/denomination[i];
if(rev!=0)
{
totalNotes+=rev;
}
i++;
}
}
ALGORITHM
STEP 1 : Design a program to display the denomination of an input amount, upto 5 digits.
STEP 2 : The available denominations with the bank are of rupees 1000,500,100,50,20,10,5,2 and 1.
STEP 4 : Display the break-up in descending order of denominations. (i.e. preference should be given to the
highest denomination available) along with the total number of notes.
OUTPUT
INPUT: 14836
DENOMINATION:
1000 X 14 = 14000
500 X 1 = 500
100 X 3 = 300
50 X 1 = 50
5X1 =5
1X1 =1
Program 25
Given the two positive integers p and q, where p < q. Write a program to determine how many kaprekar
numbers are there in the range between 'p' and 'q'(both inclusive) and output them.About 'kaprekar' number:
A positive whole number 'n' that has 'd' number of digits is squared and split into 2 pieces, a right hand piece
that has 'd' digits and a left hand piece that has remaining 'd' or 'd-1' digits. If sum of the pieces is equal to the
number then it's a kaprekar number.
Example 1
OUTPUT:
1,9,45,55,99,297,999
PROGRAM
import java.util.Scanner;
int p, q, h;
p = S.nextInt();
q = S.nextInt();
int count;
int flag = 0;
h = p;
while(h < q)
int y = h;
count = 0;
while(y != 0)
{
++count;
y = (int)y/10;
int m, n, w;
w = h*h;
n = (int)(w%Math.pow(10,count));
m = (int)(w/Math.pow(10,count));
if((n + m) == h)
System.out.println(h);
++flag;
++h;
ALGORITHM
STEP 2 : Determine how many kaprikar numbers are there in the range between 'p' and 'q'(both inclusive) and
output them.
OUTPUT
INPUT:
p=1
q=1000
OUTPUT:
1,9,45,55,99,297,999