Acknowledgement: Computer Project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 52

Acknowledgement

I would like to express my special thanks of gratitude to my


teacher Akash Sir as well as Senior Principal Mrs. Jyoti
Kashyap and our Principal Mrs 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 this project within the
limited time frame.

Computer Project

Name: Jyotik Banerji


Class: XII-E

Internal Examiner’s sign: __________


External Examiner’s sign: __________
Index

S.No. Program Page No.


1 Circular Prime Numbers
2 Boundary and Diagonals of a Square Matrix
3 Words beginning and ending with vowels
4 Print number between 100-1000
5 Rotate matrix
6 Print vowels/consonants
7 Magic number
8 Symmetrical array
9 Delete repeated word
10 Isbn number
11 Square matrix
12 Prime-adam number
13 Palindrome integer
14 Alphabetical order
15 Matrix array
16 Number in words
17 Encryption program
18 Birth date
19 Palindrome words
20 Kaprekar number
21 Frequency sentence
22 Ceaser cipher
23 Prime number
24 Highest capacity box
25 Goldbach number

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:

______ ______ ______ ______ ______ ______ ______ ______ ______

A1)
import java.util.Scanner;
public class CircularPrime
{
public static booleanisPrime(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; while (num != 0)
{ c++; num /= 10;
} return c; }
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("ENTER INTEGER TO CHECK (N): ");
int n = in.nextInt();
if (n <= 0)
{ System.out.println("INVALID INPUT");
return; }
booleanisCircularPrime = true; if (isPrime(n))
{ System.out.println(n);
int digitCount = getDigitCount(n);
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."); }
Else
{ System.out.println(n + " IS NOT A CIRCULAR PRIME.")
}}}
OUTPUT
ENTER INTEGER TO CHECK: 131
131
311
113
131 IS A CIRCULAR PRIME

____________________________________________________________________________

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:

______ ______ ______ ______ ______ ______ ______ ______ ______

A2)
import java.util.Scanner;
public class MatrixSort
{ public static void main(String args[])
{ Scanner in = new Scanner(System.in);
System.out.print("ENTER MATRIX SIZE (M): ");
int m = in.nextInt();
if (m <= 3 || m >= 10)
{
System.out.println("THE MATRIX SIZE IS OUT OF RANGE.");
return; }
int a[][] = new int[m][m];
System.out.println("ENTER ELEMENTS OF MATRIX");
for (int i = 0; i< m; i++)
{
System.out.println("ENTER ROW " + (i+1) + ":");
for (int j = 0; j < m; j++)
{
a[i][j] = in.nextInt();
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);
}

public static void sortNonBoundaryMatrix(int a[][], int m)


{
int b[] = new int[(m - 2) * (m - 2)];
int k = 0;
for (int i = 1; i< m - 1; i++)
{
for (int j = 1; j < m - 1; j++)
{
b[k++] = a[i][j]; }
}
for (int i = 0; i< k - 1; i++)
{
for (int j = 0; j < k - i - 1; j++)
{
if (b[j] >b[j + 1])
{
int t = b[j];
b[j] = b[j+1];
b[j+1] = t;
}
}
}
k = 0;
for (int i = 1; i< m - 1; i++)
{
for (int j = 1; j < m - 1; j++)
{ a[i][j] = b[k++]; }
}
}
public static void computePrintDiagonalSum(int a[][], int m)
{
int sum = 0;
System.out.println("DIAGONAL ELEMENTS");
for (int i = 0; i< m; i++)
{ for (int j = 0; j < m; j++)
{ if (i == j || i + j == m - 1)
{ sum += a[i][j];
System.out.print(a[i][j] + "\t"); }
else { System.out.print("\t");
}}
System.out.println(); }
System.out.println("SUM OF THE DIAGONAL ELEMENTS = " + sum); }
public static void printMatrix(int a[][], int m)
{
for (int i = 0; i< m; i++)
{ for (int j = 0; j < m; j++)
{ System.out.print(a[i][j] + "\t");
}
System.out.println(); }
}}

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    

SUM OF THE DIAGNOL ELEMENTS=59

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

______ ______ ______ ______ ______ ______ ______ ______ ______

A3)
import java.util.*;
public class VowelWord
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("ENTER THE SENTENCE:");
String ipStr = in.nextLine().trim().toUpperCase();
int len = ipStr.length();
char lastChar = ipStr.charAt(len - 1);
if (lastChar != '.' &&lastChar != '?' &&lastChar != '!')
{ System.out.println("INVALID INPUT");
return;
}
String str = ipStr.substring(0, len - 1);
StringTokenizerst = new StringTokenizer(str);
StringBuffersbVowel = new StringBuffer();
StringBuffer sb = new StringBuffer();
int c = 0; while (st.hasMoreTokens())
{
String word = st.nextToken();
int wordLen = word.length();
if (isVowel(word.charAt(0)) &&isVowel(word.charAt(wordLen - 1)))
{ c++;
sbVowel.append(word);
sbVowel.append(" "); }
else
{ sb.append(word); sb.append(" ");
}
}
String newStr = sbVowel.toString() + sb.toString();
System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = " + c);
System.out.println(newStr);
}
public static booleanisVowel(char ch)
{
ch = Character.toUpperCase(ch);
boolean ret = false; if (ch == 'A' || ch == 'E' || ch == 'I' || ch == ' O ' || ch == ' U ' )
ret = true ;
return ret;
}}

