Comp Proj
Comp Proj
Comp Proj
Acknowle
dgement
I would like to express my special thanks
of gratitude to my teacher Akash Sir as
well as our 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.
Computer Project
Class: XII-B
1
Internal
Examiner’s sign:
__________
External
Examiner’s sign:
__________
Index
S.No. Program Page No.
1 Circular Prime Numbers 4-5
2 Boundary and Diagonals of a Square Matrix 6-8
3 Vowel words and shift of words 9-10
4 Find number whose sum of all its digits is equal to N 11-12
5 Rotate the matrix 90ᵒ clockwise and corner element 13-14
sum
6 Find the number of vowels and consonants in each word 15-16
7 Number which is composite as well as a magic 17-18
number.
8 Check if the given matrix is symmetric or not. 19-21
1
Question 1
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.
A number is said to be prime if it has only two factors 1 and itself.
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
import java.util.Scanner;
class CircPrime
{
int n;
CircPrime(int nn)
{n=nn;}
int prime(int x)
{
1
int c=0;
for (int i=2;i<x;i++)
{
if(n%i==0)
{c++;}
}
if(c==0)return 1;
else return 0;
}
int rev(int x)
{
int c=0;int len=0;int a=n;
while(x!=n)
{
String str=(a%10)+""+(a/10);len=str.length();
int s=Integer.parseInt(str);x=s;a=s;
if(prime(s)==1)
{c++;}
}
if (c==len)
return c;
else return -1;
}
void disp()
{
int r=rev(0);
if (r==-1)
{System.out.println("It is Not a Circular Prime Number");}
else
{System.out.println("It is a Circular Prime Number");}
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int w=sc.nextInt();
CircPrime obj=new CircPrime(w);
obj.disp();
}
}
SR.NO FUNCTION PURPOSE
1) CircPrime(int nn) Constructor to assign initial values.
2) int rev(int x) To find the reverse of the given number.
3) void disp() Display function to show if it is circular prime or
not.
4) static void main() Main function to call the other functons.
1
Question 2
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:
1. Sort the non-boundary elements in ascending order using any standard sorting
technique and rearrange them in the matrix.
2. Calculate the sum of both the diagonals.
3. 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
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
OUTPUT:
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
REARRANGED MATRIX
9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8
DIAGONAL ELEMENTS
9 5
3 6
8 13
7 8
import java.util.*;8
class sort
{
int mat[][];
int m;
int n;
sort(int mm,int nn)
1
{ m=mm;
n=nn;
mat=new int[m][n];}
void input()
{ Scanner sc=new Scanner(System.in);
for (int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("Enter the number at ["+i+"]["+j+"]");
mat[i][j]=sc.nextInt();
}
}}
void sortarr()
{ int s=(m-2)*(n-2);
int arr[]=new int[s];int p=0;
for (int i=1;i<m-1;i++)
{
for (int j=1;j<n-1;j++)
{
arr[p]=mat[i][j];p++;
}
}
for (int k=0;k<s;k++)
{
for (int l=0;l<s-1;l++)
{
if(arr[l]>arr[l+1])
{
int temp=arr[l];
arr[l]=arr[l+1];
arr[l+1]=temp;
}
}
}
int q=0;
for (int c=1;c<m-1;c++)
{
for (int d=1;d<n-1;d++)
{
mat[c][d]=arr[q];
q++;
}}}
int calc()
{ int sum=0;
for (int i=0;i<m;i++)
{
for (int j=1;j<n;j++)
{
if((i==j)||(i==(n-j-1)))
{
1
sum=sum+mat[i][j];
}
}
}return sum;}
void display()
{ for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
if((i==j)||(i==(n-j-1)))
{
System.out.print(mat[i][j]+" ");
}
else
{System.out.print(" ");}
}System.out.println();
}System.out.println();
for (int c=0;c<m;c++)
{
for (int d=0;d<n;d++)
{
System.out.print(mat[c][d]+" ");
}System.out.println();
}System.out.println();}
static void main()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter no. of rows");
int r=sc.nextInt();
System.out.println("Enter no. of columns");
int c=sc.nextInt();
sort obj=new sort(r,c);
obj.input();
obj.display();
obj.sortarr();
obj.display();
}}
Question 3
Write a program to accept a sentence which may be terminated by either '.', '?' or
'!' only. The words may be separated by more than one blank space and are in
UPPER CASE.
Perform the following tasks:
1. Find the number of words beginning and ending with a vowel.
2. 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
Example 2
import java.util.*;
class sent
{
Scanner sc=new Scanner(System.in);
String str,newsent;int vow;
sent()
{
str="";
newsent="";
vow=0;
}
void input()
{
System.out.println("Enter a Sentence");
str=sc.nextLine();
}
void wordvow()
{
StringTokenizer ste=new StringTokenizer(str);
int cw=ste.countTokens();
1
while(ste.hasMoreTokens())
{
String st=ste.nextToken();
st=st.trim();
int len=st.length();
char ch=st.charAt(0);char cha=st.charAt(len-1);
if("AEIOUaeiou".indexOf(ch)>=0 && "AEIOUaeiou".indexOf(cha)>=0)
{
newsent=newsent+" "+st;vow++;
}
}
}
void wordcos()
{
StringTokenizer ste=new StringTokenizer(str);
int cw=ste.countTokens();
while(ste.hasMoreTokens())
{
String st=ste.nextToken();
int len=st.length();
char ch=st.charAt(0);char cha=st.charAt(len-1);
if("AEIOUaeiou".indexOf(ch)==-1 || "AEIOUaeiou".indexOf(cha)==-1)
{
newsent=newsent+" "+st;
}
}
System.out.println("NUMBER OF WORD BEGINNING AND ENDING WITH VOWEL "+vow);
System.out.println("NEW SENTENCE " +newsent);
}
static void main()
{
sent obj=new sent();
obj.input();
obj.wordvow();
obj.wordcos();
}
}
Question 4
Given two positive numbers M and N, such that M is between 100 and 10000
and N is less than 100. Find the smallest integer that is greater than M and
whose digits add up to N. For example, if M=100 and N=11, then the smallest
integer greater than 100 whose digits add up to 11 is 119.
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 3
INPUT :
M = 99
N = 11
OUTPUT :
INVALID INPUT
Example 4
INPUT :
M = 112
N = 130
OUTPUT :
INVALID INPUT
import java.util.Scanner;
1
class check
{
int M;
int N;int len;
check(int m,int n)
{
M=m;
N=n;
}
int sum(int n)
{
int s=0;len=0;
while (n>0)
{
s=s+(n%10);
n=n/10;len++;
}
return s;
}
void num()
{
for(int i=M;i<10000;i++)
{
if(sum(i)==N)
{
System.out.println("REQUIRED NUMBER IS= "+i);
System.out.println("Total number of digits= "+len);
break;
}
}
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of M");
int m=sc.nextInt();
System.out.println("Enter the value of N");
int n=sc.nextInt();
if (m<100 && m>10000 && n>100)
System.out.println("Invalid Input");
else
{
check obj=new check(m,n);
obj.num();
}
}
}
variables
2) int sum(int n) To find the sum of the given number
3) void num() To calculate and print the required number
4) static void main() Main to call the above functions.
Question 5
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 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. Display the original matrix.
2. Rotate the matrix 90ᵒ clockwise as shown below:
Original matrix Rotated matrix
1 2 3 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3
3. Find the sum of the elements of the four corners of the matrix.
Example 1
INPUT :
M = 3
OUTPUT :
ORIGINAL MATRIX
import java.util.Scanner;
class mat
{
int arr[][];
1
int M;
int N;
mat(int m)
{ M=N=m;
arr=new int[M][N];
}
void input()
{ Scanner sc=new Scanner(System.in);
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.println("Enter The value at ["+i+"]["+j+"]");
arr[i][j]=sc.nextInt();
}
}
}
void display()
{ for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
void turn90()
{ for(int i=0;i<M;i++)
{
for(int j=N-1;j>=0;j--)
{
System.out.print(arr[j][i]+" ");
}
System.out.println();
}
}
static void main()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter rows");
int m=sc.nextInt();
mat obj=new mat(m);
obj.input();
obj.display();
obj.turn90();
}}
Question 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.
Perform the following tasks:
1. Convert the first letter of each word to uppercase.
2. Find the number of vowels and consonants in each word and display them with
proper headings along with the words.
Test your program with the following inputs.
Example 1
INPUT: Intelligence plus character is education.
OUTPUT:
Intelligence Plus Character Is Education
WORD VOWELS CONSONANTS
Intelligence 5 7
Plus 1 3
Character 3 6
Is 1 1
Education 5 4
Example 2
INPUT: God is great.
OUTPUT:
God Is Great
WORDS VOWELS CONSONANTS
God 1 2
Is 1 1
Great 2 3
import java.util.*;
class string
{
String str;
string(String s)
{ str=s;}
1
int vowel(String s)
{ int c=0;
for (int i=0;i<s.length();i++)
if("AEIOUaeiou".indexOf(s.charAt(i))>=0)
c++;
return c;
}
int consonant(String s)
{ int c=0;
for (int i=0;i<s.length();i++)
if("AEIOUaeiou".indexOf(s.charAt(i))==-1)
c++;
return c;
}
void sep()
{
String stri="";
int len=str.length();
char c=str.charAt(len-1);
if(c=='.'||c=='?')
{ str=str.substring(0,len-1);
StringTokenizer st=new StringTokenizer(str);
System.out.println("WORD \t VOWEL \t CONSONANT");
while(st.hasMoreTokens())
{
String word=st.nextToken();
String ch=""+word.charAt(0);ch=ch.toUpperCase();
word=ch+(word.substring(1));
stri=stri+" "+word;
int vow=vowel(word);
int cons=consonant(word);
System.out.println(word+"\t "+vow+"\t "+cons);
} System.out.println(stri+c);
}
else System.out.println("Invalid Input");
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String s=sc.nextLine();
string obj=new string(s);
obj.sep();
}
}
Question 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:
THE COMPOSITE MAGIC INTEGERS ARE:
10, 28, 46, 55, 64, 82, 91, 100
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8
Example 2:
INPUT:
m = 1200
n = 1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE:
1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9
Example 3:
INPUT:
m = 120
n = 99
OUTPUT:
INVALID INPUT
1
import java.util.Scanner;
class comag
{
int M;int N;
comag(int m,int n)
{N=n;M=m;}
int composite(int n)
{ int c=0;
for(int i=2;i<n;i++)
{if(n%i==0)c++;}
if (c>0)
return 0;
else return -1;
}
int sum(int n)
{ int s=0;
while(n!=0)
{ s=s+(n%10);
n=n/10; }
return s;
}
void magic()
{
System.out.println("COMPOSITE NUMBERS ARE:-");
int c=0;
for(int i=M;i<=N;i++)
{int n=i;
while((n/10)!=0)
{
n=sum(n);
}
if(n==1 && composite(i)==0)
{System.out.print(i+",");c++;}
}
System.out.println();
System.out.println("FREQUENCY OF COMPOSITE NUMBER IS:-"+c);
}
static void main()
{ Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE LOWER LIMIT");
int m=sc.nextInt();
System.out.println("ENTER THE UPPER LIMIT");
int n=sc.nextInt();
if(n>m)
{comag obj=new comag(m,n);
obj.magic();}
else System.out.println("INVALID INPUT");
}
1
Question 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.
Perform the following task:
1. Display the original matrix
2. 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.
3. 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
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10
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
1
import java.util.Scanner;
class diagonal
{
int arr[][];
int size;
diagonal(int n)
{
size=n;
arr=new int[size][size];
}
void input()
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
System.out.println("Enter The value at ["+i+"]["+j+"]");
arr[i][j]=sc.nextInt();
}
}
}
int sumleft()
{
int s=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(i==j)
s=s+arr[i][j];
}
}return s;
}
int sumright()
{
int s=0;
1
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(i+j==size)
s=s+arr[i][j];
}
}return s;
}
void display()
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
System.out.print(arr[i][j]+" ");
}System.out.println();
}
}
void symmetric()
{
int c=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(arr[i][j]==arr[j][i])
c++;
}
}
if(c==(size*size))System.out.println("It Is a Symmetrical Matrix");
else System.out.println("It Is NOT a Symmetrical Matrix");
System.out.println("Sum Of Left Diagonal Is:-"+sumleft());
System.out.println("Sum Of Right Diagonal Is:-"+sumright());
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of matrix");
int n=sc.nextInt();
diagonal obj=new diagonal(n);
obj.input();
System.out.println("ORIGINAL MATRIX IS:-");
obj.display();
obj.symmetric();
}
}
Question 9
Write a program to accept a sentence which may be terminated by either '.', '?',
or '!' only. Any other character may be ignored. The words may be separated by
more than one blank space and are in UPPER CASE.
Perform the following tasks.
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
INPUT: A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.
WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6
OUTPUT: A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.
Example 2
INPUT: AS YOU SOW, SO SO YOU REAP.
WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 4
OUTPUT: AS YOU SOW, SO YOU REAP.
Example 3
INPUT: STUDY WELL ##
OUTPUT: INVALID INPUT
import java.util.*;
class upcase
{
String str;
int n;
String del;
1
upcase()
{ str="";
n=0;
del="";}
void accept()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter a Sentence");
str=sc.nextLine();
str=str.trim();
int len=str.length();
if((".?").indexOf(str.charAt(len-1))>=0)
{
System.out.println("Enter a word to be Deleted");
del=sc.nextLine();
System.out.println("Enter the position of word to be deleted");
n=sc.nextInt();
print();
}
else System.out.println("INVALID INPUT");
}
void print()
{ String sen="";
StringTokenizer st= new StringTokenizer(str);
int cw=st.countTokens();
for(int i=0;i<cw;i++)
{
String sent=st.nextToken();
int len=sent.length();
if(i!=(n-1))
{
sen=sen+" "+sent;
}
else
{
char ch=sent.charAt(len-1);
if(",".indexOf(ch)>=0)
{sen=sen+ch;}
}
}System.out.println("THE NEW SENTENCE IS:-"+sen);
}
static void main()
{ upcase obj=new upcase();
obj.accept();
}}
Question 10
An ISBN ( International Standard Book Number) is a ten digit code which
uniquely identifies a book. The first nine digits represent the Group, Publisher
and Title of the book and the last digit is used to check whether ISBN is correct
or not.
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:
1. 02011003311 = 10 x 0 + 9 x 2 + 8 x 0 + 7 x 1 + 6 x 1 + 5 x 0 + 4 x 3 + 3 x 3 + 2 x
1 + 1 x 1 = 55 Since 55 leaves no remainder when divisible by 11, hence it is a
valid ISBN.
2. 007462542X = 10 x 0 + 9 x 0 + 8 x 7 + 7 x 4 + 6 x 6 + 5 x 2 + 4 x 5 + 3 x 4 +
2 x 2 + 1 x 10 = 176 Since 176 leaves no remainder when divided by 11,
hence it is a valid ISBN.
3. 0112112425 = 10 x 0 + 9 x 1 + 8 x 1 + 7 x 2 + 6 x 1 + 5 x 1 + 4 x 1 + 3 x 4 +
2 x 2 + 1 x 5 = 71 Since 71 leaves no remainder when divided by 11, hence it
is not a valid ISBN.
Design a program to accept a ten digit code from the user. For an invalid inout,
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
INPUT CODE: 0201530821
OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE
1
Example 2
INPUT CODE: 035680324
OUTPUT : INVALID INPUT
Example 3
INPUT CODE: 0231428031
OUTPUT : SUM = 122
LEAVES REMAINDER – INVALID ISBN CODE
import java.util.*;
class isnb
{
String n;
isnb()
{n="";}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE ISBN CODE");
n=sc.next();
if((n.trim()).length()==10)
sum();
else
System.out.println("INVALID INPUT");
}
void sum()
{
int sum=0;
for(int i=9;i>=0;i--)
{
String ch=""+n.charAt(10-i-1);
if("xX".indexOf(ch)==-1)
{
int num=Integer.parseInt(ch);
sum=sum+((i+1)*num);
}
else
1
{
sum=sum+((i+1)*10);
}
}System.out.println("SUM :-\t"+sum);
if(sum%11==0)
System.out.println("LEAVES NO REMAINDER :- IT IS A VALID ISBN CODE");
else
System.out.println("LEAVES REMAINDER :- IT IS NOT A VALID ISBN CODE");
}
static void main()
{
isnb obj=new isnb();
obj.accept();
}
}
Question 11
Write a program to 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. Allow the user to input integers into this matrix. Display
appropriate error message for an invalid input. Perform the following tasks:
1. Display the input matrix.
2. Create a mirror image of the inputted matrix.
3. Display the mirror image matrix.
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
MIRROR IMAGE MATRIX
12 16 4
14 2 8
3 1 6
Example 2
INPUT : M = 22
1
import java.util.*;
class mat2
{
int M[][];
int si;
mat2()
{}
void display()
{ for(int i=0;i<si;i++)
{
for(int j=0;j<si;j++)
{
System.out.print(M[i][j]+"\t");
}
System.out.println();
}}
void accept()
{ Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE SIZE OF MATRIX");
si=sc.nextInt();
if(si<=20 && si>2)
{ M=new int[si][si];
for(int i=0;i<si;i++)
{
for(int j=0;j<si;j++)
{
System.out.println("ENTER THE INTEGER AT :- ["+i+"]["+j+"]");
M[i][j]=sc.nextInt();
}
}
System.out.println("ORIGINAL MATRIX IS:-");
display();
System.out.println();
System.out.println("MIRRORED MATRIX IS:-");
mirror();}
else
{System.out.println("OUT OF RANGE");}}
void mirror()
{
for(int i=0;i<si;i++)
{ for(int j=si-1;j>=0;j--)
{System.out.print(M[i][j]+"\t");}
System.out.println();
}}
1
Question 12
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.
Perform the following tasks:
1. Display the count of palindromic words in the sentence.
2. Display the palindromic words in the sentence.
Example of palindromic words:
MADAM, ARORA, NOON
Test your program with the sample data and some random data:
Example 1
INPUT : MOM AND DAD ARE COMING AT NOON.
OUTPUT : MOM DAD NOON
NUMBER OF PALINDROMIC WORDS : 3
Example 2
INPUT : NITIN ARORA USES LIRIL SOAP.
OUTPUT : NITIN ARORA LIRIL
NUMBER OF PALINDROMIC WORDS : 3
Example 3
INPUT : HOW ARE YOU?
OUTPUT : NO PALINDROMIC WORDS
1
import java.util.*;
class reverse
{
String sent;
reverse()
{ sent="";}
void input()
{ Scanner sc=new Scanner(System.in);
System.out.println("ENTER A SENTENCE");
sent=sc.nextLine();}
String rev(String s)
{
int l=s.length();
String str="";
for(int i=0;i<l;i++)
{
str=s.charAt(i)+""+str;
}
return str;
}
void palin()
{ int c=0;
StringTokenizer st=new StringTokenizer(sent);
int cw=st.countTokens();
int l=sent.length();
if(",.?!".indexOf(sent.charAt(l-1))>=0)
{
sent=sent.substring(0,l-1);
for(int i=0;i<cw;i++)
{
String sen=st.nextToken();
if(sen.equals(rev(sen)))
{
System.out.print(sen+"\t");
c++;
}
}
if (c!=0)
{System.out.println();
System.out.println("NUMBER OF PALIMNDROME NUMBERS ARE:-"+c);}
else{System.out.println("NO PALINDROMIC WORDS");}
}
else{System.out.println("INVALID INPUT");}}
static void main()
{ reverse obj=new reverse();
obj.input();
obj.palin();
1
}}
Question 13
A prime palindrome integer is a positive integer (without leading zeros) which
is prime as well as a palindrome. Given two positive integers m and n, where m
< n, write a program to determine how many prime-palindrome integers are
there in the range between m and n (both inclusive) and output them.
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
Example 2
INPUT:
m = 100
n = 5000
OUTPUT: OUT OF RANGE
1
import java.util.*;
class pripal
{
int m;
int n;
pripal()
{}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE LOWER LIMIT");
m=sc.nextInt();
System.out.println("ENTER THE LOWER LIMIT");
n=sc.nextInt();
}
boolean isprime(int n)
{
int c=0;
for(int i=2;i<n;i++)
{if(n%i==0)
c++;}
if(c==0)
return true;
else return false;
}
boolean palin(int n)
{
int i=n;int s=0;
while(n>0)
{
s=(s*10)+(n%10);
n=n/10;
}
if(s==i)return true;
else return false;
}
void check()
{
input();
if(m<n&&m<3000&&n<3000)
{int c=0;
System.out.println("THE PRIME PALINDROME NUMBERS ARE:-");
for(int i=m;i<=n;i++)
{
if(isprime(i) && palin(i))
{
System.out.print(i+",\t");
c++;
}
}System.out.println();
System.out.println("THE FREQUENCY OF PRIME PALINDROME NUMBERS is:- "+c);}
1
Question 14
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:
1. Obtain the length of the sentence (measured in words)
2. Arrange the sentence in alphabetical order of the words.
Test your program with the sample data and some random data:
Example 1:
INPUT: NECESSITY IS THE MOTHER OF INVENTION.
OUTPUT:
Length: 6
Rearranged Sentence:
INVENTION IS MOTHER NECESSITY OF THE
Example 2:
INPUT: BE GOOD TO OTHERS.
OUTPUT:
Length: 4
Rearranged Sentence: BE GOOD OTHERS TO
1
import java.util.*;
class arrange
{
String sent;
int cw;
arrange()
{
sent="";
cw=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Sentence");
sent=sc.nextLine();
}
void check()
{
StringTokenizer st=new StringTokenizer(sent);
cw=st.countTokens();
int arr[]=new int[cw];
String arr1[]=new String[cw];
for(int i=0;i<cw;i++)
{
String s=st.nextToken();
int l=s.length();
if(",.?!".indexOf(s.charAt(l-1))>=0)
{
s=s.substring(0,l-1);
}
arr1[i]=s;
char ch=s.charAt(0);
arr[i]=(int)ch;
}
for (int k=0;k<cw;k++)
{
for (int l=0;l<cw-1;l++)
{
if(arr[l]>arr[l+1])
{
int temp=arr[l];
arr[l]=arr[l+1];
arr[l+1]=temp;
String temp1=arr1[l];
arr1[l]=arr1[l+1];
arr1[l+1]=temp1;
}
}
}
System.out.println("LENGTH:- "+cw);
System.out.println();
1
Question 15
Write a program to 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. Allow the user to input integers into this
matrix.
Perform the following tasks on the matrix:
1. Display the input matrix
2. Find the maximum and minimum value in the matrix and display them along with
their position.
3. Sort the elements of the matrix in ascending order using any standard sorting
technique and rearrange them in the matrix.
Output the rearranged matrix.
Example 1
INPUT:
M=3
N=4
Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-4
OUTPUT:
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
1
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 9
import java.util.*;
class mat1
{
int mat[][];
int mat2[][];
int m;
int n;
mat1()
{}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Number of rows");
m=sc.nextInt();
System.out.println("Enter the Number of columns");
n=sc.nextInt();
mat=new int[m][n];
mat2=new int[m][n];
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
{ System.out.println("Enter the value at ["+i+"]["+j+"]");
mat[i][j]=sc.nextInt();
}
}
1
}
void check()
{
int s=m*n;int p=0;int max=0;int min=mat[0][0];
int arr[]=new int[s];int r1=0;int r2=0;int c1=0;int c2=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[p]=mat[i][j];p++;
if(mat[i][j]>max)
{max=mat[i][j];r1=i;c1=j;}
if(mat[i][j]<min)
{min=mat[i][j];r2=i;c2=j;}
}
}
for (int k=0;k<s;k++)
{
for (int l=0;l<s-1;l++)
{
if(arr[l]>arr[l+1])
{
int temp=arr[l];
arr[l]=arr[l+1];
arr[l+1]=temp;
}
}
}
System.out.println("LARGEST NUMBER :- "+max);
System.out.println("ROW :- "+r1+"\nCOLUMN "+c1);
System.out.println("SMALLEST NUMBER :- "+min);
System.out.println("ROW :- "+r2+"\nCOLUMN "+c2);
System.out.println("REARRANGED ARRAY IS:- ");
int q=0;
for (int x=0;x<m;x++)
{
for (int z=0;z<n;z++)
{
mat2[x][z]=arr[q];q++;
System.out.print(mat2[x][z]+"\t");
}
System.out.println();
}
}
void display()
{
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
{System.out.print(mat[i][j]+"\t");
}System.out.println();
}
1
}
static void main()
{
mat1 A=new mat1();
mat1 B=new mat1();
A.accept();
A.display();
A.check();
}
}
Question 16
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.
Sample input and output of the program
Examples
Input: 29
Output: TWENTY NINE
Input: 17001
Output: OUT OF RANGE
Input: 119
Output: ONE HUNDRED AND NINETEEN
import java.util.Scanner;
class toWord
{
int n;
String res;
toWord(int num)
{
n=num;
res="";
1
}
int count(int n)
{
int c=0;
while(n!=0)
{
n=n/10;
c++;
}return c;
}
String ones(int n)
{
String o[]={"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
return o[n];
}
String teens(int n)
{
String
teen[]={"ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTEEN
","NINETEEN"};
return teen[n-11];
}
String ten(int n)
{
String ten[]={"TWENTY","THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};
return ten[n-2];
}
void check()
{
if(n<1000)
{
int l=count(n);
if(l<=1)
{res=ones(n);}
else if(l>1 && n<20)
{res=teens(n);}
else if(l>1 && n>19 && n<100)
{
res=""+ten(n/10)+" "+ones(n%10);
}
else
{
if((n%100)<11 && (n%100)>0)
{res=ones(n/100)+" HUNDRED AND "+ones(n%100);}
else if((n%100)==0)
{res=ones(n/100)+" HUNDRED";}
else if((n%100)<20 && (n%100)>10)
{res=ones(n/100)+" HUNDRED AND "+teens(n%100);}
else
{res=ones(n/100)+" HUNDRED AND "+ten((n%100)/10)+" "+ones(n%10);}
}
1
System.out.println(res);
}
else
{System.out.println("OUT OF BOUND");
}
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER A NUMBER");
int number=sc.nextInt();
toWord obj=new toWord(number);
obj.check();
}
}
SR.NO FUNCTION PURPOSE
1) toWord(int num) Default constructor to initialize value to variable
2) int count(int n) To count the number of digits
3) String ones(int n) To find the number in words(1-10)
4) String ten(int n) To find the number in words(20,30,40…90)
5) String teens(int n) To find the number in words(11-19)
6) void check() To find the whole of the number in words
7) static void main() To call the above functions.
Question 17
Encryption is a technique of coding messages to maintain their secrecy. 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.
Write a program to accept the size of the array.
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.
THE WEATHER IS FINE.
IT IS COOL.
1
OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.
import java.util.*;
class code
{
String arr[];
int n;
code(int num)
{n=num;}
void accept()
{
Scanner sc=new Scanner(System.in);
arr = new String[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter the Sentence");
arr[i]=sc.nextLine();
}
}
String even(String s)
{
StringTokenizer st=new StringTokenizer(s);
String news="";
int cw=st.countTokens();
1
for(int i=0;i<cw;i++)
{
String sent=st.nextToken();
int l=sent.length();
if(".?".indexOf(sent.charAt(l-1))>=0)
{sent=sent.substring(0,l-1);}
news=sent+" "+news;
}return news;
}
String odd(String s)
{
String news="";
StringTokenizer st=new StringTokenizer(s);
int cw=st.countTokens();
for(int i=0;i<cw;i++)
{
String str=st.nextToken();
int l=str.length();
for(int j=0;j<l;j++)
{
if(".?".indexOf(str.charAt(l-1))>=0)
{str=str.substring(0,l-1);l--;}
int c=str.charAt(j)+2;
if(c>90)
{c=(c-90-1)+65;}
char ch=(char)c;
news=""+news+ch;
}
news=news+" ";
}return news;
}
void sentence()
{
for(int i=0;i<n;i++)
{
String newsent="";
String sent=arr[i];
if((i+1)%2==0)
{
newsent=newsent+" "+even(sent);
}
else
{
newsent=newsent+" "+odd(sent);
}
System.out.println(newsent);
}
}
static void main()
{
Scanner sc=new Scanner(System.in);
1
int i=1;
while (i==1)
{
System.out.println("ENTER THE SIZE OF ARRAY");
int si=sc.nextInt();
if(si>0 && si<=4)
{
code obj=new code(si);
obj.accept();
obj.sentence();
}
System.out.println("Press 1 to run the program again \npress 0 to stop");
i=sc.nextInt();
}
}
}
Question 18
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.
Testing of the program
Example 1
INPUT: Enter your date of birth in dd mm yyyy format
05
01
2010
OUTPUT: VALID DATE
5
Example 2
INPUT: Enter your date of birth in dd mm yyyy format
03
1
04
2010
OUTPUT: VALID DATE
93
Example 3
INPUT: Enter your date of birth in dd mm yyyy format
34
06
2010
OUTPUT: INVALID DATE
import java.util.*;
class date
{
int dd;
int mm;
int yyyy;
date()
{}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE DATE");
dd=sc.nextInt();
System.out.println("ENTER THE MONTH");
mm=sc.nextInt();
System.out.println("ENTER THE YEAR");
yyyy=sc.nextInt();
}
void check()
{
int arr[]={1,2,3,4,5,6,7,8,9,10,11,12};
int arr1[]={31,28,31,30,31,30,31,31,30,31,30,31};
int day=0;
if(mm<=12 && yyyy<=2022)
{
if(yyyy%4==0)
{arr1[1]=29;}
else
{arr1[1]=28;}
if(dd<=arr1[mm-1])
{
if(yyyy%4==0)
{arr1[2]=29;}
else
{arr1[2]=28;}
1
System.out.println("VALID DATE");
for(int i=0;i<mm-1;i++)
{
day=day+arr1[i];
}
day=day+dd;
System.out.println(day);
}
}
else
System.out.println("INVALID DATE");
}
static void main()
{
date obj=new date();
obj.accept();
obj.check();
}
}
SR.NO FUNCTION PURPOSE
1) date() Default constructor
2) void accept() To accept the date from user
3) void check() To check if the date is valid or not and find the
day of year
4) static void main() To call the above function by creating an object.
Question 19
A bank intends to design a program to display the denomination of an input
amount, upto 5 digits. The available denomination 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
OUTPUT: ONE FOUR EIGHT THREE SIX
DENOMINATION:
1000 X 14 =14000
500 X 1 =500
100 X 3 =300
1
50 X 1 =50
5 X 1 =5
1 X 1 =1
Example 2
INPUT: 235001
OUTPUT: INVALID AMOUNT
import java.util.*;
class bank
{
public static void main()
{
Scanner scan=new Scanner(System.in);
int amt;
System.out.print("Enter a five-digit amount : ");
amt=scan.nextInt();
if(amt>99999)
{
System.out.println("INVALID AMOUNT.");
}
else
{
int a[]={1000,500,100,50,20,10,5,2,1};int i,p,r,b,t;
p=amt;
for(i=0;i< a.length;i++)
{
t=amt/a[i];
1
if(t!=0)
{
System.out.println(a[i]+"X"+t+"="+(t*a[i]));
amt=amt%a[i];
}
}
String ones[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
r=0;
while(p>0)
{
r=r*10+p%10;
p/=10;
}
while(r>0)
{
b=r%10;
System.out.print(ones[b].toUpperCase()+" ");
r/=10;
}
}
}
}
SR.NO FUNCTION PURPOSE
1) public static void main() To find the denomination
2) String ones[] To store the digits in words
3)
4)
Question 20
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
INPUT:
p=1
q=1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:
1
1,9,45,55,99,297,999
FREQUENCY OF KAPREKAR NUMBERS IS:8
import java.util.*;
class kaper
{
int p;int q;
kaper()
{
p=0;q=0;
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE LOWER LIMIT");
p=sc.nextInt();
System.out.println("ENTER THE UPPER LIMIT:-");
q=sc.nextInt();
if(p<q)
{
check();
}
else
System.out.println("INVALID INPUT");
}
int digit(int n)
{
int c=0;
while(n>0)
{
n=n/10;
c++;
}return c;
}
void check()
{
int c=0;
System.out.println("THE KAPREKAR NUMBERS ARE:-");
for(int i=p;i<=q;i++)
{
int sq=i*i;
int d=digit(i);
int s=(int)Math.pow(10,d);
int sum=(sq%s)+(sq/s);
if (sum==i)
{
1
System.out.print(i+",\t");c++;
}
}System.out.println();
System.out.println("THE FREQUENCY OF KAPREKAR NUMBERS IS:-"+c);
}
static void main()
{
kaper obj=new kaper();
obj.accept();
}
}
SR.NO FUNCTION PURPOSE
1) kaper() Default constructor
2) void accept() To accept the lower ant the upper limit
3) int digit(int n) To find the number of digits
4) void check() To check if the number is kaprekar or not and
print
5) static void main() To call the above functions
Question 21
Input a paragraph containing 'n' number of sentences where (1<=n<=4). The
words are to be separated with single blank space and are in upper case. A
sentence may be terminated either with a full stop (.) or question mark (?).
Perform the following:
1. Enter the number of sentences, if it exceeds the limit show a message.
2. Find the number of words in the paragraph
3. Display the words in ascending order with frequency.
Example 1
INPUT: Enter number of sentences: 1
1
Enter sentences:
TO BE OR NOT TO BE.
OUTPUT:
Total number of words: 6
WORD FREQUENCY
OR 1
NOT 1
TO 2
BE 2
Example 2
INPUT: Enter number of sentences: 3
Enter sentences:
THIS IS A STRING PROGRAM. IS THIS EASY? YES, IT IS.
OUTPUT:
Total number of words: 11
WORD FREQUENCY
A 1
STRING 1
PROGRAM 1
EASY 1
YES 1
IT 1
THIS 2
IS 3
import java.util.*;
class wordsent
{
String sent;
int n;
int cw;
wordsent(int para,String s)
{
n=para;
sent=s;
}
int check(String s)
{
StringTokenizer st=new StringTokenizer(sent);
1
int c=0;
for(int j=0;j<cw;j++)
{
String sen=st.nextToken();
int l=sen.length();
if(Character.isDigit(sen.charAt(l-1))==false&&Character.isLetter(sen.charAt(l-1))==false)
{sen=sen.substring(0,l-1);}
if(s.equalsIgnoreCase(sen))
c++;
} return c;
}
void print()
{
System.out.println("ORIGINAL SENTENCE:-"+sent);
StringTokenizer st=new StringTokenizer(sent);
cw=st.countTokens();
System.out.println("TOTAL NUMBER OF WORDS ARE:-"+cw);
String arr[]=new String[cw];
int arr1[]=new int[cw];
for(int i=0;i<cw;i++)
{
String str=st.nextToken();
int l=str.length();
if(Character.isDigit(str.charAt(l-1))||Character.isLetter(str.charAt(l-1)))
{
arr[i]=str;
arr1[i]=check(str);
}
else
{
str=str.substring(0,l-1);
arr[i]=str;
arr1[i]=check(str);
}
}
for (int k=0;k<cw;k++)
{
for (int l=0;l<cw-1;l++)
{
if(arr1[l]>arr1[l+1])
{
int temp=arr1[l];
arr1[l]=arr1[l+1];
arr1[l+1]=temp;
String temp2=arr[l];
arr[l]=arr[l+1];
arr[l+1]=temp2;
}
}
}
for (int x=0;x<cw;x++)
1
{
for (int z=x+1;z<cw;z++)
{
if(arr[x].equals(arr[z]))
{
for (int m=z;m<cw-1;m++)
{
arr[m]=arr[m+1];
arr1[m]=arr1[m+1];
cw--;z--;
}
}
}
}
System.out.println("WORD \t FREQUENCY");
for(int r=0;r<cw-1;r++)
{System.out.println(arr[r]+"\t"+arr1[r]);}
}
}
Question 22
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.
Class name – Employee
Data Members-:
empno - to store employee no
empname - to store employee name
empdesign – to store designation
Member Functions-:
Employee( ) - default contructor
Employee(---) - parameterized constructor
void display( ) – to display data members with proper message
1
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
SUPER CLASS:-
import java.util.*;
class Employee
{
int empno;
String empname;
String empdesign;
Employee()
{}
Employee(String name, int no, String desig)
{
empno=no;
1
empname=name;
empdesign=desig;
}
void display()
{
System.out.println("NAME OF THE EMPLOYEE :- "+empname);
System.out.println("ROLL NO. OF THE EMPLOYEE :- "+empno);
System.out.println("DESIGNATION OF THE EMPLOYEE :- "+empdesign);
}
}
DERIVED CLASS:-
import java.util.*;
class Salary extends Employee
{
double basic;double net;
Salary()
{}
Salary(double ba, String name,String desig,int no)
{
super(name,no,desig);
basic=ba;net=0.0;
}
void calculate()
{
double da=(10*basic)/100;
double hra=(15*basic)/100;
double salary=basic+da+hra;
double pf=(8*salary)/100;
net=salary-pf;
}
void display()
{
super.display();
System.out.println("NET SALARY:- "+net);
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NAME");
String nm=sc.next();
1
Question 23
Design a class Prime.
Data Members-:
n - to store the number
Member Functions-:
Prime(int) - constructor to initialize n
1
import java.util.*;
class prime
{
int c=0;
int n;
prime(int num)
1
{n=num;}
boolean isPrime(int n,int i)
{
if(n<=2)
return (n==2)? true : false;
if(n%i==0)
return false;
if(i*i>n)
return true;
return isPrime(n,i+1);
}
void check()
{
if(isPrime(n,2))
System.out.println("IT IS A PRIME NUMBER");
else
System.out.println("IT IS NOT A PRIME NUMBER");
}
static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE VALUE TO BE CHECKED");
int numb=sc.nextInt();
prime obj=new prime(numb);
obj.check();
}
}
Question 24
Design a class Stack-:
Data Members-:
int arr[ ] – size 50
int size – store no of elements
1
int top
Member Functions-:
Stack(int s) – to initialize the size by s and top by -1
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()
import java.util.*;
class stack
{
int arr[];
int size;
int top;
stack(int s)
{
arr=new int[50];
1
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]);
}
}
}
SR.NO FUNCTION PURPOSE
1) stack(int s)
2) void pushvalue(int v)
3) int popvalue()
4) void display()
Question 25
Design a class SQ
Data Members-:
int arr[ ] – max size 100
int size – store the capacity of arr
1
int front
int rear
Member Functions-:
SQ(int s) – to initialize the size and front and rear by -1
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”
void display() – display elements of the queue
import java.util.*;
class SQ
{
int arr[]=new int[100];
int size;
int front;int rear;
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()
{
if(front<=rear && front!=-1)
{ System.out.println("Value Deleted "+arr[front]);
return arr[front++];}
else
{System.out.println("Queue is empty...UNDERFLOW");
return -999;
}
}
void display()
{
if(front>rear)
{System.out.println("NO VALUES TO BE PRINTED");}
else
{
1
for(int i=front;i<=rear;i++)
{System.out.println(arr[i]);}
}
}
}