Class 12 ABC
List of Computer Science Practical Assignments
(2025-26)
--------------------------------------------------------------------------------------------------------
General Instructions
Please present the assignment in the following sequence:
Program number
Full Question for the program
Algorithm for the problem(typed on MS Word)
Code (copied & pasted on MS Word from BlueJ IDE)
Sample I/O (copied & pasted on MS Word)
Variable Description (Typed on MS Word)
Your practical sessional work file should have an index page.
The top cover should mention your name, class, section, roll number, unique id and index
number (if available at the time of submission).
Both side printing can be done, however each program must start on a fresh page.
Use font as Times New Roman and font size as 14.
All programs should be done in OOP approach (i.e. with instance variables, constructor,
functions etc.) A main function should be designed to execute the code.
The work should be neat and presentable.
The practical sessional work needs to be submitted in a channel file before first pre board
practical examinations.
Similarly the practical sessional work needs to be submitted after spiral binding before
second pre-board practical examination. This would be the final submission and the files
will not be returned thereafter. Students are advised to keep a soft copy of their practical
work for their reference.
The practical should be shown to the teacher before the spiral binding is done towards
the end of November.
--------------------------------------------------------------------------------------------------------
Question 1.
Hamming numbers are positive integers whose prime factors include 2, 3 and 5 only.
Example:
N=6 is a hamming number as 6=2 * 3, so its prime factors are limited to 2, 3.
N=8 is a hamming number as 8=2*2*2 and it has only 2 as its prime factors.
N=90 is a hamming number as 90=2*3*3*5 which has only 2, 3, 5 as prime factors.
N=14 is not a hamming number as 14=2*7. It has 7 as one of its prime factor.
N=44 is not a hamming number as 44= 2*2*11. It has 11 as one of its prime factors.
Design a program to accept any positive integer number and check if it is Hamming number or
not. Display the result with an appropriate message in the format specified below. The program
should also generate error message if a negative number is entered. Test your program for
the following data and some random data.
1
Example 1:
INPUT: Enter any number: 3600
OUTPUT: 3600=2*2*2*2*3*3*5*5
3600 IS A HAMMING NUMBER
Example 2:
INPUT: Enter any number: 5832
OUTPUT: 5832=2*2*2*3*3*3*3*3*3
5832 IS A HAMMING NUMBER
Example 3:
INPUT: Enter any number: 7854
OUTPUT: 7854=2*3*7*11*17
7854 IS NOT A HAMMING NUMBER
Example 4:
INPUT: Enter any number: -120
OUTPUT: NEGATIVE NUMBER ENTERED.
INVALID INPUT
Question 2
Write a program in JAVA to enter the date in (dd/mm/yyyy) format and day of the week on
which a project was given. If a college allots 90 days (including the project giving day) to
complete the work, then calculate the date on which the project is to be submitted along with
the day name. If the final submission day is SUNDAY then the students get one extra day
and submits the project on the next day that is MONDAY.
Also check the validity of the date entered and display your output in the format given
below.Test your program with the given sample data and some random data
Example 1
INPUT: ENTER PROJECT DATE: 1/9/2022
DAY OF THE WEEK: THURSDAY
OUTPUT: PROJECT SUBMISSION DATE: 29/11/2022
DAY OF THE WEEK: TUESDAY
Example 2
INPUT: ENTER PROJECT DATE: 25/11/2022
DAY OF THE WEEK: FRIDAY
OUTPUT: PROJECT SUBMISSION DATE: 22/02/2023
DAY OF THE WEEK: WEDNESDAY
Example 3
2
INPUT: ENTER PROJECT DATE:28/6/2022
DAY OF THE WEEK:TUESDAY
OUTPUT: PROJECT SUBMISSION DATE: 25/9/2022
DAY OF THE WEEK: SUNDAY
Example 4
INPUT: ENTER PROJECT DATE: 31/9/2022
DAY OF THE WEEK: THURSDAY
OUTPUT: INVALID DATE
Question 3
Most (NOT ALL) cell phone keypads look like the following arrangement (the letters are above
the respective number)
ABC DEF
1 2 3
GHI JKL MNO
4 5 6
PQRS TUV WXYZ
7 8 9
[SPACE]
0
For sending text/SMS the common problem is the number of keystrokes to type a particular text.
For example, the words “STOP”, there are a total of 9 keystrokes needed to type the word. You
need to press the key 7 four times, the 8 once, the key 6 three times and the key 7 once to get it.
Develop a program to find the number of keystrokes needed to type the text.
For this problem, accept just one word without any punctuation marks, numbers or white spaces
and the text message would consist of just 1 word.
Test your data with the sample data and some random data:
Example 1:
INPUT: DEAR
OUTPUT: Number of keystrokes = 7
3
Example 2:
INPUT: Thanks
OUTPUT: Number of keystrokes = 12
Example 3:
INPUT: Good-Bye
OUTPUT: INVALID ENTRY
Question 4:
A mystery number is a number that can be expressed as the sum of two numbers and those two
numbers should be the reverse of each other. For example, 584=193+391, 524=163+361 etc.
Write a program to accept the values of M and N [where 100<= (M, N) <=9999 and M<N]. Print
all the mystery numbers in the range (i.e. from M to N). Also print the frequency of such
numbers in the given range.
INPUT 1:
M=200
N=250
OUTPUT 1:
202 =101+101
222 =111+111
242 =121+121
The frequency is 3
INPUT 2:
M=500
N=600
OUTPUT 2:
504 =153+351
505 =104+401
524 =163+361
525 =114+411
544 =173+371
545 =124+421
564 =183+381
565 =134+431
584 =193+391
585 =144+441
The frequency is 10
INPUT 3:
M=75
N=180
4
OUTPUT 3:
Invalid Input
INPUT 4:
M=350
N=150
OUTPUT 4:
Invalid Input
Question 5
Write a program to accept a string (of length less than 50 and in upper case) and print
the longest sequence of consecutive letters present in it. (Assume only one such string
of longest length is present in the user input)
INPUT 1: COUNT YOUR CHICKENS BEFORE THEY HATCH
OUTPUT 1: RSTU
INPUT 2: DON BOSCO BANDEL
OUTPUT 2: ABCDE
INPUT 3: CITY CENTRE
OUTPUT 3: NO CONSECUTIVE LETTERS PRESENT
INPUT 4: TO ERR IS HUMAN, TO FORGIVE IS DIVINE
OUTPUT 4: DEFGHI
INPUT 5: HIPPOPOTOMONSTROSESQUIPPEDALIOPHOBIA
OUTPUT 5: LMNOPQRSTU
INPUT 6: strength is life weakness is death
OUTPUT 6: INVALID INPUT
INPUT 7: ARISE, AWAKE, AND STOP NOT UNTIL THE GOAL IS ACHIEVED
OUTPUT 7: INVALID INPUT
Question 6
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 store the answers of each participant row-wise.
Calculate the marks for each participant by matching the correct answer stored in a
single dimensional array of size 5. Display the scores for each participant and also the
participant(s) having the highest score.
Example: If the value of N = 4, then the array would be:
5
Q.1 Q.2 Q.3 Q.4 Q.5
Participant 1 A B B C A
Participant 2 D A D C B
Participant 3 A A B A C
Participant 4 D C C A B
Key to the question: D C C A B
Note: Array entries are line fed (i.e. one entry per line)
Test your program for the following data and some random data:
INPUT 1: N=5
Participant 1 D A B C C
Participant 2 A A D C B
Participant 3 B A C D B
Participant 4 D A D C B
Participant 5 B C A D D
Key : B C D A A
OUTPUT 1: Scores:
Participant 1 = 0
Participant 2 = 1
Participant 3 = 1
Participant 4 = 1
Participant 5 = 2
Highest score: Participant 5
INPUT 2: N=4
Participant 1 A C C B D
Participant 2 B C A A C
Participant 3 B C B A A
Participant 4 C C D D B
Key : A C D B B
OUTPUT 2: Scores:
Participant 1 = 3
Participant 2 = 1
Participant 3 = 1
Participant 4 = 3
Highest score: Participant 1
Participant 4
INPUT 3: N = 12
OUTPUT 3: INPUT SIZE OUT OF RANGE
Question 7
According to the Gregorian calendar, it was Monday on the date 01/01/1900. Write a program to
6
accept a date between 01/01/1900 and 31/12/2100 (Both dates inclusive) and print the
corresponding day. The program should check the validity of the user input date (Both range and
otherwise)
Question 8
Write a program to declare a square matrix A[ ][ ] of order MxM where ‘M’ is the number of
rows and the number of columns, such that M must be greater than 2 and less than 10. Accept the
value of M as user input. Display an appropriate message for an invalid input. Allow the user to
input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Rotate the matrix 90° clockwise as shown below:
Original matrix Rotated matrix
123 741
456 852
789 963
(c) Find the sum of the elements of the four corners of the matrix.
Test your program for the following data and some random data:
Example 1
INPUT :
M=3
123
456
789
OUTPUT :
ORIGINAL MATRIX
123
456
789
MATRIX AFTER ROTATION
741
852
963
Sum of the corner elements = 20
Example 2
INPUT :
M=3
7
123
456
789
OUTPUT :
ORIGINAL MATRIX
123
456
789
MATRIX AFTER ROTATION
741
852
963
Sum of the corner elements = 18
Example 3
INPUT :
M = 14
OUTPUT :
SIZE OUT OF RANGE
Example 4
INPUT :
M = 112
N = 130
OUTPUT :
INVALID INPUT
Question 9
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.
8
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
Example 2
INPUT:
N = 30
OUTPUT:
PRIME PAIRS ARE:
7, 23
11, 19
13, 17
Example 3
INPUT:
N = 17
OUTPUT:
INVALID INPUT. NUMBER IS ODD.
Example 4
INPUT:
N = 126
OUTPUT:
INVALID INPUT. NUMBER OUT OF RANGE.
Question 10
Write a program to declare a single dimensional array a[] and a square matrix b[][]
of size N, where N > 2 and N < 10. Allow the user to input positive integers into
the single dimensional array.
Perform the following task on the matrix:
(a) Sort the elements of the single dimensional array in ascending order using any sorting
technique and display the sorted elements.
(b) Fill the matrix b[][] in the following format.
If the array a[] ={5,2,8,1} then after sorting, a[]={1,2,5,8}
then the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
9
(c) Display the filled matrix in the above format
Test your program for some random data.
Example 1:
INPUT
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
OUTPUT: SORTED ARRAY :1 3 7
FILLED MATRIX
137
131
113
Question 11
Write a program to declare a single dimensional array a[ ] and a square matrix b[ ] [ ] of size N,
where N>2 and N<10. Allow the user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
(a) Sort the elements of the single dimensional array in ascending order using any standard
sorting technique and display the sorted elements.
(b) Fill the square matrix b[ ][ ] in the following format.
If the array a[ ] = { 5, 2, 8, 1 } then, after sorting a[ ] = {1, 2, 5, 8}
10
Question 12
Write a program to accept a paragraph containing TWO sentences only. The sentences may be
terminated by either ‘.’, ‘?’ or ‘!’ only. Any other character may be ignored. The words are to be
separated by a single blank space and must be in UPPER CASE.
Perform the following tasks:
(a) Check for the validity of the accepted paragraph for the number of sentences and for the
terminating character.
(b) Separate the two sentences from the paragraph and find the common words in the two sentences
with their frequency of occurrence in the paragraph.
(c) Display both the sentences separately along with the common words and their frequency, in the
format given below:
Example 1
INPUT: IS IT RAINING? YOU MAY GET WET IF IT IS RAINING.
OUTPUT: IS IT RAINING?
YOU MAY GET WET IF IT IS RAINING.
COMMOM WORDS FREQUENCY
IS 2
IT 2
RAINING 2
Example 2
INPUT: INDIA IS MY MOTHERLAND AND I AM PROUD OF MY MOTHERLAND.
ALL INDIANS ARE MY BROTHERS AND SISTERS.
OUTPUT: INDIA IS MY MOTHERLAND AND I AM PROUD OF MY
MOTHERLAND.
ALL INDIANS ARE MY BROTHERS AND SISTERS.
COMMOM WORDS FREQUENCY
MY 3
AND 2
Example 3
INPUT: ARE YOU COMING? I AM GETTING LATE.
OUTPUT: ARE YOU COMING?
I AM GETTING LATE.
NO COMMON WORDS
Example 4
INPUT: AT LAST, THE TIGER WAS SAVED.
OUTPUT: INVALID INPUT
Question 13
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.
11
(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.
THEHT BIRDRIB ISI FLYINGNIYLF
Example 2
INPUT:
IS THE WATER LEVEL RISING?
OUTPUT:
IS THE WATER LEVEL RISING?
ISI THEHT WATERETAW LEVEL RISINGNISIR
Example 3
INPUT:
THIS MOBILE APP LOOKS FINE.
OUTPUT:
THIS MOBILE APP LOOKS FINE.
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF
Example 3
INPUT:
YOU MUST BE CRAZY#
OUTPUT:
INVALID INPUT
Question 14
Write a Program in Java to check whether a number is a Keith Number or not.
The n digit number x is called Keith number if it appears in a special sequence (defined
below) generated using its digits. The special sequence has first n terms as digits of x and
other terms are recursively evaluated as sum of previous n terms. The task is to find if a
given number is Keith Number or not.
Examples:
INPUT:
x = 197
OUTPUT:
197 IS A KEITH NUMBER.
12
197 has 3 digits, so n = 3
[The number is Keith because it appears in the special sequence that has first three terms as
1, 9, 7 and remaining terms evaluated using sum of previous 3 terms. 1, 9, 7, 17, 33, 57, 107,
197, ....]
INPUT:
x = 12
OUTPUT:
12 IS NOT A KEITH NUMBER.
[The number is not Keith because it doesn't appear in the special sequence generated using its
digits. 1, 2, 3, 5, 8, 13, 21, ....]
INPUT :
x = 14
OUTPUT :
14 IS A KEITH NUMBER.
[14 is a Keith number since it appears in the sequence 1, 4, 5, 9, 14, ...]
Question 15
Consider the sequence of natural numbers.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
………………………………….
Removing every second number produces the sequences
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 ………………………….
Removing every third number produces the sequences
1, 3, 7, 9, 13, 15, 19, 21, 25 ………………………….
This process continues indefinitely by removing the fourth, fifth…and so on, till after a fixed
number of steps, certain natural numbers remain indefinitely. These are known as Lucky
Numbers.
Write a program to generate and print lucky numbers less than a given number N.
SAMPLE INPUT : N = 10
OUTPUT : The Lucky numbers less than 10 are: 1 3 7
SAMPLE INPUT : N = 25
OUTPUT : The Lucky numbers less than 25 are: 1 3 7 13 19
Question 16
The Tower of Hanoi is a mathematical problem formed of three towers. The purpose of the
puzzle is to push the whole stack to another bar. While moving the stacks, we need to obey
some simple rules:
We are allowed to move only one disc at a time.
We can only transfer the top disc of one stack to another bar which is empty.
We cannot place the larger disc on the smaller disc.
Write a program (using a recursive function) to accept number of disks and display the steps
required to accomplish the above task.
Question 17
13
A program to design recursive binary search.
Question 18
A positive integer n is called a Sphenic number if the number of its factors (other than 1 and
itself) is exactly three and all these factors are distinct prime numbers. In other words, if n is
a Sphenic integer if, n=p x q x r (p, q, and r are three distinct prime numbers and their
product is n).
For example
110 is a Sphenic number because 110=2 x 5 x 11. (2, 5, 11 are distinct prime numbers)
117 is not a Sphenic number because 117=3 x 3 x 13. (3 is repeated)
Write a program to accept an integer from the user and check it is a Sphenic number or not. If
it is a Sphenic number, display its prime factors as well.
Examples:
Input: Input:
Enter a number: 30 Enter a number:438
Output: Output:
30 is a Sphenic number 438 is a Sphenic number
30=2 x 3 x 5 438=2 x 3 x 73
Input: Input:
Enter a number: 165 Enter a number: 91
Output: Output:
165 is a Sphenic number 91 is not a Sphenic number
165=3 x 5 x 11
Input:
Input: Enter a number: 117
Enter a number: 165 Output:
Output: 117 is not a Sphenic number
165 is a Sphenic number
165=3 x 5 x 11
Question 19
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
14
113
Hence, 131 is a circular prime.
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.
Test your program with the following 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.
Question 20
Write a program to input a list of integers in an array and arrange them in a way similar to the to-
and-fro movement of a Pendulum.
The minimum element out of the list of integers, must come in center position of array.
The number in the ascending order next to the minimum, goes to the right, the next higher
number goes to the left of minimum number and it continues.
As higher numbers are reached, one goes to one side in a to-and-fro manner similar to that of
a Pendulum.
Examples:
Input: 1 3 2 5 4
Output: 5 3 1 2 4
Explanation:
The minimum element is 1, so it is moved to the middle.
The next higher element 2 is moved to the right of the
middle element while the next higher element 3 is
15
moved to the left of the middle element and
this process is continued.
Input: 11 12 31 14 5
Output: 31 12 5 11 14
Question 21
Write a program to declare a square matrix A[][] of order (M × M) where 'M' must be greater
than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform the
following tasks on the matrix:
Sort the non-boundary elements in ascending order using any standard sorting technique and
rearrange them in the matrix.
Calculate the sum of both the diagonals.
Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
Test your program for the following 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
SUM OF THE DIAGONAL ELEMENTS = 59
Example 2
INPUT:
M=5
16
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
OUTPUT:
ORIGINAL MATRIX
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
REARRANGED MATRIX
7 4 1 9 5
8 0 1 2 19
13 3 5 5 1
10 6 10 12 16
1 8 17 6 8
DIAGONAL ELEMENTS
7 5
0 2
5
6 12
1 8
SUM OF THE DIAGONAL ELEMENTS = 46
Question 22
Write a program to traverse a matrix spirally as follows:
17
Question 23
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 UPPER CASE.
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.
THEHT BIRDRIB ISI FLYINGNIYLF
Example 2
INPUT: IS THE WATER LEVEL RISING?
OUTPUT: IS THE WATER LEVEL RISING?
ISI THEHT WATERETAW LEVEL RISINGNISIR
Example 3
INPUT: THIS MOBILE APP LOOKS FINE.
OUTPUT: THIS MOBILE APP LOOKS FINE.
THISIHT MOBILELIBOM APPA LOOKSKOOL FINENIF
Example 4
INPUT: YOU MUST BE CRAZY#
OUTPUT: INVALID INPUT
18
Question 24
Question 25
19