OUTPUT
ENTER THE SENTENCE:
YOU ARE WHAT YOU EAT

NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL= 1


ARE YOU WHAT YOU EAT

______________________________________________________________________________

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:

______ ______ ______ ______ ______ ______ ______ ______ ______

A4)
import java.util.*;
class Number
{
int sumDig(long n)
{
int sum = 0, d;
while(n>0)
{
d = (int)(n%10);
sum = sum + d;
n = n/10;
}
return sum;}
int countDig(long n)
{ String s = Long.toString(n);
int len = s.length(); return len; }
public static void main()throws Exception
{ questionob = new question();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a value of 'm' from 100 to 10000 : ");
int m = sc.nextInt();
System.out.print("Enter a value of n from 1 to 99 : ");
int n = sc.nextInt();
if(m10000 || n99)
{
System.out.println("Invalid Input");
}
else
{
long i = (long)m;
while(ob.sumDig(i)!=n)
{
i=i+1;
}
System.out.println("The required number = "+i);
System.out.println("Total number of digits = "+ob.countDig(i));
}
}}

OUTPUT
Enter a value of ‘m’ from 100 to 10000: 100
Enter a value of ‘n’ from 1 to 99: 20
The required number = 299
Total number of digits = 3

______________________________________________________________________________

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.

______ ______ ______ ______ ______ ______ ______ ______ ______


A5)
import java.util.*;
class mat
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
if(m<3 || m>9)
System.out.println("Size Out Of Range");
else
{
int A[][]=new int[m][m];
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}
System.out.println("The Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");

}
System.out.println();
}

int B[][]=new int[m][m];


int x;

for(int i=0;i<m;i++)
{
x = m-1;
for(int j=0;j<m;j++)
{
B[i][j]=A[x][i];
x--;
}
}
System.out.println("Matrix After Rotation is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding sum of corner
elements
System.out.println("Sum of the corner elements = "+sum);
}
}
}

OUTPUT

Enter The Size Of The Matrix: 4

The Original Matrix Is:

Matrix After Rotation Is:

Sum of the corner elements=18


_____________________________________________________________________________

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

A6)
import java.util.*;
class Count
{
public static void main(String arr[])
{
String str,str1="",word="";
int vowel=0,consonent=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter any String");
str=sc.nextLine();
if(!(str.endsWith(".")||str.endsWith("?")))
{
System.out.println("Invalid Input");
System.exit(0);
}
else
{
str=str.substring(0,str.length()-1);

for (int a=0;a<str.length();a++)


{
if(str.charAt(a)==' ')
{
str1=str1+' '+Character.toUpperCase(str.charAt(a+1));
a++;
continue;
}
str1=str1+str.charAt(a);
}
str=str1;
System.out.println(str);
}
System.out.println("Word\tVowels\tConsonants");
for (int a=0;a<str.length();a++)
{
if(str.charAt(a)==' ')
{
System.out.println(word+"\t"+vowel+"\t"+conso);
vowel=0;
consonent=0;

word="";
}
else
{
char ch=Character.toLowerCase(str.charAt(a));
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
vowel++;
}

else
{
consonent++;
}
word=word+str.charAt(a);
}
}
System.out.println(word+"\t"+vowel+"\t"+consonent);
}
}

OUTPUT
Enter any String
Music is the best healer
Music Is TheBest Healer
Word Vowels Consonants
Music 2 3
Is 1 1
The12
Best 1 3
Healer 3 3

______________________________________________________________________________

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

