Computer Project
Computer Project
Science Project
Session 2025-2026
Navneet Singh
XII-I
INTERNAL/EXTERNAL
ASSESSMENT
CLASS:XII-I
MARKS:
SIGNATURE:
MARKS:
SIGNATURE:
1
Preface
Through this project, I have learned a lot about the practical use
of computer programming and its importance.
2
Acknowledgment
3
Table of Contents
1. Matrix Sorting Program ........................................................................................................................... 5
4
1. Matrix Sorting Program
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 to input integers into this matrix. Perform the
following tasks on the matrix:
5
45. {
46. if(temp[j] > temp[j + 1])
47. {
48. int t = temp[j];
49. temp[j] = temp[j + 1];
50. temp[j + 1] = t;
51. }
52. }
53. }
54. k = 0;
55. for(i = 0; i < m; i++)
56. for(j = 0; j < n; j++)
57. a[i][j] = temp[k++];
58. System.out.println("Matrix after sorting rows:");
59. for(i = 0; i < m; i++) //loop prints the sorted matrix
60. {
61. for(j = 0; j < n; j++)
62. System.out.print(a[i][j] + "\t");
63. System.out.println();
64. }
65. }//main end
66. }//class end
Algorithm
Step 1: START
Step 2: import java.io.* package (importing all classes of java.io package)
Step 3: DECLARE class MatrixSort
Step 4: DECLARE public static void main(String args[]) which throws IOException
Step 5: DECLARE a new InputStreamReader object in
Step 6: DECLARE a new BufferedReader object br.
Step 7: INITIALIZE m = 0, n = 0, I = 0, j = 0, k = 0
Step 8: DECLARE arrays a[][], temp[]
Step 9: PRINT "M = "
Step 10: INPUT value of m from user.
Step 11: PRINT "N = "
Step 12: INPUT value of n from user.
Step 13: IF (m <= 2 or m >= 10 or n <= 2 or n>= 10) execute Step 14 to Step 15
Step 14: PRINT "Matrix size out of range." in a new line
Step 15: return
Step 16: INITIALIZE a = new int[m][n]
Step 17: INITIALIZE temp = new int[m * n]
Step 18: PRINT "Enter elements of matrix:" in a new line.
Step 19: Set i = 0. Repeat Step 20 to Step 22 till i < m and increase i by 1 after every iteration.
Step 20: Set j = 0. Repeat Step 21 to Step 22 till j < n and increase j by 1 after every iteration.
Step 21: Take INPUT for every a[i][j]
Step 22: PERFORM temp[k++] = a[i][j]
Step 23: PRINT "Original Matrix:" in a new line
Step 24: Set i = 0. Repeat Step 25 to Step 27 till i < m and increase i by 1 after every iteration.
Step 25: Set j = 0. Repeat Step 26 till j < n and increase j by 1 after every iteration.
Step 26: PRINT a[i][j] + "\t"
Step 27: PRINT a blank new line.
6
Step 28: Set i = 0. Repeat Step 29 to Step 33 till i < temp.length and increase i by 1 after every
iteration.
Step 29: Set j = 0. Repeat Step 30 to Step 33 till j < temp.length and increase j by 1 after every
iteration.
Step 30: IF (temp[j] > temp[j + 1]) execute Step 31 to 33
Step 31: PERFORM t = temp[j],
Step 32: PERFORM temp[j] = temp[j + 1]
Step 33: PERFORM temp[j + 1] = t
Step 34: PERFORM k = 0
Step 35: Set i = 0. Repeat Step 36 to Step 37 till i < m and increase i by 1 after every iteration.
Step 36: Set j = 0. Repeat Step 37 till j < n and increase j by 1 after every iteration.
Step 37: PERFORM [i][j] = temp[k++]
Step 38: PRINT "Matrix after sorting rows:" in a new line
Step 39: Set i = 0. Repeat Step 40 to Step 42 till i < m and increase i by 1 after every iteration.
Step 40: Set j = 0. Repeat Step 41 till j < n and increase j by 1 after every iteration.
Step 41: PRINT a[i][j] + "\t"
Step 42: PRINT a blank new line.
Step 43: END public static void main(String args[]) which throws IOException
Step 44: END class MatrixSort
Step 45: STOP
INPUT
:
m=4
n=3
Enterelements
-2 3of matrix:
11 5 16
9 3 7
0 1 4
8
OUTPUT
:
Original
11 -2Matrix:
3
5 16 7
9 0 1 4
3 8
Matrix after
3 sorting
11 rows:
-2 5 071 16
4 9
3 8
7
INPUT
:
m=3
n=3
Enterelements
5 19of matrix:
22 7 9 36 12
13 6
OUTPUT
:
Original5matrix:
19
22 7 936 12
13 6
Matrix after
19 sorting
22 rows:
5 7 6 12 36
9 13
INPUT
:
m = 11
OUTPUT :
Matrix size out of range.
n=5
8
2. Mirror Image of Matrix Program
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:
9
45. }
46. System.out.println();
47. }
48. }//main end
49. }//class end
Algorithm
Step 1: START import java.io.* package (importing all classes of java.io package) DECLARE class
Step 2: Mirror DECLARE public static void main(String args[]) which throws IOException
Step 3: DECLARE a new BufferedReader object br connected with InputStreamReader. PRINT
Step 4: "m = " INPUT value of ‘m’ from user If m < 3 or m > 19 then PRINT "SIZE OUT OF RANGE"
Step 5: and return else continue to Step 9 INITIALIZE a[][] as a 2 dimensional array of m rows
Step 6: and m columns INITIALIZE b[][] as a 2 dimensional array of m rows and m columns
Step 7: INITIALIZE p as 0 and q as m – 1 PRINT "Enter matrix elements:" in a new line Set i = 0.
Step 8: Repeat Step 14 to Step 19 till i < m and increase i by 1 after every iteration. PERFORM q
Step 9: = m - 1 Set j = 0. Repeat Step 16 to Step 18 till j < m and increase j by 1 after every
Step 10: iteration. Take INPUT for each a[i][j] PERFORM b[p][q] = a[i][j] Decrease value of q by 1
Step 11: Increase value of p by 1 PRINT "ORIGINAL MATRIX" in a new line Set i = 0. Repeat Step
Step 12: 22 to Step 24 till i < m and increase i by 1 after every iteration. Set j = 0. Repeat Step 23
Step 13: till j < m and increase j by 1 after every iteration. PRINT a[i][j] + "\t" PRINT a new blank
Step 14: line PRINT "MIRROR IMAGE MATRIX" in a new line Set i = 0. Repeat Step 27 to Step 29
Step 15: till i < m and increase i by 1 after every iteration. Set j = 0. Repeat Step 28 till j < m and
Step 16: increase j by 1 after every iteration. PRINT b[i][j] + "\t" PRINT a new blank line END
Step 17: public static void main(String args[]) which throws IOException END class Mirror STOP
Step 18:
Step 19:
Step 20:
Step 21:
Step 22:
Step 23:
Step 24:
Step 25:
Step 26:
Step 27:
Step 28:
Step 29:
Step 30:
Step 31:
Step 32:
10
INPUT:
M=3
4 16 12
8 2 1 14
6 3
OUTPUT:
ORIGINAL MATRIX
4 16 12
8 2 1 14
6 3
MIRROR IMAGE MATRIX
12 16 4
14 2 1 8
3 6
INPUT:
M = 22
OUTPUT:
SIZE OUT OF RANGE
11
3. Arrange Words based on Length of Words Program
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.
a) Check for the validity of the accepted sentence only for the terminating character.
b) Arrange the words in ascending order of their length. If two or more words have the
same length, then sort them alphabetically.
c) Display the original sentence along with the converted sentence.
31. {
32. for(int j = 0; j < a.length - 1 - i; j++)
33. {
34. if(a[j].compareTo(a[j + 1]) > 0)
35. {
36. String temp = a[j];
37. a[j] = a[j + 1]; a[j
38. + 1] = temp;
39. }
40. }
41. }
12
42. for(int i = 0; i < a.length; i++) //aranges the word according to their
43. length
44. {
45. for(int j = 0; j < a.length - 1 - i; j++)
46. {
47. if(a[j].length() > a[j + 1].length())
48. {
49. String temp = a[j];
50. a[j] = a[j + 1]; a[j
51. + 1] = temp;
52. }
53. }
54. }
55. for(int i = 0; i < a.length; i++) //prints the arranged words of the
56. array according to order specified by the question
57. System.out.print(a[i] + " ");
System.out.println();
}//main end
58. }//class end
59.
Algorithm
Step 1: START
Step 2: import java.io.* package (importing all classes of java.io package)
Step 3: import java.util.StringTokenizer
Step 4: DECLARE class Arrange
Step 5: DECLARE public static void main(String args[]) which throws IOException
Step 6: DECLARE a new BufferedReader object br connected with InputStreamReader.
Step 7: PRINT " Sentence: "
Step 8: INPUT value of String ‘s’ from user
Step 9: Trim String s
Step 10: Convert String s to Uppercase
Step 11: INITIALIZE int len with s.length()
Step 12: INITIALIZE char last with s.charAt(len - 1)
Step 13: Make a switch case as ‘last’ as the label. If ‘last’ is equal to ‘.’ or ‘?’ or ‘!’ then break
else PRINT "INVALID INPUT" in a new line and then return
Step 14: PRINT String s in a new line
Step 15: DECLARE a new StringTokenizer object st
Step 16: Store the number of tokens in String st in a new variable int count using the
countTokens() function.
Step 17: DECLARE a new single dimensional String array a[] with size equal to int count
Step 18: Set i = 0. Repeat Step 19 till i < len - 1 and increase i by 1 after every iteration
Step 19: PERFORM a[i] = st.nextToken()
Step 20: Set i = 0. Repeat Step 21 to Step 25 till i < a.length and increase i by 1 after every
iteration
Step 21: Set j = 0. Repeat Step 22 to Step 25 till j < a.length - 1 - i and increase j by 1 after every
iteration
Step 22: IF a[j].compareTo(a[j + 1]) > 0 then execute Step 23 to Step 25
Step 23: PERFORM String temp = a[j]
Step 24: PERFORM a[j] = a[j + 1]
13
Step 25: PERFORM a[j + 1] = temp
Step 26: Set i = 0. Repeat Step 27 to Step 31 till i < a.length and increase i by 1 after every
iteration
Step 27: Set j = 0. Repeat Step 28 to Step 31 till j < a.length - 1 - i and increase j by 1 after every
iteration
Step 28: IF a[j].length() > a[j + 1].length() then execute Step 29 to Step 31
Step 29: PERFORM String temp = a[j]
Step 30: PERFORM a[j] = a[j + 1]
Step 31: PERFORM a[j + 1] = temp
Step 32: Set i = 0. Repeat Step 27 till i < a.length and increase i by 1 after every iteration
Step 33: PRINT a[i] + " "
Step 34: PRINT a new blank line
Step 35: END public static void main(String args[]) which throws IOException
Step 36: END class Mirror
Step 37: STOP
INPUT:
As you sow so shall you reap.
OUTPUT:
AS YOU SOW SO SHALL YOU REAP.
AS SO SOW YOU YOU REAP SHALL
INPUT:
Self help is the best help.
OUTPUT:
SELF HELP IS THE BEST HELP.
IS THE BEST HELP HELP SELF.
INPUT:
Be kind to others.
OUTPUT:
BE KIND TO OTHERS.
BE TO KIND OTHERS
INPUT:
Nothing is impossible#
OUTPUT:
INVALID INPUT
14
4. ISBN Code Program
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. 0201103311:
10 * 0 + 9 * 2 + 8 * 0 + 7 * 1 + 6 * 1 + 5 * 0 + 4 * 3 + 3 * 3 + 2 * 1 + 1 * 1 = 55.
Since 55 leaves no remainder when divided by 11, hence it is a valid ISBN.
2. 007462542X:
10 * 0 + 9 * 0 + 8 * 7 + 7 * 4 + 6 * 6 + 5 * 2 + 4 * 5 + 3 * 4 + 2 * 2 + 1 * 10 = 176.
Since 176 leaves no remainder when divided by 11, hence it is a valid ISBN.
3. 0112112425:
10 * 0 + 9 * 1 + 8 * 1 + 7 * 2 + 6 * 1 + 5 * 1 + 4 * 2 + 3 * 4 + 2 * 2 + 1 * 5 = 71.
Since 71 leaves a 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 input, display
an appropriate message.
System.out.println("INVALID INPUT");
return;
15
18. }
19. if(!(last >= '0' && last <= '9') && last != 'X') //checks if last char
20. acter of String 'isbn' is between 0 and 9 and if it is not equal to 'X'
21. {
22. System.out.println("INVALID INPUT");
23. return;
24. }
25. boolean wrong = false;
26. for(int i = 0; i < len - 1; i++) //checks if String 'isbn'is a valid I
27. SBN number or not
28. {
29. char ch = isbn.charAt(i);
30. if(!(ch >= '0' && ch <= '9'))
31. {
32. wrong = true;
33. break;
34. }
35. else
36. {
37. int d = Integer.parseInt(Character.toString(ch));
38. sum += d * n;
39. n--;
40. }
41. }
42. if(wrong) //checks the conditions and then prints the message accordingly
43. {
44.
45. System.out.println("INVALID INPUT");
46. return;
47. }
48. if(last == 'X')
49. sum += 10;
50. else
51. sum += Integer.parseInt(Character.toString(last));
52. System.out.println("SUM = " + sum);
53. if(sum % 11 == 0)
54. System.out.println("LEAVES NO REMAINDER - VALID ISBN CODE");
else
System.out.println("LEAVES REMAINDER - INVALID ISBN CODE");
}//main end
55. }//class end
Algorithm
Step 1: START
Step 2: import java.io.* package (importing all classes of java.io package)
Step 3: DECLARE class ISBN
Step 4: DECLARE public static void main(String args[]) which throws IOException
Step 5: DECLARE a new BufferedReader object br connected with InputStreamReader
Step 6: PRINT "ISBN Code: "
Step 7: DECLARE variable String isbn and INPUT the value of String isbn from the user.
Step 8: Trim String isbn and convert it to UpperCase
Step 9: PERFORM int len = isbn.length()
Step 10: PERFORM char last = isbn.charAt(len - 1)
Step 11: INITIALIZE a new variable int sum with value 0.
Step 12: INITIALIZE a new variable int n with value 10.
Step 13: IF (len != 10) execute Step 14 to Step 15
16
Step 14: PRINT "INVALID INPUT" in a new line
Step 15: return
Step 16: IF (!(last >= '0' && last <= '9') && last != 'X') execute Step 17 to Step 18
Step 17: PRINT "INVALID INPUT" in a new line
Step 18: return
Step 19: INITIALIZE a new variable boolean wrong with value false
Step 20: Set i = 0. Repeat Step 21 to Step 27 till i < len - 1 and increase i by 1 after every
iteration.
Step 21: INITIALIZE a new variable char ch with value isbn.charAt(i)
Step 22: IF (!(ch >= '0' && ch <= '9')) execute Step 23 to Step 24
Step 23: wrong = true
Step 24: break else
Step 25: PERFORM int d = Integer.parseInt(Character.toString(ch))
Step 26: PERFORM sum+= d * n
Step 27: PERFORM n--
Step 28: IF (wrong == true) execute Step 29 to Step 30
Step 29: PRINT “INVALID INPUT" in a new line
Step 30: return
Step 31: IF (last == 'X') execute Step 32 else execute Step 33
Step 32: sum += 10
Step 33: PERFORM sum += Integer.parseInt(Character.toString(last))
Step 34: PRINT "SUM = " + sum in a new line.
Step 35: IF (sum % 11 == 0) execute Step 36 else execute Step 37
Step 36: PRINT "LEAVES NO REMAINDER - VALID ISBN CODE” in a new line
Step 37: PRINT “LEAVES REMAINDER - INVALID ISBN CODE" in a new line.
Step 38: END public static void main(String args[]) which throws IOException
Step 39: END class ISBN.
Step 40: STOP
Input
Output
SUM =99
LEAVES NO REMAINDER - VALID ISBN CODE
Input
ISBNCode: 035680324
Output
17
Input
Output
SUM = 122
LEAVES REMAINDER – INVALID ISBN CODE
18
5. Date Program
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.
19
47. {
48. if(ar[i] < p)
49. b = i;
50. if(ar[i] > p)
51. break;
52. }
53. nday = p - ar[b];
54. }
55. else //N was not between 1 and 100
56. {
57. k = 1;
58. System.out.println("N Days are out of range ");
59. }
60. if(k != 1) //prints the output after conditions are fulfilled
61. {
62. System.out.println("Date is : " + date + "th " + br[a + 1] + " "
63. + year); System.out.println("Date after "+ n + " days is: "+nday+"th " + b
64.
65. r[b+1]+" "+year1);
66. }
67. }
68. else //if year is not leap year
69. {
70. if(day >=1 && day <= 365) //checks if day is within range
71. {
72. for(i = 0; i <= 12; i++) //calculates the month and date
73. {
74. if(ar[i] < day)
75. a = i;
76. if(ar[i] > day)
77. break;
78. }
79. date = day - ar[a];
80. }
81. else //day was not within range
82. k = 1;
83. if(k == 1) //prints error message since day was not within range
84. System.out.println("Days are out of Range");
85. if(n >= 1 && n <= 100) //checks if n is between 1 and 100
86. {
87. if(p > ar[12]) //if p>365 then moves to over to the next year
88. {
89. p = p - ar[12];
90. year1 = year1 + 1;
91. }
92. for(i = 0; i <= 12; i++) //calculates the month and date
93. {
94. if(ar[i] < p)
95. { } if(ar[i] >
96. p) b = i;
97.
98.
99. break;
100. }
101. nday = p - ar[b];
102. }
103. else //prints error message since n was out of range
104. {
105. k = 1;
106. System.out.println("N days are out of range ");
107. }
108. if(k != 1) //prints output message is k is not equal to 1
{
System.out.println("Date is: "+date+"th " + br[a+1]+" "+ye
ar);
20
109. System.out.println("Date after "+ n + " days is: "+nday+"t
h " + br[b+1]+" "+year1);
110. }
111. }
112. }//main end
113. }//class end
Algorithm
Step 1: START
Step 2: import java.io.* package (importing all classes of java.io package)
Step 3: DECLARE class Find
Step 4: DECLARE public static void main(String args[]) which throws IOException
Step 5: DECLARE a new BufferedReader object br connected with InputStreamReader
Step 6: DECLARE int n, day, year, i, p
Step 7: INITIALIZE int k with value 0
Step 8: INITIALIZE a single dimensional array int ar[] with {0, 31, 59, 90, 120, 151, 181, 212,
243, 273, 304, 334, 365}
Step 9: INITIALIZE a single dimensional array String br[] with {"", "January", "February",
"March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"}
Step 10: PRINT "Day number: "
Step 11: Input value of variable int day from the user
Step 12: PRINT "Year: "
Step 13: Input value of variable int year from the user
Step 14: PRINT "Day after (N Days): "
Step 15: Input value of variable int n from the user
Step 16: PERFORM p = day + n
Step 17: INITIALIZE a = 1, b = 1, date = 0, nday = 0, year1 = year
Step 18: IF (year % 4 == 0) then execute Step 19 to Step 40 else execute Step 41 from Step 61
Step 19: Set i = 2. Repeat Step 20 till i <= 12 and increase i by 1 after every iteration.
Step 20: Perform ar[i] += 1
Step 21: IF (day >= 1 && day <= 366) then execute Step 22 to Step 25 else execute Step 26
Step 22: Set i = 2. Repeat Step 23 to Step 24 till i <= 12 and increase i by 1 after every iteration.
Step 23: IF (ar[i] < day) PERFORM a = i
Step 24: IF (ar[i] > day) break
Step 25: PERFORM date = day - ar[a]
Step 26: PERFORM k = 1
Step 27: IF (k == 1) execute Step 28 else IF (k != 1 && n >= 1 && n <= 100) execute Step 29 to
Step 35 else execute Step 36 to Step 37.
PRINT "Days are out of Range" in a new line
Step 28:
IF (p > ar[12]) execute Step 30 to Step 31
Step 29: PERFORM p = p - ar[12]
Step 30: PERFORM year1 = year1 + 1
Step 31: Set i = 0. Repeat Step 33 to Step 34 till i <= 12 and increase i by 1 after every iteration.
Step 32: IF (ar[i] < p) PERFORM b = i
Step 33: IF (ar[i] > p) break
Step 34:
21
Step 35: PERFORM nday = p - ar[b]
Step 36: PERFORM k = 1
Step 37: PRINT "N Days are out of range " in a new line.
Step 38: IF (k != 1) execute Step 39 to Step 40
Step 39: PRINT "Date is : " + date + "th " + br[a + 1] + " " + year in a new line
Step 40: PRINT "Date after "+ n + " days is: "+nday+"th " + br[b+1]+" "+year1 in a new line
Step 41: IF (day >=1 && day <= 365) execute Step 42 to Step 45 else execute step 46
Step 42: Set i = 0. Repeat Step 43 to Step 44 till i <= 12 and increase i by 1 after every iteration.
Step 43: IF (ar[i] < day) PERFORM a = i
Step 44: IF (ar[i] > day) break
Step 45: PERFORM date = day - ar[a]
Step 46: PERFORM k = 1
Step 47: IF (k == 1) PRINT "Days are out of Range" in a new line.
Step 48: IF (n >= 1 && n <= 100) execute Step 49 to Step 55 else execute Step 56 to Step 57
Step 49: IF (p > ar[12]) execute Step 50 to Step 51
Step 50: PERFORM p = p - ar[12]
Step 51: PERFORM year1 = year1 + 1
Step 52: Set i = 0. Repeat Step 53 to Step 54 till i <= 12 and increase i by 1 after every iteration.
Step 53: IF (ar[i] < p) PERFORM b = i
Step 54: IF (ar[i] > p) break
Step 55: PERFORM nday = p - ar[b]
Step 56: PERFORM k = 1
Step 57: PRINT "N days are out of range " in a new line
Step 58: IF (k != 1) execute Step 59 to Step 60
Step 59: PRINT "Date is: "+date+"th " + br[a+1]+" "+year in a new line
Step 60: PRINT "Date after "+ n + " days is: "+nday+"th " + br[b+1]+" "+year1 in a new line
Step 61: END public static void main(String args[]) which throws IOException
Step 62: END class Find
Step 63: STOP
INPUT:
Day number: 255
Year: 2018
Day after (N Days): 22
OUTPUT:
Date is: 12th September 2018
Date after 22 days is: 4th October 2018
INPUT:
Day number: 360
Year: 2018
Day after (N Days): 45
OUTPUT:
Date is: 26th December 2018
Date after 45 days is: 9th February 2019
22
INPUT:
Day number: 500
Year: 2018
Day after (N Days): 33
OUTPUT:
Days are out of Range
INPUT:
Day number: 150
Year: 2018
Day after (N Days): 330
OUTPUT:
N days are out of range
23
6. Circular Prime Number Program
A Circular Prime is a prime number that remains prime under cyclic shifts of its digits.
When theleftmost digit is removed and replaced at the end of the remaining string of
digits, thegenerated number is still prime. The process is repeated until the original
number isreached again.
A numberis said to be prime if it has only two factors 1 and itself.
Example:
131 311 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.
1. 2.
import java.io.*; //importing IO Package
3. 4.
class CircularPrime //declaring class
5. {6.
7. 8. public static void main(String args[])throws IOException //main function
9. {
10. InputStreamReader in = new InputStreamReader(System.in); //declaring
11. InputStreamReader object
12. BufferedReader br = new BufferedReader(in); //declaring BufferedReader
13. object System.out.print("N = ");
14. int n = Integer.parseInt(br.readLine());
15. String s = Integer.toString(n);
16. int len = s.length();
17. boolean status = true;
18. for(int i = 1; i <= len; i++)
19. //does cyclic shifts of the number 'n'
20. and checks if the generated number is still prime
21. {
22. if(!isPrime(n))
23. status = false;
24. System.out.println(n);
25. s = s.substring(1) + s.charAt(0);
26. n = Integer.parseInt(s);
27. }
28. if(status) //prints the message accroding to evaluation of the condition
29.
30. System.out.println(n + " IS A CIRCULAR PRIME.");
31. else
32. System.out.println(n + " IS NOT A CIRCULAR PRIME.");
33. }
34. } public static boolean isPrime(int num) //function to check if the number is
prime or not
{
int f = 0;
for(int i = 1; i <= num; i++)
if(num % i == 0)
f++;
return (f == 2);
}
24
INPUT:
N= 197
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME.
INPUT:
N= 1193
OUTPUT:
1193
1931
9311
3119
1193
INPUTIS: A CIRCULAR PRIME.
N = 29
OUTPUT:
29
92
29 IS NOT A CIRCULAR PRIME.
25
7. Special Number Program
A special number is a number in which the sum of the factorial of each digit is equal to
the number itself.
For example: 145.
1! + 4! + 5! = 1 + 24 + 120 = 145.
Thus, 145 is a special number.
Design a class Special to check if a given number is a special number. Some of the
members of the class are given below:
Class name: Special
Data members:
n: to store the integer.
Member functions:
Special(): constructor to assign 0 to n.
Special(int): parameterized constructor to assign a value to ‘n’.
void sum(): calculate and display the sum of the first and last digit of ‘n’.
void isSpecial(): check and display if the number ‘n’ is a special number.
Specify the class Special giving details of the constructors, void sum() and void
isSpecial(). You need not write the main() function.
26
27. int d = num % 10;
28. int f = 1;
29. for(int i = 1; i <= d; i++)
30.
31. f *= i;
32. sum += f;
33. num /= 10;
34. }
35. if(n == sum) //prints the output message depending on the condition
36. System.out.println(n + " is a special number.");
37. else
38. System.out.println(n + " is not a special number.");
39. }
40. }//class end
Input
145
Output
153
Output
27
8. Triangular Number Program
Aclass Numbers contains the following data members and methods to check for
triangular numbers.
Atriangular number is formed by the addition of a consecutive sequence of integers
starting from1.
Example:
1+ 2 = 3
1+ 2 + 3=6
1+ 2 + 3+4= 10
1+ 2 + 3+4+ 5 + 15
Therefore,3,6, 10, 15 are triangular numbers.
Class name:Numbers
Data members/instance variables:
n: integer tobe checked whether it is triangular or not.
Member function/methods:
void getNum(): to accept the integer n.
int check(int): to check if n is triangular.
void display(): to display a suitable message whether n is triangular or not.
Specify the class Numbers giving details of the methods getNum(), check(int) and
display().
28
27. {
28. if(check(n) == 1)
29. System.out.println(n + " is triangular.");
30. else
31. System.out.println(n + " is not triangular.");
32. }
33. }//class end
Input
21
Output
21isatriangular number.
Input
17
Output
17isnot a triangular number.
29
9. Date and Month Program
Design a class Convert to find the date and the month from a given day number for a
particular year.
Example: If day number is 64 and the year is 2020, then the corresponding date would
be: March 4, 2020 i.e. (31 + 29 + 4 = 64).
30
26. System.out.println("Invalid day number!");
27. System.exit(0);
28. }
29. }
30. else if(d > 365) //checks for invalid day number for a normal year
31. {
32. System.out.println("Invalid day number!");
33. System.exit(0);
34. }
35. }
36. public void dayToDate() //converts day to date
37. {
38. int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
39. if((y % 100 == 0 && y % 400 == 0) || y % 4 == 0)
40. a[1] = 29;
41. int sum = 0;
42. if(d <= 31) m = 0; else{ while(d > 0)
43. {
44. d -= a[m];
45. m++;
46. if(d <= a[m])
47. break;
48. }
49. }
50. }
51. public void display() //displays the date with the year
52. {
53. String months[] = {"January", "February", "March", "April", "May", "June"
54. , "July", "August", "September", "October", "November", "December"};
55. System.out.println(months[m] + " " + d + ", " + y);
56. }
57. public static void main(String args[])throws IOException //main function
58. {
59. Convert obj = new Convert();
60. obj.accept();
61. obj.dayToDate();
62. obj.display();
}//main end
63. }//class end
INPUT:
INPUT: M = 22
OUTPUT: SIZE OUT OF RANGE
OUTPUT:
February 14, 2009
INPUT:
Day number: 60
Year: 2020
OUTPUT:
February 29, 2020
31
10. Composite Magic Number Program
Composite number: A composite number is a number that 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 digits 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 the frequency.
32
34. for(int i = 1; i <= num; i++)
35. if(num % i == 0)
36. f++;
37. return f > 2;
38. }
39. public static boolean isMagic(int num) //function to check for magic number
40. {
41.
42. while(num > 9){
43. num = sumOfDigits(num);
44. }
45. return num == 1;
46. }
47. public static int sumOfDigits(int num) //function to calculate sum of digits
48. of a number
49. {
50. int sum = 0;
51. while(num != 0){
52. sum += num % 10;
53. num /= 10;
54. }
return sum;
}
55. }//class end
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
INPUT:
m = 1200
n = 1300
OUTPUT:
THE COMPOSITE MAGIC INTEGERS ARE:
1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288
FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9
INPUT:
m = 120
n = 99
OUTPUT:
INVALID INPUT
33
11. Frequency of And, An Program
Input a sentence from the user and count the number of times the words “an” and “and”
are present in the sentence. Design a class Frequency using the description given
below:
Class name: Frequency
Data members/variables:
text: stores the sentence.
countAnd: to store the frequency of the word “and”.
countAn: to store the frequency of the word “an”.
len: stores the length of the string.
Member functions/methods:
Frequency(): constructor to initialize the instance variables.
void accept(String n): to assign ‘n’ to ‘text’, where the value of the parameter ‘n’ should
be in lowercase.
void checkAndFreq(): to count the frequency of “and”.
void checkAnFreq(): to count the frequency of “an”.
void display(): to display the number of “and” and “an” with appropriate messages.
Specify the class Frequency giving details of the constructor, void accept(String), void
checkAndFreq(), void checkAnFreq() and void display(). Also define the main() function
to create an object and call the methods accordingly to enable the task.
34
34. public void checkAnFreq() //to count the frequency of “an”
35. {
36. StringTokenizer st = new StringTokenizer(text, "!.?, ");
37. int count = st.countTokens();
38. for(int i = 1; i <= count; i++)
39. {
40. String word = st.nextToken();
41. if(word.equals("an"))
42. countAn++;
43. }
44. }
45. public void display()//displays the number of “and” and “an” with appropriate
46. messages
47. {
48. System.out.println("Number of And(s): " + countAnd);
49. System.out.println("Number of An(s): " + countAn);
50. }
51. public static void main(String args[])throws IOException //main function
52. {
53. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
54. //declaring BufferedReader object
55. System.out.print("Enter the sentence: ");
56. String s = br.readLine();
57. Frequency obj = new Frequency();
58. obj.accept(s);
59. obj.checkAndFreq();
60. obj.checkAnFreq();
obj.display();
}//main end
61. }//class end
INPUT:
Enter the sentence: He is studying for an exam and is planning to become an
engineer.
OUTPUT:
Number of And(s): 1
Number of An(s): 2
INPUT:
Enter the sentence: What an ingenious and elegant party it is!
OUTPUT:
Number of And(s): 1
Number of An(s): 1
INPUT:
It's really dumb to borrow money from people to fulfill your luxurious dreams
OUTPUT:
Number of And(s): 0
Number of An(s): 0
35
12. Words Beginning with Capital Letter Program
A class Capital has been defined to check whether a sentence has words beginning with
a capital letter or not.
Some of the members of the class are given below:
Specify the class Capital, giving the details of the constructor, void input(), boolean
isCap(String) and void display(). Define the main() function to create an object and call
the function accordingly to enable the task.
36
30. for(int i = 1; i <= count; i++){
31. String word = st.nextToken();
32. if(isCap(word))
33. freq++;
34. }
35. System.out.println("Sentence: " + sent);
36. System.out.println("Frequency: " + freq);
37. }
38. public static void main(String args[])throws IOException //main function
39. {
40. Capital obj = new Capital();
41. obj.input();
42. obj.display();
43. }//main end
44. }//class end
45.
INPUT:
Sentence: We're going to play baseball in Prospect Park.
OUTPUT:
Sentence: We're going to play baseball in Prospect Park.
Frequency: 3
INPUT:
Sentence: Agatha Christie wrote many books.
OUTPUT:
Sentence: Agatha Christie wrote many books.
Frequency: 2
37
13. Fibonacci Strings Program
38
30. for(int i = 3; i <= n; i++){
31. System.out.print(z + "\t");
32. x = new String(y); y = new
33. String(z); z = new String(y
34. + x);
35. }
36. }
37. }
38. public static void main(String args[])throws IOException //main function
39. {
40. FiboString obj = new FiboString();
41. obj.accept();
42. obj.generate();
43. }//main end
44. }//class end
45.
INPUT:
n=6
OUTPUT:
a b ba bab babba babbabab
INPUT:
n=4
OUTPUT:
a b ba bab
39
14. Vowels Consonants Program
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.
40
45. }
46. public static void display(String w) //displays the number of consonants a
47. nd vowels in the string(word) passed to it
48. {
49. int v = 0; int c = 0; w =
50. w.toUpperCase(); for(int i = 0; i <
51. w.length(); i++) {
52.
53.
54. char ch = w.charAt(i);
55. switch(ch)
56. {
57. case 'A':
58. case 'E':
59. case 'I':
60. case 'O':
61. case 'U':
62. v++;
63. break;
64. default:
65. c++;
66. }
67. }
68. System.out.printf("%-16s%-16d%-16d\n", w, v, c);
69. /*
70. * System.out.printf("%-16s%-16d%-16d\n", w, v, c);
71. * %16d refers to 16 character spacing for an integer, and %16d refers to
72. 16 character spacing for a String.
73. *
74. * If we use %-16d, then the integer will be left aligned
75. * If we use %-16s, then the string will be left aligned
76. } */
}
INPUT:
String: The quick brown fox jumps over the lazy dog.
OUTPUT:
The Quick Brown Fox Jumps Over The Lazy Dog.
Word Vowels Consonants
THE 1 2
QUICK 2 3
BROWN 1 4
FOX 1 2
JUMPS 1 4
OVER 2 2
THE 1 2
LAZY 1 3
DOG 1 2
41
INPUT:
String: How are you?
OUTPUT:
How Are You?
Word Vowels Consonants
HOW 1 2
ARE 2 1
YOU 2 1
INPUT:
String: The misguided kid cannot paint
OUTPUT:
Invalid input.
42
15. Series Sum Program
Specify the class SeriesSum, giving details of the constructor, double findFact(int),
double findPower(int, int), void calculate() and void display(). Define the main() function
to create an object and call the functions accordingly to enable the task.
1. import java.io.*;
2. class SeriesSum
3. {
4. private int x; //to store an integer number
5. private int n; //o store number of terms
6. private double sum; //double variable to store the sum of the series
7. public SeriesSum(int xx, int nn) // constructor to assign x = xx and n = n
8. n {
9.
10. x = xx; n =
11. nn; sum =
12. 0.0;
13. }
14. public double findFact(int m) //to return the factorial of m using recursiv
15. e technique
16. {
17. if(m <= 1)
18. return 1.0;
19. else
20. return m * findFact(m - 1);
21. }
22. public double findPower(int x, int y) //to return x raised to the power of
23. y using recursive technique
24. {
25. if(y == 0)
26. return 1.0;
27. else
28. return x * findPower(x, y - 1);
29. }
public void calculate() //to calculate the sum of the series by invoking the
recursive functions
{
for(int i = 1; i <= n; i++)
43
30. sum += findPower(x, i * 2) / findFact(i * 2 - 1);
31. } public void display() { } public static void
32. main(String[] args)throws IOException
//to { sum of the series
display the
33.
34. System.out.println("Sum = " + sum);
35.
36. //main function
37.
38. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
39. //declaring BufferedReader object
40. System.out.print("x = ");
41. int xx = Integer.parseInt(br.readLine());
42. System.out.print("n = ");
43. int nn = Integer.parseInt(br.readLine());
44. SeriesSum obj = new SeriesSum(xx, nn);
45. obj.calculate();
46. obj.display();
}//main end
47. }//class end
INPUT:
x=5
n=6
OUTPUT:
Sum = 369.90764227092353
INPUT:
x=3
n=6
OUTPUT:
Sum = 30.0528226461039
44
16. Reverse Matrix Elements Program
Class name:MatRev
Data members/instancevariables:
arr[][]: to storeintegerelements
m: to store thenumberofrows
n: to store thenumberofcolumns
Member functions/methods:
MatRev(int mm,intnn):parameterized constructor to initialize the data members m =
mm and n = nn.
void fillArray():toenterelementsinthe array.
int reverse(intx):returnsthereverseof the number x.
void revMat(MatRevp):reverseseach element of the array of the parameterized object
and storesitinthearrayofthecurrent object.
void show():displaysthearrayelements in matrix form.
Define the classMatRevgivingdetails of the constructor, void fillArray(), int reverse(int),
void revMat(MatRev)andvoidshow(). Define the main() function to create objects and
call the functionsaccordinglytoenable the task.
45
31. }
32. public void revMat(MatRev p)
33. /*reverses each element of the array of the
34. * parameterized object and stores it in the array of the current object
35. *
36. */
37. {
38. for(int i = 0; i < m; i++)
39. { for(int j = 0; j < n; j++)
40. { }
41.
42. this.arr[i][j] = reverse(p.arr[i][j]);
43.
44. }
45. }
46. public void show() //displays the array elements in matrix form
47. {
48. for(int i = 0; i < m; i++)
49. {
50. for(int j = 0; j < n; j++)
51. { } System.out.println();
52. System.out.print(arr[i][j] + "\t");
53.
54.
55. }
56. }
57. public static void main(String args[])throws IOException //main function
58. {
59. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
60. //declaring BufferedReader object
61. System.out.print("Enter number of rows: "); int x =
62. Integer.parseInt(br.readLine());
63. System.out.print("Enter number of columns: "); int y =
64. Integer.parseInt(br.readLine()); MatRev obj1 = new
65. MatRev(x, y); MatRev obj2 = new MatRev(x, y);
66. obj1.fillArray(); obj2.revMat(obj1);
67. System.out.println("Original Matrix"); obj1.show();
68. System.out.println("Matrix with reversed elements");
69. obj2.show();
70.
71.
72.
}//main end
73. }//class end
46
INPUT:
Enter number of rows: 3
Enter number of columns: 3
Enter matrix elements: 43 56
3 65 23 754 34 56 23
OUTPUT:
OriginalMatrix
43 56 3
65 23 754
34 56 23
Matrix with reversed elements
34 65 3
56 32 457
43 65 32
INPUT:
Enter number of rows: 4
Enter number of columns: 2
Enter matrix elements: 3 45
2342 43 23 76 56 687
OUTPUT:
Original Matrix
3 45
2342 43
23 76
56 687
Matrix with reversed elements
3 54
2432 34
32 67
65 786
47
17. Goldbach Number Program
AGoldbachnumber is a positive even integer that can be expressed as the sum of two
odd primes.
Note:Alleveninteger numbers greater than 4 are Goldbach numbers.
Example:
6=3+3
10=3+7
10=5+5
Hence,6hasone odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3, 7
and 5, 5.
Writeaprogram to accept an even integer ‘N’ where N > 9 and N < 50. Find all the odd
primepairswhose sum is equal to the number ‘N’.
48
38. f++;
39. } if(f ==
40. 2) return
41. true;
42. return false;
43. }
44. }//class end
INPUT:
N = 14
OUTPUT:
Prime pairs are:
3, 11
7, 7
INPUT:
N = 30
OUTPUT:
Prime numbers are:
7, 23 11, 19 13, 17
INPUT:
N = 126
OUTPUT:
Invalid input. Number is outof range.
49
18. Vowel Word Program
Design a class VowelWord to accept a sentence and calculate the frequency of words
that begin with a vowel. The words in the input string are separated by a single blank
space and terminated by a full stop. The description of the class is given below:
Class name: VowelWord
Data members/instance variables:
str: to store a sentence.
freq: store the frequency of the words beginning with a vowel.
Member functions:
VowelWord(): constructor to initialize data members to legal initial values.
void readStr(): to accept a sentence.
void freqVowel(): counts the frequency of the words that begin with a vowel.
void display(): to display the original string and the frequency of the words that begin
with a vowel.
Specify the class VowelWord giving details of the constructor, void readStr(), void
freqVowel() and display(). Also define the main() function to create an object and call
the methods accordingly to enable the task.
50
34. char ch = word.charAt(0);
35. switch(ch)
36. {
37. case 'A':
38. case 'E':
39. case 'I':
40. case 'O':
41. case 'U':
42. freq++;
43. }
44. }
45. }
46. public void display() //to display the original string and the frequency of
47. the words that begin with a vowel
48. {
49. System.out.println("Original string: " + str);
50. System.out.println("Required frequency: " + freq);
51. }
52. public static void main(String args[])throws IOException //main function
53. {
54. VowelWord obj = new VowelWord();
55. obj.readStr();
56. obj.freqVowel();
57. obj.display();
}//main end
58. }//class end
Input
Sentence: Yesterday I had an unforgettable experience.
Output
Output
51
19. Disarium Number Program
A disarium number is a number in which the sum of the digits to the power of their
respective position is equal to the number itself.
Example: 135 = 11 + 32 + 53
Hence, 135 is a disarium number.
Design a class Disarium to check if a given number is a disarium number or not. Some
of the members of the class are given below:
Class name: Disarium
Data members/instance variables:
int num: stores the number.
int size: stores the size of the number.
Methods/Member functions:
Disarium(int n): parameterized constructor to initialize the data members num = n and
size = 0.
void countDigit(): counts the total number of digits and assigns it to size.
int sumOfDigits(int n, int p): returns the sum of the digits of the number ‘n’ to the power
of their respective positions ‘p’ using recursive technique.
void check(): checks whether the number is a disarium number and displays the result
with an appropriate message.
Specify the class Disarium giving details of the constructor, void countDigit(), int
sumOfDigits(int, int) and void check(). Define the main() function to create an object and
call the functions accordingly to enable the task.
52
26. {
27. if(num == sumOfDigits(num, size))
28. System.out.println(num + " is a Disarium Number.");
29. else
30. System.out.println(num + " is not a Disarium Number.");
31. }
32. public static void main(String args[])throws IOException //main function
33. {
34. InputStreamReader in = new InputStreamReader(System.in);
35. //declaring InputStreamReader object
36. BufferedReader br = new BufferedReader(in); //declaring BufferedReader
37. object
System.out.print("Number: ");
38. int n = Integer.parseInt(br.readLine());
39. Disarium obj = new Disarium(n);
40. obj.countDigit();
41. obj.check();
}//main function end
INPUT:
Number = 89
OUTPUT:
89 is a Disarium Number.
INPUT:
Number = 52
OUTPUT:
89 is not a Disarium Number.
53
20. Combine Two Arrays into One Program
A class Combine contains an array of integers which combines two arrays into a single
array including the duplicate elements if any, and sorts the combined array. Some of the
members of the class are given below:
Class name: Combine
Data members/instance variables:
com[]: integer array
size: size of the array
Member functions/methods:
Combine(int num): parameterized constructor to assign size = num.
void inputArray(): to accept the array elements.
void sort(): sorts the elements of combined array in ascending order using the selection
sort technique.
void mix(Combine a, Combine b): combines the parameterized object arrays and stores
the result in the current object array along with duplicate elements if any.
void display(): displays the array elements.
Specify the class Combine giving details of the constructor, void inputArray(). void sort(),
void mix(Combine, Combine) and void display(). Also define the main() function to create
an object and call the methods accordingly to enable the task.
54
34. }
35. }
36. public voidmix(Combinea, Combine b) //combines the parameterized object
arrays and stores theresultinthecurrentobjectarrayalong withduplicate
elemen ts
37. {
38. intindex =0;
39. for(inti =0; i <a.size; i++)
40. this.com[index++]=a.com[i];
41. for(inti =0; i <b.size; i++)
42. this.com[index++]=b.com[i];
43. }
44. public voiddisplay() //displays the array elements
45. {
46. for(inti =0; i <size;i++)
47. System.out.print(com[i]+ "\t");
48. System.out.println();
49. }
50. public static void main(String args[])throws IOException //main function
51. { BufferedReader
52. br=newBufferedReader(newInputStreamReader(System.in));
53. //declaringBufferedReaderobject
54. System.out.print("Firstarray size:");
55. intsize1 =Integer.parseInt(br.readLine());
56. System.out.print("Secondarray size:");
57. intsize2 =Integer.parseInt(br.readLine());
58. Combineobj1= newCombine(size1);
Combineobj2= Combineobj3=
//makingobjectof class combine
59. newCombine(size2);
System.out.println("Enterfirstarrayelements:");
60. newCombine(size1+ //makingobjectof classcombine
61. size2); //makingobjectof classcomb
62. ine
63. //callsinputArray()
64. ofobj1to inputfirst arrayelements
65. obj1.inputArray();
66. System.out.println("Entersecondarray elements:");//callsinputArray()
67. of obj2 to input second array elements
68. obj2.inputArray();
69. System.out.println("Firstarrayelements:"); //callsdisplay()of obj
70. 1todisplayelementsof first array
71. obj1.display();
72. System.out.println("Secondarrayelements:"); //callsdisplay()ofobj2
to display elements of first array
obj2.display();
obj3.mix(obj1, obj2); //callsmix()of obj3
obj3.sort();
System.out.println("Combinedarray:"); //pritsthecombinedarrayby cal
ling display() of obj3
obj3.display();
}//mainend
73. }//classend
74.
INPUT:
First array size: 5
Second array size: 6
55
Enter first array elements: 12
434 643 764 23 Enter second
array elements: 76 3424 76 7
87 5
OUTPUT:
First array elements:
INPUT:
First array size: 3
Second array size: 5
Enter first array elements:
53
645
76
Enter second array elements:
345
75
6
75
976
OUTPUT:
First array elements:
53 645 76
Second array elements:
345 75 6 75 976
Combined array: 75 76
6 53 75 345 645 976
56