12E Comp Project Final
12E Comp Project Final
PROJECT
SESSION : 2024-25
MADE BY:
ANSH TRIPATHI
XII-E
INTERNAL/EXTERNAL ASSESSMENT
PREFACE
In an age where computer plays a very significant role in the
working of the society, it is extremely important for every
1
computer science student to get as much practical knowledge as
possible.
Through this project, I have learned a lot about the practical use
of computer programming and it’s importance.
I am pleased to present this project on computer programming.
2
ACKNOWLEDGEMENT
The final outcome of this project required a lot of guidance and
support from many people.
First of all I would like to express my gratitude to Ms Kalpana
Tripathi, the Principal of the school for providing me the
opportunity to do this project.
I am extremely grateful to Mr Pankaj Chawla for providing great
help, encouragement and invaluable guidance on the project.
Last but not the least, the cooperation and contribution of my
parents and friends is sincerely appreciated and gratefully
acknowledged.
3
INDEX
Program Programs Page
Number Number
4
15 Prime Palindrome Number 38-39
16 Quiz 40-41
5
PROGRAM 1 :
Write a program to declare a square matrix a[][] of order (m × 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:
(a) Display the input matrix.
(b) Create a mirror image of the inputted matrix.
(c) Display the mirror image matrix.
1. Start
2. Import java.util package
3. Declare class MirrorMat
4. Declare public static void main
5. Make object sc of the Scanner class
6. Input int m from the user
7. Terminate program if m less than 3 or m greater than 19
8. Declare int i,j and a double dimension array a of size m*m
9. Input matrix elements from the user
10. Print original matrix
11. Print mirror matrix
7
12. End public static void main
13. End class
14. Stop
INPUT
M=3
4 16 12
8 2 14
6 1 3
OUTPUT
ORIGINAL MATRIX
4 16 12
8 2 14
6 1 3
12 16 4
14 2 8
3 1 6
8
PROGRAM 2:
Caesar Cipher is an encryption technique which 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.
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
9
{
System.out.println("INVALID
LENGTH"); return; }
String cipher = "";
for(int i = 0; i < L; i++) //Loop to form cipher
{
char ch = text.charAt(i);
if(Character.isLetter(ch))
{
if(Character.isUpperCase(ch))
{
if(ch > 'M')
cipher += (char)(ch - 13); else
cipher += (char)(ch + 13);
}
else {
if(ch > 'm')
cipher += (char)(ch - 13); else
cipher += (char)(ch + 13);
}
} else
cipher += ch;
}
System.out.println("The cipher text is:\n" + cipher);
} // end of psvm
} //end of class
ALGORITHM FOR PROGRAM 2
1. Start
2. Import java.util package
3. Declare class CaesarCipher
4. Declare public static void main
1
5. Make object sc of the Scanner class
6. Input string from the user and store in text
7. Store length of text in L
8. Terminate program if L less than 4 or m greater than 99
9. Initialize String cipher with default value
10. Make a for loop with condition (int i = 0; i < L; i++)
11. Extract each character in ch
12. Check if ch is a letter
13. Check ch is uppercase
14. Apply the Caesar Cipher condition
15. Print the Caesar Cipher text
16. End public static void main
17. End class
18. Stop
OUTPUT 1:
INPUT:
OUTPUT:
OUTPUT 2:
INPUT:
1
Encryption helps to secure data.
OUTPUT:
OUTPUT 3:
INPUT:
You
OUTPUT:
INVALID LENGTH
PROGRAM 3 :
Write a program to input a positive integer and check if it is a fascinating
number.
A fascinating number is one which when multiplied by 2 and 3, and then,
after the results are concatenated with the original number, the new
number contains all the digits from 1 to 9 exactly once.
1
public static void main(String args[]) //Start of psvm
{
Scanner sc=new Scanner (System.in); //Creating an object sc int
n,n2,n3,j;
System.out.print("N = "); //input from
user n = sc.nextInt(); n2 = n * 2;
n3 = n * 3; String con = n + "" + n2 + n3;
boolean found = true;
for(char i = '1'; i <= '9'; i++) //to check if Fascinating
{
int count = 0;
for(j = 0; j < con.length(); j++)
{
char ch = con.charAt(j);
if(ch == i)
count++;
}
if(count > 1 || count == 0)
{
found = false;
break;
}
}
if(found)
System.out.println(n + " is a fascinating number.");
else
System.out.println(n + " is not a fascinating number.");
} // end of psvm
} //end of class
1
1. Start
2. Import java.util package
3. Declare class Fascinating
4. Declare public static void main
5. Make object sc of the Scanner class
6. Declare int n,n2,n3,j
7. Input n from user
8. Make n2=n*2
9. Make n3=n*3
10. Make String con and concanicate n,n1,n2
11. Initialize Boolean found=true
12. Make a for loop with condition (char i = '1'; i <= '9'; i++)
13. Make int count=0
14. Make a for loop to check if the number is Fascinating. If count is greater
than 1 or 0 then make found=false.
15. If found= true then print (n + " is a fascinating number.") else (n + " is not a
fascinating number.");
16. End public static void main
17. End class
INPUT
192
OUTPUT:
1
PROGRAM 4:
Write a program in Java to create a two-dimensional array of size [n × n].
The value of n is entered by the user and make sure that 2 < n < 10. Now
fill the natural numbers into this matrix in a circular or spiral fashion
starting from 1 to n
{
System.out.println("Size out of
range!"); return; }
int a[][] = new int[n][n];
int value = n * n; int r1 =
n - 1; int r2 = 0;
int c1 = 0; int c2 = n - 1;
while(value >= 1) //Forming Spiral Matrix
{
for(int i = r1; i >= r2; i--)
a[i][c1] = value--; for(int i = c1
+ 1; i <= c2; i++) a[r2][i]
= value--; for(int i = r2 + 1; i <=
r1; i++) a[i][c2] =
value--;
1
for(int i = c2 - 1; i >= c1 + 1; i--)
a[r1][i] = value--;
r1--; r2++;
c1++; c2--
;
}
for(int i = 0; i < n; i++) //Loop to print spiral matrix
{
for(int j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
} // end of psvm
} //end of class
1. Start
2. Import java.util package
3. Declare class SpiralMat
4. Declare public static void main
5. Make object sc of the Scanner class
6. Input matrix size in n from the user
7. Terminate program if n less than 2 or greater than 3
8. Make int array a of size n*n
9. Make int r1 = n – 1, r2 = 0, c1 = 0, c2 = n – 1
10.Make loop to store spiral matrix
11.Print the spiral matrix
1
INPUT:
Matrix size = 5
OUTPUT:
21 20 19 18 17
22 7 6 5 16
23 8 1 4 15
24 9 2 3 14
25 10 11 12 13
PROGRAM 5 :
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. Accept
a positive number N and check whether it is a circular prime or not. The
new numbers formed after the shifting of the digits should also be
displayed.
1
{
if(!isPrime(n))
status = false;
System.out.println(n); s
= s.substring(1) + s.charAt(0);
n = Integer.parseInt(s);
}
if(status)
System.out.println(n + " IS A CIRCULAR PRIME.");
else
System.out.println(n + " IS NOT A CIRCULAR PRIME.");
}
public static boolean isPrime(int num){
int f = 0;
for(int i = 1; i <= num; i++)
if(num % i == 0)
f++;
return (f == 2); }
// end of psvm
} //end of class
1. Start
2. Import java.util package
3. Declare class CircularPrime
4. Declare public static void main
5. Make object sc of the Scanner class
6. Input number in int n from user
7. Convert n to string and store in s
8. Make Boolean status=true
1
9. Make a for loop with condition (int i = 1; i <= len; i++) to check if it is
circular prime number or not
10. If status = true, then print (n + " IS A CIRCULAR PRIME.") else print (n + "
IS NOT A CIRCULAR PRIME.");
11. Make public static boolean isPrime(int num) to check if num is prime or
not
12. End public static boolean isPrime(int num)
13. End public static void main
14. End class
15. Stop
INPUT:
N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME.
PROGRAM 6:
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 uppercase.
Perform the following tasks:
(a) Accept the sentence and reduce all the extra blank space between
two words to a single blank space.
1
(b) Accept a word from the user which is a part of the sentence along
with its position number and delete the word and display the sentence.
1
st = new StringTokenizer(t, " ?.!"); String
z = new String(); count =
st.countTokens();
for(int i = 1; i <= count; i++)
{
String w = st.nextToken();
if(word.startsWith(w) && pos == i)
z += w.replace(word, "");
else
z += w + " ";
}
z = z.trim(); //Deleting empty spaces after the sentence
z += last;
System.out.println(z);
} // end of psvm
} //end of class
INPUT:
OUTPUT:
2
PROGRAM 7:
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, 7 and 5, 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’.
2
if(isPrime(p) && isPrime(q) && p <= q)
System.out.println(p + ", " + q); p += 2;
}
}
public static boolean isPrime(int n){
int f = 0; for(int i =
1; i <= n; i++){ if(n % i
== 0) f++; }
if(f == 2) return true;
return false; } // end of
isPrime
} //end of class
INPUT:
N = 30
OUTPUT:
2
PROGRAM 8:
A Prime-Adam integer is a positive integer (without leading zeroes)
which is a prime as well as an Adam number.
Prime number: A number which 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
reverse to each other. Example: If n = 13 and reverse of ‘n’ is 31, then,
132 = 169, and 312 = 961 which is 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)
2
{
if(isPrime(i))
{
int rev = reverse(i);
int s1 = i * i; int s2 =
rev * rev;
if(reverse(s1) == s2)
{ if(count ==
0)
System.out.print(i);
else
System.out.print(", " +
i); count++; }
}
}
if(count == 0)
System.out.println("NIL"); else
System.out.println();
System.out.println("Frequency of Prime-Adam integers is: " + count);
}
public static boolean isPrime(int n) //To check prime
{ int f = 0; for(int
i = 1; i <= n; i++)
{
if(n % i == 0)
f++;
}
return f == 2;
}
public static int reverse(int n) //To reverse number
{
2
int rev = 0; for(int i =
n; i > 0; i /= 10) rev =
rev * 10 + i % 10; return
rev; } // end of isPrime
} //end of class
INPUT:
m = 100 n
= 200
OUTPUT:
PROGRAM 9:
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 that has more than
two factors.
Magic number: A magic number is a number in which the eventual sum
of the digits is equal to 1.
2
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).
2
return false;
}
public static void main(String []args) //Start of psvm
{
CompositeMagic ob=new CompositeMagic(); //Creating an object ob
Scanner sc=new Scanner(System.in); //Creating an object sc int
m,n,i,count=0; boolean comp,magic;
System.out.println("Enter m and n");
m=sc.nextInt();
n=sc.nextInt();
System.out.println("The Composite Magic Numbers are: ");
for(i=m;i<=n;i++) //Loop to check if number is Magic Composite
{
comp=ob.isComposite(i);
magic=ob.isMagic(i);
if(comp==true && magic==true)
{
System.out.print(i+", ");
count++;
}
}
System.out.println();
System.out.println("Number of Magic Composite numbers = "+count);
} // end of psvm
} //end of class
INPUT: m
= 1200 n
= 1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE:
1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288
2
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9
PROGRAM 10:
An n-digit number N is a Keith Number if it forms a Fibonacci-like
sequence that begins with the digits of the number itself. The most
significant digit is followed by the rest of the digits. Now each
subsequent term is the sum of the previous n terms, and the number N
itself appears in this sequence. Write a program to accept an integer and
check whether it is a Keith Number or not.
2
{
terms[i - 1] = terms[i];
}
terms[count - 1] = next;
next = 0;
for(int i = 0; i < count; i++)
next += terms[i];
}
if(next == n)
System.out.println(n + " is a Keith Number.");
else
System.out.println(n + " is not a Keith Number.");
}
public static int countDigits(long n) //To count number of digits in n
{
int count = 0;
while(n != 0){ count++;
n
/= 10;
}
return count; } //
end of countDigits
} //end of class
INPUT
N = 197
OUTPUT
2
197 is a Keith Number.
PROGRAM 11:
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
2
the highest capacity available, and if boxes left are less than 6, an extra
carton of capacity 6 should be used.)
{
System.out.println("INVALID INPUT");
}
else
{
int cart[] = {48, 24, 12, 6}; //Array to store types of cartons
int copy = N; int totalCart = 0,count = 0;
System.out.println("OUTPUT :"); for(int i=0; i<4; i++) //Loop
to calculate cartons in given format
{
count = N / cart[i];
if(count!=0) {
System.out.println("\t"+cart[i]+"\tx\t"+count+"\t= "+cart[i]*count);
}
totalCart = totalCart + count;
N = N % cart[i]; } if(N>0)
{
System.out.println("\tRemaining Boxes "+N+" x 1 = "+N);
totalCart = totalCart + 1;
3
} else
{
System.out.println("\tRemaining Boxes\t\t= 0");
}
System.out.println("\tTotal number of boxes = "+copy);
System.out.println("\tTotal number of cartons = "+totalCart);
}
} // end of psvm
} //end of class
INPUT
N = 140
OUTPUT :
48 X 2 = 96
24 x 1 = 24
12 x 1 = 12
6x1=6
Remaining boxes 2 x 1 = 2
Total number of boxes = 140
Total number of cartons = 6
3
PROGRAM 12:
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. Design a program to accept a ten digit code from the
user. For an invalid input, display an appropriate message.
3
System.out.println("INVALID
INPUT"); return; }
if(!(last >= '0' && last <= '9') && last != 'X')
{
System.out.println("INVALID INPUT");
return;
}
boolean wrong = false;
for(int i = 0; i < len - 1; i++)
{
char ch = isbn.charAt(i);
if(!(ch >= '0' && ch <= '9'))
{
wrong = true;
break;
}
else{
int d = Integer.parseInt(Character.toString(ch));
sum += d * n; n--;
}
}
if(wrong)
{
System.out.println("INVALID
INPUT"); return; }
if(last == 'X') sum
+= 10;
else
sum += Integer.parseInt(Character.toString(last));
System.out.println("SUM = " + sum);
if(sum % 11 == 0)
System.out.println("LEAVES NO REMAINDER - VALID ISBN CODE"); else
3
System.out.println("LEAVES REMAINDER - INVALID ISBN CODE");
} // end of psvm
} //end of class
INPUT
OUTPUT
SUM = 122
LEAVES REMAINDER – INVALID ISBN CODE
PROGRAM 13 :
Write a program in Java to allow the user to enter a sentence that may
only terminate with a ‘.’, ‘?’ or a ‘!’.
For an invalid sentence, display a suitable error message and exit the
program.
It is assumed that the words in the sentence are separated with a single
blank space.
Convert the sentence into uppercase.
Now arrange the words in the sentence in ascending order, based on
their potential.
The potential of a word is found by summing up the ASCII values of the
individual letters of the word.
Display the words in ascending order of their potential.
3
= 0; for(int i = 0; i <
w.length(); i++)
p += w.charAt(i); return p;
}
public static void main(String args[]) //Start of psvm
{
Scanner sc=new Scanner(System.in); //Creating an object sc
System.out.print("Sentence: ");
String s = sc.nextLine(); int len
= s.length(); char last = s.charAt(len
- 1);
if(last != '!' && last != '?' && last != '.')
{
System.out.println("Invalid
sentence!"); return; }
s = s.toUpperCase();
StringTokenizer st = new StringTokenizer(s, " !?.,");
int count = st.countTokens();
String words[] = new String[count]; int
p[] = new int[count]; for(int i =
0; i < count; i++)
{
words[i] = st.nextToken();
p[i] = potential(words[i]);
System.out.println(words[i] + " = " + p[i]);
}
for(int i = 0; i < count; i++)
{
for(int j = 0; j < count - 1 - i; j++)
{
if(p[j] > p[j + 1])
{
3
int temp = p[j];
p[j] = p[j + 1]; p[j + 1] = temp;
String t =
words[j]; words[j] =
words[j + 1];
words[j + 1] = t;
}
}
}
for(int i = 0; i < count; i++)
System.out.print(words[i] + " "); System.out.println();
} // end of psvm
} //end of class
INPUT HOW DO YOU
DO?
OUTPUT
HOW = 238
DO = 147
YOU = 253
DO = 147
DO DO HOW YOU
PROGRAM 14:
Design a program to accept a day number (between 1 and 366), year (in
4 digits) from the user to generate and display the corresponding date.
Also, accept N (1 <= N <= 100) from the user to compute and display the
future date corresponding to ‘N’ days after the generated date. Display
an error message if the value of the day number, year and N are not
within the limit or not according to the condition specified.
3
import java.util.*; //importing java.util package class Day
//Declaring class
{
public static void main(String args[]) //Start of psvm
{
Scanner sc=new Scanner(System.in); //Creating an object sc int
n, day, year, i, p, k=0;
int ar[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
String br[]={"", "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"}; //To store months
System.out.print("Day number: "); //Input from user day = sc.nextInt();
System.out.print("Year: ");
year = sc.nextInt();
System.out.print("Date after(N
days): "); n = sc.nextInt(); p
= day + n;
int a = 1, b = 1, date = 0, nday = 0, year1 = year;
if(year % 4 == 0)
{
for(i = 2; i <= 12; i++)
{
ar[i] += 1;
}
if(day >= 1 && day <= 366)
{
for(i = 0; i <= 12; i++)
{
if(ar[i] < day)
a = i; if(ar[i] >
day) break;
}
date = day - ar[a];
3
} else
k
= 1; if(k
== 1)
System.out.println("Days are out of Range");
else if(k != 1 && n >= 1 && n <= 100){ if(p >
ar[12]) {
p = p - ar[12]; year1
= year1 + 1;
}
for(i = 0; i <= 12; i++)
{
if(ar[i] < p)
b = i; if(ar[i]
> p)
break;
}
nday = p - ar[b];
} else
{
k = 1;
System.out.println("N Days are out of range ");
}
if(k != 1)
{
System.out.println("Date is : " + date + "th " + br[a + 1] + " " + year);
System.out.println("N Date is : "+ nday + "th " + br[b + 1] + " " + year1);
}
}
else{
if(day >=1 && day <= 365){
for(i = 0; i <= 12; i++){
3
if(ar[i] < day) a = i;
if(ar[i] > day)
break;
}
date = day - ar[a];
} else
k
= 1; if(k
== 1)
System.out.println("Days are out of Range");
if(n >= 1 && n <= 100){
if(p > ar[12]){ p=
p - ar[12]; year1 =
year1 + 1;
}
for(i = 0; i <= 12; i++){
if(ar[i] < p){
b = i;
} if(ar[i]
> p)
break;
}
nday = p - ar[b];
}
else{ k
= 1;
System.out.println("N days are out of range ");
}
if(k != 1){
System.out.println("Date is : "+date+"th " + br[a+1]+" "+year);
System.out.println("N Date is : "+nday+"th " + br[b+1]+" "+year1);
}
3
}
} // end of psvm
} //end of class Output
1:
INPUT:
DAY NUMBER: 255
YEAR: 2018
DATE AFTER (N DAYS): 22 OUTPUT:
DATE: 12TH SEPTEMBER, 2018
DATE AFTER 22 DAYS: 4TH OCTOBER, 2018
OUTPUT 2:
INPUT:
DAY NUMBER: 360
YEAR: 2018
DATE AFTER (N DAYS): 45 OUTPUT:
DATE: 26TH DECEMBER, 2018
DATE AFTER 45 DAYS: 9TH FEBRUARY, 2019
3
OUTPUT 3:
INPUT:
DAY NUMBER: 500
YEAR: 2018
DATE AFTER (N DAYS): 33 OUTPUT:
DAY NUMBER OUT OF RANGE
OUTPUT 4:
INPUT:
DAY NUMBER: 150
YEAR: 2018
DATE AFTER (N DAYS): 330 OUTPUT:
DATE AFTER (N DAYS) OUT OF RANGE
PROGRAM 15:
A prime palindrome integer is a positive integer (without leading
zeroes) 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 .
4
public static void main(String args[]) //Start of psvm
{
Scanner sc=new Scanner(System.in); //Creating an object sc
System.out.print("m = "); int m = sc.nextInt(); System.out.print("n = ");
int n = sc.nextInt();
if(m > 3000 || n > 3000 || m > n) //To terminate program if invalid input
{
System.out.println("OUT OF RANGE.");
return; }
int count = 0;
System.out.println("THE PRIME PALINDROME INTEGERS ARE:"); for(int
i = m; i <= n; i++) //Loop to print Prime Palindromes
{
if(isPalindrome(i) && isPrime(i))
{
if(count == 0)
System.out.print(i);
else
System.out.print(", " + i); count++;
}
}
System.out.println("\nFREQUENCY OF PRIME PALINDROME INTEGERS: " +
count);
}
public static boolean isPalindrome(int n) //Function to check palindrome
{
int rev = 0; for(int i =
n; i > 0; i /= 10) rev =
rev * 10 + i % 10; return
n == rev;
}
public static boolean isPrime(int n) //Function to check prime
4
{ int f = 0; for(int
i = 1; i <= n; i++) {
if(n % i == 0)
f++; if(f > 2)
return false;
}
return f == 2;
} // end of isPrime
} //end of class
INPUT:
m = 100
n = 1000
OUTPUT:
PROGRAM 16 :
The result of a quiz competition is to be prepared as follows:
The quiz has five questions with four multiple choices (A, B, C, D), with
each question carrying 1 mark for the correct answer. Design a program
to accept the number of participants N such that N must be greater than
3 and less than 11. Create a double-dimensional array of size (N * 5) to
4
store the answers of each participant row-wise. Calculate the marks for
each participant by matching the correct answer stored in a
singledimensional array of size 5. Display the scores for each participant
and also the participants(s) having the highest score.
{
System.out.println("INPUT SIZE OUT OF RANGE.");
return; }
char q[][] = new char[n][5];
char a[] = new char[5]; int score[]
= new int[n];
System.out.println("Key to the questions:");
for(int i = 0; i < a.length; i++)
a[i] = sc.nextLine().charAt(0);
System.out.println("Answers by participants:");
for(int i = 0; i < n; i++)
{
System.out.println("Participant " + (i + 1));
for(int j = 0; j < 5; j++)
{
4
q[i][j] = sc.nextLine().charAt(0);
if(q[i][j] == a[j]) score[i]++;
}
if(highest < score[i])
highest = score[i];
}
for(int i = 0; i < n; i++)
System.out.println("Participant " + (i + 1) + " = " + score[i]);
System.out.println("Highest score(s):");
for(int i = 0; i < n; i++)
if(score[i] == highest)
System.out.println("Participant " + (i + 1));
} // end of psvm
} //end of class
INPUT:
N=4
Participant 1 A C C B D Participant
2BCAAC
Participant 3 B C B A A
Participant 4 C C D D B
Key: A C D B B
OUTPUT:
Scores:
Participant 1 = 3
Participant 2 = 1
4
Participant 3 = 1 Participant
4 = 3 Highest score:
Participant 1
Participant 4
PROGRAM 17:
Write a program to input a natural number less than 1000 and display it
in words.
4
int d = n / 100;
n %= 100;
words += ones[d] + " hundred ";
}
if(n > 19 || n == 10) //For tens
{
int d = n / 10;
n %= 10;
words += tens[d] + " ";
}
if(n > 10)
{
int d = n % 10; n
/= 10; if(threeDigit)
//For teens
words += "and " + teens[d];
else words
+= teens[d];
}
else if(n > 0)
{
if(threeDigit) //For unit digits
words += "and " + ones[n];
else words
+= ones[n];
}
words = words.toUpperCase();
System.out.println(words);
} // end of psvm
} //end of class
4
INPUT
29
OUTPUT
TWENTY NINE
PROGRAM 18:
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:
(a) Find the number of words beginning and ending with a vowel. (b)
Place the words which begin and end with a vowel at the beginning,
followed by the remaining words as they occur in the sentence.
4
char ch1 = w.charAt(0); // Storing the first character
char ch2 = w.charAt(l-1); // Storing the last character
if("AEIOUaeiou".indexOf(ch1)>=0 && "AEIOUaeiou".indexOf(ch2)>=0)
{
return true;
}
else
{
return false;
}
}
public static void main(String args[]) //Start of psvm
{
BeginEndVowel ob = new BeginEndVowel();
Scanner sc = new Scanner(System.in); //Creating an object sc
System.out.print("Enter a sentence :
"); String s = sc.nextLine(); s=
s.toUpperCase(); int l = s.length();
char last = s.charAt(l-1); // Extracting the last character /*
Checking whether the sentence ends with '.' or '?' or not */
if(last != '.' && last != '?' && last != '!')
{
System.out.println("Invalid Input. End a sentence with either '.', '?' or '!'
only");
return;
}
StringTokenizer str = new StringTokenizer(s," .?!");
int x = str.countTokens(); int c = 0; String w =
"", a = "", b = "";
for(int i=1; i<=x; i++)
{
w = str.nextToken(); // Extracting words and saving them in w
4
if(ob.isVowel(w))
{
c++; // Counting all words beginning and ending with a vowel
a = a + w + " "; // Saving words beginning and ending with a vowel in 'a'
} else
b = b + w + " "; // Saving all other words in variable 'b'
}
System.out.println("OUTPUT : \nNUMBER OF WORDS BEGINNING AND
ENDING WITH A VOWEL = " + c);
System.out.println(a+b);
} // end of psvm
} //end of class
INPUT
YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY.
OUTPUT
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY
PROGRAM 19:
Write a program to declare a matrix a[][] of order (m × n) where ‘m’ is
the number of rows and ‘n’ is the number of columns such that the values
of both ‘m’ and ‘n’ must be greater than 2 and less than 10. Allow the user
4
to input integers into this matrix. Perform the following tasks on the
matrix:
Display the original matrix.
Sort each row of the matrix in ascending order using any standard
sorting technique.
Display the changed matrix after sorting each row.
4
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
// loop for rows of matrix
for ( i = 0; i < a.length; i++)
{
// loop for column of matrix
for ( j = 0; j < a[i].length; j++)
{
// loop for comparison and swapping
for (k = 0; k < a[i].length - j - 1; k++)
{
if (a[i][k] > a[i][k + 1])
{
// swapping of elements
int t = a[i][k]; a[i][k] =
a[i][k + 1]; a[i][k + 1] = t;
}
}
}
}
System.out.println("Sorted Array");
// printing the sorted matrix
5
for ( i = 0; i < a.length; i++)
{
for ( j = 0; j < a[i].length; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
} // end of psvm
} //end of class
INPUT
m=3n
=3
22 5 19
7 36 12
9 13 6
OUTPUT:
Original matrix:
22 5 19
7 36 12
9 13 6
5
Matrix after sorting rows:
5 19 22
7 12 36
6 9 13
PROGRAM 20:
A Smith number is a composite number, the sum of whose digits is the
sum of the digits of its prime factors obtained as a result of prime
factorization (excluding 1). The first few such numbers are 4, 22, 27, 58,
85, 94, 121 …
5
n=n/10;
}
return s;
}
//function for generating prime factors and finding their sum int
sumPrimeFact(int n)
{
int i=2, sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDig(i); //Here 'i' is the prime factor of 'n' and we are
finding its sum
n=n/i; }
else
i++;
}
return sum;
}
5
if(a==b)
System.out.println("It is a Smith Number");
else
System.out.println("It is NOT a Smith Number");
} // end of psvm
} //end of class
INPUT
94
OUTPUT
It is a Smith Number