A7)
import java.util.*;
class MagicComposite
{
booleanisComposite(int n)
{
int count=0;
for(int i=1;i<=n;i++)
{
if(n%i==0) count++;
}
if(count>2)
return true;
else
return false;
}
int sumDig(int n)
{
int s = 0;
while(n>0)
{
s = s + n%10;
n = n/10;
}
return s;
}
booleanisMagic(int n)
{
int a = sumDig(n);
while(a>9)
{
a = sumDig(a);

}
if(a == 1)
return true;
else
return false;
}
public static void main(String args[])
{
MagicCompositeob = new MagicComposite();

Scanner sc=new Scanner(System.in);


System.out.print(“Enter the lower limit(m) : “);
int m=sc.nextInt();
System.out.print(“Enter the upper limit(n) : “);
int n=Sc.nextInt();
int c=0;
}
}
OUTPUT
Enter the lower limit(m): 1200
Enter the upper limit(n): 1300
The Composite Magic Integers are:
1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288
The frequency of Composite Magic Integers is: 9

______________________________________________________________________________

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

______ ______ ______ ______ ______ ______ ______ ______ ______

A8)
import java.util.*;
public class Symmetric
{
public static void main()
{
Scanner in=new Scanner(System.in);
int m,i,j,flag=0,ld=0,rd=0;
System.out.println("Enter size of matrix : ");
m=in.nextInt();
if((m>2)&&(m<10))
{
int a[][]=new int[m][m];
System.out.println("Enter Elements in the matrix : ");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=in.nextInt();
}
}
System.out.println("ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]!=a[j][i])
{
flag=1;
break;
}
}
}
if(flag==0)
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
for(i=0;i<m;i++)
{
ld=ld+a[i][i];
rd=rd+a[i][m-1-i];
}
System.out.println("The sum of the left diagonal : "+ld);
System.out.println("The sum of the right diagonal : "+rd);
}
else
System.out.println("THE MATRIX SIZE IS OUT OF RANGE");
}
}

OUTPUT

Enter size of matrix:22


THE MATRIX SIZE IS OUT OF RANGE
______________________________________________________________________________

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

A9)
import java.util.*;
class RemoveWord
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence : ");
String s = sc.nextLine();
s = s.toUpperCase();
int l = s.length();
char last = s.charAt(l-1);
if(last != '.' && last != '?' && last != '!')
{
System.out.println("Invalid Input. End a sentence with either '.', '?' or '!' only");
}
else
{
StringTokenizer str = new StringTokenizer(s," .?!");
int c = str.countTokens();
String w="",ans = "";
System.out.print("Enter the word to delete : ");
String del = sc.next();
System.out.print("Enter the word position is the sentence : ");
int x = sc.nextInt();
if(x<1 || x>c)
{
System.out.println("Sorry! The word position entered is out of range");
}
else
{
for(int i=1; i<=c; i++)
{
w = str.nextToken();
if(w.equals(del)==true &&i == x)
continue;
ans = ans + w + " ";
}
System.out.print("Output : "+ans.trim()+last);
}
}
}
}

OUTPUT
Enter a sentence
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.

______________________________________________________________________________

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
INPUT CODE: 0201530821
OUTPUT : SUM = 99
LEAVES NO REMAINDER – VALID ISBN CODE

______ ______ ______ ______ ______ ______ ______ ______ ______

A10)
import java.util.*;
class ISBN
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a 10 digit code : ");
String s=sc.nextLine();
int l=s.length();
if(l!=10)
System.out.println("Output : Invalid Input");
else
{
String ch;
int a=0, sum=0, k=10;
for(int i=0; i<l; i++)
{
ch=Character.toString(s.charAt(i));
if(ch.equalsIgnoreCase("X"))
a=10;
else
a=Integer.parseInt(ch);
sum=sum+a*k;
k--;
}
System.out.println("Output : Sum = "+sum);
if(sum%11==0)
System.out.println("Leaves No Remainder - Valid ISBN Code");
else
System.out.println("Leaves Remainder - Invalid ISBN Code");
}
}
}

OUTPUT

Enter a ten digit code: 0231428031


Output: Sum = 122
Leaves Remainder – Invalid ISBN Code

______________________________________________________________________________

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

A11)
import java.util.*;
class mirror
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the square matrix :");
int m=sc.nextInt();
if(m<2||m>20)
{
System.out.println("SIZE OUT OF RANGE");
}
else
{
int b[][]=new int[m][m];
int a[][]=new int[m][m];
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<m;i++)
{ int k=2;
for(int j=0;j<m;j++)
{
b[i][j]=a[i][k];
k--;
}
}
System.out.println("ORIGINAL MATRIX");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}

System.out.println("MIRRIOR IMAGE");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
}

OUTPUT
Enter the size of the square matrix : 4
Enter the elements of the Matrix:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The original matrix:
1234
5678
9 10 11 12
13 14 15 16

The Mirror Image:


4321
8765
12 11 10 9
16 15 14 13

______________________________________________________________________________

Question 12
A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as well as an
Adam number.

Prime number: A number that has only two factors, i.e. 1 and the number itself.Example: 2, 3, 5,
7 ... etc.

