KEMBAR78
Computer Project | PDF
0% found this document useful (0 votes)
9 views57 pages

Computer Project

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views57 pages

Computer Project

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

ISC Computer

Science Project
Session 2025-2026

Navneet Singh
XII-I
INTERNAL/EXTERNAL
ASSESSMENT

NAME: Navneet Singh

CLASS:XII-I

NAME OF INTERNAL EXAMINER:

MARKS:

SIGNATURE:

NAME OF EXTERNAL EXAMINER:

MARKS:

SIGNATURE:

1
Preface

In an age where computer plays a very significant role in the


working of the society, it is extremely important for every
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 its importance.

I am pleased to present this project on computer


programming.

2
Acknowledgment

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.

Navneet Singh XII-I

3
Table of Contents
1. Matrix Sorting Program ........................................................................................................................... 5

2. Mirror Image of Matrix Program.............................................................................................................. 9

3. Arrange Words based on Length of Words Program .............................................................................. 12

4. ISBN Code Program ................................................................................................................................ 15

5. Date Program ........................................................................................................................................ 19

6. Circular Prime Number Program ............................................................................................................ 24

7. Special Number Program ....................................................................................................................... 26

8. Triangular Number Program .................................................................................................................. 28

9. Date and Month Program ...................................................................................................................... 30

10. Composite Magic Number Program ....................................................................................................... 32

11. Frequency of And, An Program .............................................................................................................. 34

12. Words Beginning with Capital Letter Program ....................................................................................... 36

13. Fibonacci Strings Program ..................................................................................................................... 38

14. Vowels Consonants Program ................................................................................................................. 40

15. Series Sum Program ............................................................................................................................... 43

16. Reverse Matrix Elements Program ......................................................................................................... 45

17. Goldbach Number Program ................................................................................................................... 48

18. Vowel Word Program ............................................................................................................................ 50

19. Disarium Number Program .................................................................................................................... 52