Adam number: The square of a number and the square of its reverse are reversed to each other.

Example: If n = 13 and reverse of 'n' = 31, then,

(13)2 = 169

(31)2 = 961 which is the reverse of 169

thus 13, is an Adam number.

Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam
integers that are in the range between m and n (both inclusive) and output them along with the
frequency, in the format given below:

Test your program with the following data and some random data:

Example
INPUT:

m=5

n = 100

OUTPUT:

THE PRIME-ADAM INTEGERS ARE:

11 13 31

FREQUENCY OF PRIME-ADAM INTEGERS IS: 3

______ ______ ______ ______ ______ ______ ______ ______ ______

A12)

import java.util.*;

public class pro1 {

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

static booleanPrime(int number) {

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

if (number % i == 0)

return false;

return true;

static int reverse(int number) {

String num = number + "";

String toReturn = "";

for (int i = num.length() - 1; i>= 0; i--)


toReturn += num.charAt(i);

return Integer.parseInt(toReturn);

static booleanAdam(int number) {

int reverse = reverse(number);

return (number * number == reverse(reverse * reverse));

public static void main(String[] args) {

System.out.println("Enter the lower limit:");

int m = sc.nextInt();

System.out.println("Enter the upper limit:");

int n = sc.nextInt();

if (m >= n) {

System.out.println("INVALID INPUT");

return;

System.out.println("THE PRIME-ADAM INTEGERS ARE:");

int count = 0;

for (int i = m; i<= n; i++) {

if (Prime(i) && Adam(i)) {

count++;

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

}
System.out.println();

System.out.print("FREQUENCY OF PRIME-ADAM INTEGERS IS: " + count);

OUTPUT:

Enter the value of m: 5

Enter the value of n: 100

THE PRIME ADAM INTEGERS ARE:

11 13 31

FREQUENCY OF PRIME-ADAM INTEGERS IS: 3

_________________________________________________________________________________

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

A13)
import java.io.*;
class PrimeP
{
public void showPrimePal() throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
int m,n, c=0;
System.out.println(“Enter the Lower Limit:”);
m=Integer.parseInt(br.readLine());
System.out.println(“Enter the Upper Limit:”);
n=Integer.parseInt(br.readLine());
if(m>n || n>3000)
System.out.println(“Out of Range.”);
else
{
System.out.println(“The Prime Palindrome integers are:”);
while(m<=n)
{
if(checkPrimePal(m))
{
if(c==0)
System.out.print(m);
else
System.out.print(“, “+m);
c++;
}
m++;
}
System.out.println(“\nFrequency of Prime Palindrome integers: “+c);
}
}
booleancheckPrimePal(int x)
{
boolean flag=false;
int c=0, temp=x;
int rev=0,rem=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
c++;

}
if(c<=2)
{
while(x!=0)
{
rem=x%10;
rev=rev*10+rem;
x/=10;
}
if(rev==temp)
flag=true;
}
return flag;
}
public static void main(String args[]) throws IOException
{
PrimePob=new PrimeP ();
ob.showPrimePal();
}
}

OUTPUT
Enter the Lower Limit:52
Enter the Upper Limit: 5500

Out of Range

______________________________________________________________________________

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

A14)
import java.io.*;
class WordsInSentence
{
public void take() throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
String str, words[], stk=””;
int i,j,c=0,flag, len;
char ch;
while(true)
{
flag=0;
System.out.println(“Enter the sentence:”);
str=br.readLine();
len= str.length();
words=new String[len];
for(i=0;i
{
if(Character.isLowerCase(str.charAt(i)))
{
flag=1;
break;
}
}
if (flag==0)
break;
else
System.out.println(“Enter the sentence again with all uppercase letters”);
}
i=0;
while(i<len)
{
ch=str.charAt(i);
if(ch==’ ‘|| ch==’.’ || ch==’?’ || ch==’!’)
{
words[c]=stk;
c++;
i++;
stk=””;
}
else

{
stk+=ch;
i++;
}
}
for(i=0;i<c;i++)
{
for(j=0;j<c-1;j++)
{
if((words[j].compareTo(words[j+1]))>0)
{
stk=words[j];
words[j]=words[j+1];
words[j+1]=stk;
}
}
}
System.out.println(“Length= “+c);
System.out.println(“\nRearranged Sentence:\n”);
for(i=0;i<c;i++)
System.out.print(words[i]+” “);
}
public static void main(String args[]) throws IOException
{
WordsInSentenceob=new WordsInSentence();
ob.take();
}
}

OUTPUT
Enter the sentence: ram plays with ball
Enter the sentence again with all uppercase letters RAM PLAYS WITH BALL
Length= 4
Rearranged Sentence: BALL PLAYS RAM WITH
_____________________________________________________________________________
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
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

______ ______ ______ ______ ______ ______ ______ ______ ______

A15)
import java.util.*;
class Matrix
{
public static void main(String args[])
{
int p=0,smallest=4999,larger=0,maxr=0,maxc=0,minr=0,minc=0;
Scanner sc=new Scanner(System.in);
System.out.println("enter m*n");
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
int b[]=new int[1000];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]<smallest)
{
smallest=a[i][j];
maxr=i;
maxc=j;
}
if(a[i][j]>larger)
{
larger=a[i][j];
minr=i;
minc=j;
}
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[p]=a[i][j];
p++;
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<p-i-1;j++)
{
if(b[j]>b[j+1])
{
int temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
p=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(b[p]+"\t");
p++;
}
System.out.println();
}
System.out.println("largest"+"="+larger);
System.out.println("Row : "+minr);
System.out.println("Column : "+minc);
System.out.println("smallest"+"="+smallest);
System.out.println("Row : "+maxr);
System.out.println("Column : "+maxc);
}
}

OUTPUT

Original matrix:
8  7  9  3
-2  0  4  5
1  3  6  -4
largest= 9
Row: 0
Column: 2
smallest= -4
Row=2
Column=3
Rearranged matrix:
-4-2  0  1
3  3  4  5
6  7 8  9

______________________________________________________________________________

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
Input: 500
Output: FIVE HUNDRED
______ ______ ______ ______ ______ ______ ______ ______ ______

A16)
import java.util.*;
class ISCprac2011q01{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);
int n;
System.out.println("Enter a number less than 1000");
n=scan.nextInt();
if(n>=1000)
{
System.out.println("OUT OF RANGE");
}else{
String result,h="",t="",o="";
int a,b,c;
String ones[]={"one", "two","three","four","five",
"six","seven","eight","nine"};
String teens[]={"eleven","twelve","thirteen","fourteen",
"fifteen","sixteen","seventeen","eighteen","nineteen"};
String tens[]={"ten","twenty","thirty","forty","fifty",
"sixty","seventy","eighty","ninety"};
if(n>=100 && n<1000){
a=n/100;
result=ones[a-1]+" hundred";
n=n%100;
}
if(n%10==0 && n!=0){
a=n/10;
if(result!=null){
result+=" and ";
result+=tens[a-1];
}else{
result=tens[a-1];
}
n=n%10;
}
if(n>20 && n<100){
a=n/10;
b=n%10;
if(result!=null){
result+=" and ";
result+=tens[a-1]+" "+ones[b-1];
}else{
result=tens[a-1]+" "+ones[b-1];
}
}
if(n>10 && n<20){
a=n%10;
if(result!=null){
result+=" and ";
result+=teens[a-1];
}else{
result=teens[a-1];
}
}
if(n<10 && n!=0){
if(result!=null)
{
result+=" and ";
result+=ones[n-1];
}else{
result=ones[n-1];
}
OR
a=n/100;
if(n>100){
n=n%100;
}
b=n/10;
c=n%10;
if(a!=0){
h=ones[a-1]+" hundred";
}

if(b==0 && c!=0){


o=ones[c-1];
}
else if(c==0 && b!=0){
t=tens[b-1];
}
else if(b==1){
t=teens[c-1];
}
else if(b!=0 && c!=0){
t=tens[b-1]+" "+ones[c-1];
}
if(b==0 && c==0)
result=h;
else if(a==0)
result=t+" "+o;
else
result=h+" and "+t+" "+o;
System.out.println("\n"+result.toUpperCase());
}
}
}

OUTPUT

Enter a number less than 1000


77
SEVENTY SEVEN
______________________________________________________________________________

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.
OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.
______ ______ ______ ______ ______ ______ ______ ______ ______

A17)
import java.io.*;
class ISCprac2011q02{
public static void main(String args[])
throws IOException{
BufferedReaderbr=new BufferedReader(
new InputStreamReader(System.in));
int nos;
System.out.print("Enter number of sentences : ");
nos=Integer.parseInt(br.readLine());
if(nos<1 || nos>=10)
System.out.println("\nInvalid Entry");
else{
int i,j,p,l;
String s[]=new String[nos];
System.out.print("\nEnter "+nos+" sentences : ");
for(i=0;i<nos;i++)
s[i]=(br.readLine()).toUpperCase();
for(i=0;i<nos;i++)
{
String t;
s[i]=" "+s[i];// add a blank space before each sentence
l=s[i].length();
if(i%2==0){
t="";
for(j=0;j<l;j++){
// store the ASCII code of the character
int ch=s[i].charAt(j);
if(ch!=32 &&ch!='.'){
ch=ch+2;//shift the letter two spaces
if(ch>90)//to maintain cyclic order
ch=ch-26;// subtract 26
}

//convert to character and add to a temporary string


t=t+(char)ch;
}
s[i]=t.trim();// remove leading or trailing spaces
}else{
t="";
p=l-1;
for(j=l-1;j>=0;j--){
//reverse loop to start extraction of words
//from last to first
char ch=s[i].charAt(j);
if(ch==' '){
t=t+s[i].substring(j+1,p)+" ";
// add the extracted word and a space
p=j;
}
}
t=t+".";
s[i]=t;
}
}
System.out.println("\nOUTPUT:");
for(i=0;i<nos;i++)
System.out.print(s[i]);
}
}
}