20. Combine Two Arrays into One Program ................................................................................................. 54

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:

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.
1. import java.io.*; //importing IO Package
2. class MatrixSort //declaring class
3. {
4. public static void main(String args[])throws IOException //main function
5. {
6. InputStreamReader in = new InputStreamReader(System.in);//declaring Input
7. StreamReader object
8. BufferedReader br = new BufferedReader(in); //declaring BufferedReader
9. object int m = 0; int n = 0; int a[][]; int i = 0; int j = 0; int temp[]; int k
10. = 0; System.out.print("M = ");
11.
12.
13.
14.
15.
16.
17. //inputing number
18. m = Integer.parseInt(br.readLine());
19. System.out.print("N = "); //inputing number
20. n = Integer.parseInt(br.readLine());
21. if(m <= 2 || m >= 10 || n <= 2 || n >= 10) //function to check of matrix
22. size is within range
23. {
24. System.out.println("Matrix size out of range.");
25. return;
26. }
27. a = new int[m][n];
28. temp = new int[m * n];
29. System.out.println("Enter elements of matrix:");
30. for(i = 0; i < m; i++) //loop to input elements of matrix
31. {
32. for(j = 0; j < n; j++)
33. {
34. a[i][j] = Integer.parseInt(br.readLine());
35. temp[k++] = a[i][j];
36. }
37. }
38. System.out.println("Original Matrix:");
39. for(i = 0; i < m; i++) //loop to print the original matrix
40. {
41. for(j = 0; j < n; j++)
42. System.out.print(a[i][j] + "\t");
43. System.out.println();
44. }
for(i = 0; i < temp.length; i++) //sorts each row of the matrix in
ascending order
{
for(j = 0; j < temp.length - 1 - i; j++)

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:

(a) Display the input matrix.


(b) Create a mirror image of the inputted matrix.
(c) Display the mirror image matrix.

1. import java.io.*; //importing IO Package


2. class Mirror //declaring class
3. {
4. public static void main(String args[])throws IOException //main function
5. {
6. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7. //declaring BufferedReader object
8. System.out.print("m = "); //inputs dimensions of square matrix and stores
9. it in 'm'
10. int m = Integer.parseInt(br.readLine());
11. if(m < 3 || m > 19) //checks if size of matrix is within specified range
12. {
13.
14. System.out.println("SIZE OUT OF RANGE");
15. return;
16. } int a[][] = new int[m][m]; int b[][] = new int[m][m];
17. int p = 0; int q = m - 1; System.out.println("Enter
18. matrix elements:"); for(int i = 0; i < m; i++) //inputs
19. elements of matrix {
20.
21.
22.
23.
24. q = m - 1;
25. for(int j = 0; j < m; j++)
26. {
27. a[i][j] = Integer.parseInt(br.readLine());
28. b[p][q] = a[i][j];
29. q--;
30. }
31. p++;
32. }
33. System.out.println("ORIGINAL MATRIX");
34. for(int i = 0; i < m; i++) //displays original matrix
35. {
36. for(int j = 0; j < m; j++)
37. { } System.out.println();
38. System.out.print(a[i][j] + "\t");
39.
40.
41. }
42. System.out.println("MIRROR IMAGE MATRIX");
43. for(int i = 0; i < m; i++) //prints mirror image matrix
44. {
for(int j = 0; j < m; j++)
{
System.out.print(b[i][j] + "\t");

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.

Perform the following tasks:

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.

1. import java.io.*; //importing IO Package


2. import java.util.StringTokenizer; //improrting StringTokenizer Class
3. class Arrange //declaring class
4. {
5. public static void main(String args[])throws IOException //main function
6. {
7. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8. //declaring BufferedReader object
9. System.out.print("Sentence: "); //inputs sentence
10. String s = br.readLine();
11. s = s.trim(); //removes leading and trailing spaces
12. s = s.toUpperCase(); //converts inputed trimmed sentence to uppercase
13. int len = s.length();
14. char last = s.charAt(len - 1);
15.
16. switch(last) //checks if sentence is valid and prints a error message
17. if invalid
18. {
19. case '.': case '?': case '!': break;
20. default: System.out.println("INVALID
21. INPUT"); return;
22.
23.
24.
25.
26. }
27. System.out.println(s);
28. StringTokenizer st = new StringTokenizer(s, "? .!,");
29. int count = st.countTokens();
30. String a[] = new String[count];
for(int i = 0; i < count; i++) //separates each word of the sentence into
an element of array 'a[]'
a[i] = st.nextToken();
for(int
//arranges the i word
= 0; ialphabetically(
< a.length; i++) will be useful later to sort words
alphabetically when they have the same length)

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.

1. import java.io.*; //importing IO package


2. class ISBN //declaring class
3. {
4. public static void main(String args[])throws IOException
5. {
6. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7. //declaring BufferedReader object
8. System.out.print("ISBN Code: "); //inputs ISBN code and stores in String
9. 'isbn'
10. String isbn = br.readLine();
11. isbn = isbn.trim().toUpperCase(); //converts String 'isbn' to Uppercase
12.
13. int len = isbn.length(); //stores length of String 'isbn' in 'len'
14. char last = isbn.charAt(len - 1); //stores last character of String 'is
15. bn' int sum = 0;
16. int n = 10;
17. if(len != 10)
{ //checks if length of String 'isbn' is equal to 10 or not

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

ISBN Code: 0201530821

Output

SUM =99
LEAVES NO REMAINDER - VALID ISBN CODE
Input

ISBNCode: 035680324

Output

OUTPUT: INVALID INPUT

17
Input

ISBN Code: 0231428031

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.

1. import java.io.*; //importing IO Package


2. class Find //declaring class
3. {
4. public static void main(String args[])throws IOException //main function
5. {
6. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
7. //declaring bufferedreader object
8. int n, day, year, i, p, k=0;
9. int ar[]={0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//stores position of days of first months of the year
String br[]={"", "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"}; //stores month s of the
year
10. System.out.print("Day number: "); //taking inputs from user
11. day = Integer.parseInt(in.readLine()); //stores day number
12. System.out.print("Year: ");
13. year = Integer.parseInt(in.readLine()); //stores year
14. System.out.print("Day after (N Days): ");
15. n = Integer.parseInt(in.readLine()); //stores calue of N
16. p = day + n;
17. int a = 1, b = 1, date = 0, nday = 0, year1 = year;
18. if(year % 4 == 0) //checks if it is a leap year
19. {
20. for(i = 2; i <= 12; i++) //add 1 to subsequent months after februa
21. ry for the extra day(29th february)
22. { } if(day >= 1 && day <= 366) //checks if day is within
23. range {
ar[i] += 1;
24.
25.
26.
27. for(i = 0; i <= 12; i++) //calculates the month and date
28. {
29. if(ar[i] < day)
30. a = i;
31. if(ar[i] > day)
32. break;
33. }
34. date = day - ar[a];
35. }
36. else
37. k = 1;
38. if(k == 1) //prints error message if 'day' was not between 1 and 366
39. else proceeds further
40. System.out.println("Days are out of Range");
41. else if(k != 1 && n >= 1 && n <= 100)
42. {
43. if(p > ar[12]) //if p>366 then moves to over to the next year
44. {
45. p = p - ar[12];
46. year1 = year1 + 1;
}
for(i = 0; i <= 12; i++) //calculates the month and date

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.

1. class Special //declaring class


2. {
3. private int n; //to store the integer
4. public Special() { //constructor
} public Special(int
to assignnum)
0 to//parameterized
n. constructor to
5. assign a value to ‘n’ { } public void sum() //calculate and display the sum of
6. the first
n = 0;and last digit
7.
8.
9.
10.
11. n = num;
12.
13.
14. of ‘n’
15. {
16. int sum = 0;
17. String s = Integer.toString(n);
18. String first = s.substring(0, 1);
19. String last = s.substring(s.length() - 1);
20. sum = Integer.parseInt(first) + Integer.parseInt(last);
21. System.out.println("Sum of first and last digit: " + sum);
22. }
23. public void isSpecial() //check and display if the number ‘n’ is a special
24. number
25. {
26. int num = n; int
sum = 0;
while(num != 0) //extracts each digit of num and calulates the digit's
factorial and adds all the factorial
{

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

145isa special number.


Input

153

Output

153isnot a special number.

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().

1. import java.io.*; //importing IO Package


2. class Numbers //declaring class
3. {
4. private int n; //integer to be checked whether it is triangular or not
5. public void getNum()throws IOException //to accept the integer n
6. {
7. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8. //declaring BufferedReader object System.out.print("N =
9. "); n = Math.abs(Integer.parseInt(br.readLine()));
10.
11. }
12. public int check(int num) //to check if n is triangular
13. {
14. int sum = 0;
15. int i = 1;
16. if(num < 3) //if num<3 then return 0 as the least triangular number is 3
17.
18. return 0;
19. while(sum < num) //adds the consecutive integers starting from 1 while
20. the condition remains true
21. {
22. sum += i;
23. i++;
24. }
25. if(sum == num) //checks if 'sum' is equal to 'num' or not
26. return 1;
return 0;
}
public void display() //displays a suitable message depending on whether n
is triangular or not

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

Some of the members of the class are given below:

Class name: Convert


Data members/instance variables:
n: integer to store the day number.
d: integer to store the day of the month (date).
m: integer to store the month.
y: integer to store the year.
Methods/Member functions:
Convert(): constructor to initialize the data members with legal initial values.
void accept(): to accept the day number and the year.
void dayToDate(): converts the day number to its corresponding date for a particular
year and stores the date in ‘d’ and the month in ‘m’.
void display(): displays the month name, date and year.
Specify the class Convert giving details of the constructor, void accept(), void
dayToDate() and void display(). Define a main() function to create an object and call the
functions accordingly to enable the task.

1. import java.io.*; //importing IO Package


2. class Convert //declaring class
3. {
4. int n; int d; int m; int y;
5. public Convert() //constructor
6. {
7.
8.
9.
10. n = 0;
11. d = 0;
12. m = 0;
13. y = 0;
14. }
15. public void accept()throws IOException //main function
16. {
17. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18. //declaring BufferedReader object
19. System.out.print("Day number: "); //inputing day of the month and year
20.
21. d = Integer.parseInt(br.readLine());
22. System.out.print("Year: ");
23. y = Integer.parseInt(br.readLine());
24. if((y % 100 == 0 && y % 400 == 0) || y % 4 == 0) //checks for leap year
25. and century leap years
{
if(d > 366) //checks for invalid day number for a leap year
{

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

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

1. import java.io.*; //importing IO package


2. class CompositeMagic //declaring class
3. {
4. public static void main(String args[])throws IOException //main function
5. {
6. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7. //declaring BufferedReader object
8. System.out.print("m = ");
9. int m = Math.abs(Integer.parseInt(br.readLine()));
10. System.out.print("n = ");
11. int n = Math.abs(Integer.parseInt(br.readLine()));
12. if(m >= n) //checking if input is valid
13. {
14. System.out.println("INVALID INPUT");
15. return;
16. }
17. int count = 0;
18. System.out.println("THE COMPOSITE MAGIC INTEGERS ARE:");
19. for(int i = m; i <= n; i++) //counting composite magic number between m
20. and n {
21.
22. if(isComposite(i) && isMagic(i))
23. {
24. if(count == 0)
25. System.out.print(i);
26. else
27. System.out.print(", " + i);
28. count++;
29. }
30. }
31. System.out.println("\nFREQUENCY OF COMPOSITE MAGIC INTEGERS IS: " + count
32. );
33. }//main end
public static boolean isComposite(int num) //function to check for composite
number
{
int f = 0;

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.

1. import java.io.*; //importing IO Package


2. import java.util.StringTokenizer; //importing StringTokenizer Class
3. class Frequency //declaring class
4. {
5. String text; //stores the sentence
6. int countAnd; //to store the frequency of the word “and”
7. int countAn; //to store the frequency of the word “an”
8. int len; //stores the length of the string
9. public Frequency() //constructor to initialize the instance variables.
10. {
11. text = "";
12. countAnd = 0;
13. countAn = 0;
14. len = 0;
15. }
16. public void accept(String n) //to assign ‘n’ to ‘text’
17. {
18. text = n.toLowerCase();
19. countAnd = 0;
20. countAn = 0;
21. len = text.length();
22. }
23. public void checkAndFreq() //to count the frequency of “and”
24. {
25. StringTokenizer st = new StringTokenizer(text, "!.?, ");
26. int count = st.countTokens();
27. for(int i = 1; i <= count; i++)
28. {
29. String word = st.nextToken();
30. if(word.equals("and"))
31. countAnd++;
32. }
33. }

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:

Class name: Capital


Data members/instance variables:
sent: to store a sentence.
freq: stores the frequency of words beginning with a capital letter.
Member functions/methods:
Capital(): default constructor.
void input(): to accept the sentence.
boolean isCap(String w): checks and returns true if word begins with a capital letter,
otherwise returns false.
void display(): displays the sentence along with the frequency of the words beginning
with a capital letter.

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.

1. import java.io.*; //importing IO Package


2. import java.util.StringTokenizer; //improrting StringTokenizer Class
3. class Capital //class
4. {
5. private String sent; //to store a sentence.
6. private int freq; //stores the frequency of words beginning with a capital
7. letter
8. public Capital() //default constructor
9. {
10. sent = new String();
11. freq = 0;
12. }
13. public void input()throws IOException //to accept the sentence
14. {
15. InputStreamReader in = new InputStreamReader(System.in);
16. //declaring InputStreamReader object
17. BufferedReader br = new BufferedReader(in); //declaring BufferedReader
18. object System.out.print("Sentence: ");
19. sent = br.readLine();
20.
21. }
22. public boolean isCap(String w) //checks and returns true if word begins with
23. a capital letter, otherwise returns false
24. {
25. char ch = w.charAt(0);
26. if(Character.isUpperCase(ch))
27. return true;
28. return false;
29. }
public void display() //displays the sentence along with the frequency of the
words beginning with a capital letter
{
StringTokenizer st = new StringTokenizer(sent, " ,?!.");
int count = st.countTokens();

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

A sequence of fibonacci strings is generated as follows:


S0 = “a”, S1 = “b”, Sn = S(n – 1) + S(n – 2) where ‘+’ denotes concatenation. Thus the sequence
is a, b, ba, bab, babba, babbabab, … n terms.
Design a class FiboString to generate fibonacci strings. Some of the members of the
class are given below:

Class name: FiboString


Data members/instance variables:
x: to store the first string.
y: to store the second string.
z: to store the concatenation of the previous two strings.
n: to store the number of terms.
Member functions/methods:
FiboString(): constructor to assign x = “a”, y = “b” and z = “ba”.
void accept(): to accept the number of terms ‘n’.
void generate(): to generate and print the fibonacci strings. The sum of (‘+’ i.e.
concatenation) first two strings is the third string. Eg. “a” is first string, “b” is second
string, then the third will be “ba”, and fourth will be “bab” and so on.
Specify the class FiboString, giving details of the constructor, void accept() and void
generate(). Define the main() function to create an object and call the functions
accordingly to enable the task.

1. import java.io.*; //importing IO package


2. class FiboString //declaring class
3. {
4. private String x; //to store the first string. //to store the second string
5. private String y; //to store the concatenation of the previous two strings
6. private String z;
//to store the number of terms
7. private int n;
8.
9. public FiboString() //constructor to assign x=“a”,y=“b” and z=“ba”.
10. {
11. x = "a";
12. y = "b";
13. z = "ba";
14. n = 0;
15. }
16. public void accept()throws IOException //to accept the number of terms ‘n’
17. {
18.
19. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20. //declaring BufferedReader object
21. System.out.print("n = ");
22. n = Integer.parseInt(br.readLine());
23. }
24. public void generate() //to generate and print the fibonacci strings
25. {
26. if(n == 1)
27. System.out.print(x + "\t");
28. else if(n == 2)
29. System.out.print(x + "\t" + y + "\t");
else{
System.out.print(x + "\t" + y + "\t");

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.

Perform the following tasks:


(i) Convert the first letter of each word to uppercase.
(ii) Find the number of vowels and consonants in each word and display them with
proper headings along with the words.

1. import java.io.*; //importing IO package


2. class Title //declaring class
3. {
4. public static void main(String args[])throws IOException //main function
5. {
6. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7. //declaring BufferedReader object
8. System.out.print("String: "); //inputing sentence
9. String s = br.readLine();
10. s = s.trim(); //removes leading and trailing spaces
11. int len = s.length(); //stores lenth of string 's' in 'len'
12. char last = s.charAt(len - 1);
13. if(last != '.' && last != '?') //checks if string is valid or not
14. {
15. System.out.println("Invalid input.");
16. return;
17. }
18. String t = new String();
19. for(int i = 0; i < len; i++) //converts the first letter of each word
20. to Uppercase
21. {
22. char ch = s.charAt(i);
23. if(i == 0)
24. t += Character.toUpperCase(ch);
25. else if(ch == ' ')
26. {
27. t += " " + Character.toUpperCase(s.charAt(i + 1));
28. i++;
29. }
30. else
31. t += Character.toLowerCase(s.charAt(i));
32. }
33. System.out.println(t);
34. System.out.println("Word\t\tVowels\t\tConsonants");
35. String word = new String();
36. for(int i = 0; i < len; i++) //separates each word from the sentence
37. and passes them onto display(String w)
38. {
39. char ch = s.charAt(i);
40. if(ch == ' ' || ch == '.' || ch == '?')
41. {
42. display(word);
43. word = new String();
44. }
else
word += ch;
}

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

A class SeriesSum is designed to calculate the sum of the following series:


Sum = x2 / 1! + x4 / 3! + x6 / 5! + … + xn / (n – 1)!
Some of the members of the class are given below:

Class name: SeriesSum


Data members/instance variables:
x: to store an integer number.
n: to store number of terms.
sum: double variable to store the sum of the series.
Member functions:
SeriesSum(int xx, int nn): constructor to assign x = xx and n = nn.
double findFact(int m): to return the factorial of m using recursive technique.
double findPower(int x, int y): to return x raised to the power of y using recursive
technique.
void calculate(): to calculate the sum of the series by invoking the recursive functions
respectively.
void display(): to display the sum of the series.

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

Design a classMatRevtoreverseeach element of a matrix. Some of the members of


the class aregivenbelow:

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.

1. import java.io.*; //importing IO package


2. class MatRev //declaring class
3. {
4. private int arr[][]; //to store integer elements
5. private int m; //to store the number of rows
6. private int n; //to store the number of columns
7. public MatRev(int mm, int nn) //parameterized constructor to initialize the
8. data
9. {
10. m = mm; n = nn; arr =
11. new int[m][n];
12.
13. }
14. public void fillArray()throws IOException //to enter elements in the array
15. {
16.
17. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18. System.out.println("Enter matrix elements:");
19. for(int i = 0; i < m; i++)
20. {
21.
22. for(int j = 0; j < n; j++)
23. { }
24. arr[i][j] = Integer.parseInt(br.readLine());
25.
26. }
27. }
28. public int reverse(int x) //returns the reverse of the number x
29. {
30. int rev = 0;
for(int i = x; i != 0; i /= 10)
rev = rev * 10 + i % 10;
return rev;

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

1. import java.io.*; //imporing IO Package


2. class Goldbach //declaring class
3. {
4. public static void main(String args[])throws IOException //main function
5. {
6. int n = 0; int p = 3; int q = 0; InputStreamReader in =
7. new InputStreamReader(System.in);
8.
9. //declaring
10. InputStreamReader
11. BufferedReader br = new BufferedReader(in); //declaring BufferedReader
12. object System.out.print("N = ");
13. n = Integer.parseInt(br.readLine());
14.
15. if(n % 2 != 0) //checking if number is odd
16. {
17. System.out.println("Invalid input. Number is odd.");
18. return;
19. }
20. else if(n < 10 || n > 49) //checking if number is out of specified range
21. {
22.
23. System.out.println("Invalid input. Number is out of range.");
24. return;
25. }
26. System.out.println("Prime pairs are:");
27. while(p < n) //finding prime number pairs
28. {
29. q = n - p;
30. if(isPrime(p) && isPrime(q) && p <= q)
31. System.out.println(p + ", " + q);
32. p += 2;
33. }
34. }
35. public static boolean isPrime(int n) //function to check if numbers are
36. prime
37. {
int f = 0;
for(int i = 1; i <= n; i++)
{
if(n % i == 0)

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.

1. import java.io.*; //importing IO Package


2. import java.util.StringTokenizer; //importing StringTokenizer Class
3. class VowelWord //declaring class
4. {
5. String str; //to store a sentence
6. int freq; //store the frequency of the words beginning with a vowel
7. public VowelWord() //onstructor to initialize data members to legal initial
8. values
9. {
10. str = "";
11. freq = 0;
12. }
13. public void readStr()throws IOException //to accept a sentence
14. {
15. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16. //declaring BufferedReader object
17. System.out.print("Sentence: ");
18. str = br.readLine();
19. str = str.trim(); //removes leading and trailing spaces
20. int len = str.length(); //stores lenth of string 'str' in 'len'
21. char ch = str.charAt(len - 1);
22. if(ch != '.') //checks if sentence terminates with a full stop, if not
23. terminates the program
24. {
25. System.out.println("Sentence must terminate with a full stop.");
26. System.exit(0);
27. }
28. }
29. public void freqVowel() //counts the frequency of the words that begin with a
30. vowel
31. {
32. StringTokenizer st = new StringTokenizer(str, " .");
33. int count = st.countTokens();
for(int i = 1; i <= count; i++)
{
String word = st.nextToken();
word = word.toUpperCase();

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

Original string: Yesterday I had an unforgettable experience.


Required frequency: 4
Input

Sentence: I will graduate from a world class university

Output

Sentence must terminate with a full stop.

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.

1. import java.io.*; //importing IO package


2. class Disarium //declaring class
3. {
4. private int num;
5. private int size;
6. public Disarium(int n) //constructor
7. {
8. num = n;
9. size = 0;
10. }
11. void countDigit() //function to count the total number of digits and assigns
12. it to size.
13. {
14. for(int i = num; i != 0; i /= 10)
15. size++;
16. }
17. public int sumOfDigits(int n, int p) //function to return the sum of the di
18. gits of the number ‘n’ to the power of their respective positions
19. {
20. if(n < 10)
21. return (int)Math.pow(n, p);
22. else{
23. int t = (int)Math.pow(n % 10, p);
24. return t + sumOfDigits(n / 10, --p);
25. }
}
public void check() //function to check whether a number is a Disarium Number
or not and display a message

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

42. }//class end


43.

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.

1. import java.io.*; //importing IO Package


2. class Combine //declaring class
3. {
4. int com[]; //integer array
5. int size; //size of the array
6. public Combine(int num) //parameterized constructor to assign size = num
7. {
8. size = num;
9. com = new int[size];
10. }
11. public void inputArray()throws IOException // to accept the array elements
12. {
13. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14. for(int i = 0; i < size; i++)
15.
16. com[i] = Integer.parseInt(br.readLine());
17. }
18. public void sort() //sorts the elements of combined array in ascending order
19. using the selection sort technique
20. {
21. for(int i = 0; i < size; i++)
22. {
23. int small = com[i];
24. int pos = i;
25. for(int j = i + 1; j < size; j++)
26. {
27. if(small > com[j])
28. {
29. small = com[j];
30. pos = j;
31. }
32. }
33. int temp = com[i];
com[i] = small;
com[pos] = temp;

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:

12 434 643 764 23


Second array elements: 87
76 3424 76 7 76 5
Combined array: 23
5 7 12 76 87 434 643 764 3424

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

You might also like