OUTPUT
Enter number of sentences:4
Enter sentences:
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.

OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.

______________________________________________________________________________

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

A18)
import java.util.*;
class DATE
{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your date of birth in
dd mm yyyyformat : ");
int d=scan.nextInt();
int m=scan.nextInt();
int y=scan.nextInt();
int dom[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(y%400==0 || (y%100!=0 && y%4==0))
dom[1]=29;
if(d<=dom[m-1])
{
System.out.print("VALID DATE ");
int i,s=0;
for(i=0;i< m-1;i++)
s=s+dom[i];
s+=d;
System.out.print(s);
}else{
System.out.print("INVALID DATE");
}
}
}

OUTPUT
Enter your date of birth in dd mm yyyy format
05
01
2010
VALID DATE
______________________________________________________________________________
Question 19

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 and are in uppercase.

Perform the following tasks:

(a) Check for the validity of the accepted sentence.

(b) Convert the non-palindrome words of the sentence into palindrome words by concatenating
the word by its reverse (excluding the last character).

Example:

The reverse of the word HELP would be LEH (omitting the last alphabet) and by concatenating
both, the new palindrome word is HELPLEH. Thus, the word HELP becomes HELPLEH.

Note: The words which end with repeated alphabets, for example ABB would become ABBA
and not ABBBA and XAZZZ becomes XAZZZAX.

[Palindrome word: Spells same from either side. Example: DAD, MADAM, etc.]

(c) Display the original sentence along with the converted sentence.

Test your program for the following data and some random data:

Example 1

INPUT:

THE BIRD IS FLYING.


OUTPUT:

THE BIRD IS FLYING.

______ ______ ______ ______ ______ ______ ______ ______ ______

A19)
import java.util.*;

public class pro5 {


String sentence;

Boolean Pallindrome(String word) {


String reverse = "";
for (int i = 0; i<word.length(); i++)
reverse = word.charAt(i) + reverse;
if (reverse.compareToIgnoreCase(word) == 0) {
return true;
}
return false;
}

void accept() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your sentence: ");
sentence = sc.nextLine();
if ("!.?".indexOf(sentence.charAt(sentence.length() - 1)) == -1) {
System.out.println("INAVLID INPUT");
return;
}
sentence = sentence.substring(0, sentence.length() - 1);
print();
}

String convert(String word) {


int count = 0;
if (!Pallindrome(word)) {
if (word.charAt(word.length() - 1) != word.charAt(word.length() - 2)) {
for (int i = word.length() - 2; i>= 0; i--)
word += word.charAt(i);
return word;
} else {
for (int i = word.length() - 1; i> 0; i--) {
if (word.charAt(i) == word.charAt(i - 1))
count++;
else {
for (int j = word.length() - 2 - count; j >= 0; j--)
word += word.charAt(j);
return word;
}
}
}

}
return word;
}

void print() {
StringTokenizerst = new StringTokenizer(sentence, " .");
int length = st.countTokens();
for (int i = 0; i< length; i++)
System.out.print(convert(st.nextToken())+" ");
}
public static void main(String[] args) {
pro5 o = new pro5();
o.accept();
}
}

OUTPUT:

ENTER THE SENTENCE:


THE BIRD IS FLYING.

THE BIRD IS FLYING.


THEHT BIRDRIB ISI FLYINGNIYLF
______________________________________________________________________________

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,9,45,55,99,297,999
FREQUENCY OF KAPREKAR NUMBERS IS:8
______ ______ ______ ______ ______ ______ ______ ______ ______

A20)
import java.util.*;
class ISCprac2010q02{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the range : ");
int p=scan.nextInt();
int q=scan.nextInt();
int d,i,n,a,b,s,freq;
freq=0;// to find the frequency of kaprekar numbers
System.out.println("The Kaprekar numbers are : ");
for(i=p;i<=q;i++)
{
n=i;
d=0;//to store the number of digits
//count the number of digits in the number
while(n>0){
d++;
n/=10;
}
s=i*i;// find the square of the number
//extract 'd' digits from the right of the square
//of the number
a=s%(int)Math.pow(10,d);
//extract 'd' or 'd-1' digits from the left of the square
//of the number
b=s/(int)Math.pow(10,d);
//Check if the two parts add up to the original number
//i.e. Condition for Kaprekar number
if(a+b==i){
System.out.print(i+" ");
freq++;
}
}
System.out.println("\nFREQUENCY OF KAPREKAR NUMBER IS : "
+freq);
}
}
OUTPUT
The Kaprekar numbers are:1,9,45,55,99,297,999
FREQUENCY OF KAPREKAR NUMBER IS: 8
______________________________________________________________________________

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
Enter sentences:
TO BE OR NOT TO BE.
OUTPUT:
Total number of words: 6
WORD FREQUENCY
OR 1
NOT 1
TO 2
BE 2
______ ______ ______ ______ ______ ______ ______ ______ ______

A21)
import java.util.*;
public class Para
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter number of sentences.");
int n = in.nextInt();
in.nextLine();

if (n < 1 || n > 3) {
System.out.println("Invalid Entry");
return;
}

System.out.println("Enter sentences.");
String ipStr = in.nextLine();
ipStr = ipStr.toUpperCase();
StringTokenizerst = new StringTokenizer(ipStr, " .?");
int wordCount = st.countTokens();

System.out.println();
System.out.println("Total number of words: " + wordCount);

String wordArr[] = new String[wordCount];


int wordFreq[] = new int[wordCount];
int idx = 0;

for (int i = 0; i<wordCount; i++) {

String word = st.nextToken();


int j = 0;
for (j = 0; j <idx; j++) {
if (wordArr[j].equals(word)) {
wordFreq[j]++;
break;
}
}
66

if (j == idx) {
wordArr[idx] = word;
wordFreq[idx]++;
idx++;
}
}

//Sort Word Frequency in ascending order


for (int i = 0; i<idx - 1; i++) {
for (int j = 0; j <idx - i - 1; j++) {
if (wordFreq[j] >wordFreq[j + 1]) {
int t = wordFreq[j];
wordFreq[j] = wordFreq[j+1];
wordFreq[j+1] = t;

String temp = wordArr[j];


wordArr[j] = wordArr[j+1];
wordArr[j+1] = temp;
}
}
}

//Display the words and frequencies


System.out.println("Word\tFrequency");
for (int i = 0; i<idx; i++) {
System.out.println(wordArr[i] + "\t" + wordFreq[i]);
}
}
}

OUTPUT
Enter number of sentences
1
Enter sentences
TO BE OR NOT TO BE.

Total number of words: 6

Word frequency
OR 1
NOT 1
TO 2
BE 2

______________________________________________________________________________

Question 22
Caesar Cipher is an encryption technique that is implemented as ROT13 ('rotate by 13 places'). It
is a simple letter substitution cipher that replaces a letter with the letter 13 places after it in the
alphabets, with the other characters remaining unchanged.
ROT13
A/a B/b C/c D/d E/e F/f G/g H/h I/i J/j K/k L/l M/m
↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕
N/n O/o P/p Q/q R/r S/s T/t U/u V/v W/w X/x Y/y Z/z

Write a program to accept a plain text of length L, where L must be greater than 3 and less than
100.
Encrypt the text if valid as per the Caesar Cipher.
Test your program with the sample data and some random data.
Example 1
INPUT:
Hello! How are you?
OUTPUT:
The cipher text is:
Uryyb! Ubjnerlbh?
______ ______ ______ ______ ______ ______ ______ ______ ______

A22)
import java.util.*;
public class pro11 {
String str;

void accept() {
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
if (str.length() > 100 || str.length() < 3) {
System.out.println("INVALID LENGTH");
return;
}
System.out.println(ROT13(str));
}

String ROT13(String str) {


String toReturn = "";
StringTokenizerst = new StringTokenizer(str);
while (st.countTokens() > 0) {
String word = st.nextToken();
for (int i = 0; i<word.length(); i++) {
if (Character.isLetter(word.charAt(i)))
toReturn += convert(word.charAt(i));
else
toReturn += word.charAt(i);
}
toReturn += " ";
}
return toReturn.trim();
}

char convert(char ch) {


if (Character.isLetter(ch + 13))
return (char) (ch + 13);
else
return (char) (ch - 13);
}

public static void main(String[] args) {


pro11 o = new pro11();
o.accept();
}
}

OUTPUT:

My name is Joe.
ZlanzrvfWbr.

______________________________________________________________________________

Question 23

Design a class Prime.


Data Members-:
 n - to store the number
Member Functions-:
 Prime(int) - constructor to initialize n
 void check( ) - to check if the no is prime or not by calling a function isPrime(int)
 booleanisPrime(int) - returns true/false for prime using recursive technique

______ ______ ______ ______ ______ ______ ______ ______ ______

A23)
import java.util.Scanner;
public class Prime
{
private int n;

public void input() {


Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
n = in.nextInt();
}

public void checkprime() {


booleanisPrime = true;
if (n == 0 || n == 1)
isPrime = false;
else {

for (int i = 2; i<= n / 2; i++) {


if (n % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime)
System.out.println("Prime Number");
else
System.out.println("Not a Prime Number");
}
public static void main(String args[]) {
Prime obj = new Prime();
obj.input();
obj.checkprime();}}
OUTPUT
Enter the number: 13
Prime Number
true

______________________________________________________________________________

Question 24
A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes, 12
boxes, 24 boxes, and 48 boxes. Design a program to accept the number of boxes to be packed
(N) by the user (maximum up to 1000 boxes) and display the break-up of the cartons used in
descending order of capacity (i.e. preference should be given to the highest capacity available,
and if boxes left are less than 6, an extra carton of capacity 6 should be used.)
Test your program with the following data and some random data:
Example 1
INPUT:
N = 726
OUTPUT:
48 * 15 = 720
6*1=6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16
______ ______ ______ ______ ______ ______ ______ ______ ______

A24)
import java.util.*;
public class pro9 {
int box, cartons_6, cartons_12, cartons_24, cartons_48, temp;

void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("ENTER NUMBER OF BOXES: ");
box = sc.nextInt();
temp = box;
if (box > 1000 || box < 1) {
System.out.println("INVALID INPUT");
return;
}
cartons();
print();
}

void cartons() {
cartons_48 = box / 48;
box %= 48;
cartons_24 = box / 24;
box %= 24;
cartons_12 = box / 12;
box %= 12;
cartons_6 = box / 6;
box %= 6;
}

void print() {
if (cartons_48 != 0)
System.out.println("48 * " + cartons_48 + " = " + (48 * cartons_48));
if (cartons_24 != 0)
System.out.println("24 * " + cartons_24 + " = " + (24 * cartons_24));
if (cartons_12 != 0)
System.out.println("12 * " + cartons_12 + " = " + (12 * cartons_12));
if (cartons_6 != 0)
System.out.println("6 * " + cartons_6 + " = " + (6 * cartons_6));
System.out.println("Remaining boxes = " + box);
System.out.println("Total number of boxes = " + temp);
System.out.println("Total number of cartons = " + (cartons_48 + cartons_24 + cartons_12 +
cartons_6));
}

public static void main(String[] args) {


pro9 o = new pro9();
o.accept();
}
}

OUTPUT:
ENTER NUMBER OF BOXES: 245
48 * 5 = 240
Remaining boxes = 5
Total number of boxes = 245
Total number of cartons = 5

______________________________________________________________________________

Question 25
A Goldbach number is a positive even integer that can be expressed as the sum of two odd
primes.
Note: All even integer numbers greater than 4 are Goldbach numbers.
Example:
6=3+3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3 and 7, 5
and 5.
Write a program to accept an even integer 'N' where N > 9 and N < 50. Find all the odd prime
pairs whose sum is equal to the number 'N'.
Test your program with the following data and some random data:
Example 1
INPUT:
N = 14
OUTPUT:
PRIME PAIRS ARE:
3, 11
7, 7
______ ______ ______ ______ ______ ______ ______ ______ ______

A25)
import java.util.*;

public class pro6 {


int number;

void accept() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
number = sc.nextInt();
if (number % 2 != 0){
System.out.println("INVALID INPUT. NUMBER IS ODD.");
return;
}
if (number < 9 || number >50){
System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
return;
}
goldbach();
}

booleanPrime(int number) {
if(number==1)
return false;
for (int i = 2; i< number; i++)
if (number % i == 0)
return false;
return true;
}

void goldbach() {
System.out.println("PRIME PAIRS ARE:");
for (int i = 0; i< number; i++)
if (Prime(i))
for (int j = i; j < number; j++)
if (Prime(j))
if ((i + j) == number)
System.out.println(i + "," + j);
}

public static void main(String[] args) {


pro6 o = new pro6();
o.accept();
}
}

OUTPUT
ENTER THE VALUE OF N: 14
PRIME PAIRS ARE:
3,11
7,7

_____________________________________________________________________________
_
COMPUTER PROJECT

ISC Class XII :2021-22

CMS Aliganj Campus 1

Name: JYOTIK BANERJI

Class: 12 - E

You might also like