Java Programs

Program [1]

Write a Program in Java to input a number and check whether it is a Keith Number or not.

Note: A Keith Number is an integer N with ‘d’ digits with the following property:

If a Fibonacci-like sequence (in which each term in the sequence is the sum of the ‘d’ previous terms) is formed, with the first ‘d’ terms being the decimal digits of the number N, then N itself occurs as a term in the sequence.

For example, 197 is a Keith number since it generates the sequence 1, 9, 7, 17, 33, 57, 107, 197, ………..

Some keith numbers are: 14 ,19, 28 , 47 , 61, 75, 197, 742, 1104, 1537……………

Sample

Enter the number : 197

The number is a Keith Number

Enter the number : 14

The number is a Keith Number

Enter the number : 53

The number is a not a Keith Number

Program [2]

Anagram :- When two strings contain the same characters irrespective of the order of occurrence then such strings are known as Anagram.

For Example :-

String1 = LISTEN

String2 = SILENT

So as we can see that both strings have same characters, only the order of characters is different so this is an Anagram.

Program [3]

Write a program to declare a square matrix M [ ] [ ] of order ‘N’ where ‘N’ must be greater than 3 and less than 10. Allow the user to accept three different characters from the keyboard and fill the array according to the instruction given below:

(i) Fill the four corners of the square matrix by character 1.

(ii) Fill the boundary elements of the matrix (except the four corners) by character 2.

(iii) Fill the non-boundary elements of the matrix by character 3.

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

Example 1:

INPUT: N = 4

FIRST CHARACTER: @

SECOND CHARACTER: ?

THIRD CHARACTER: #

OUTPUT:

@ ? ? @

? # # ?

? # # ?

@ ? ? @

Example 2:

INPUT: N = 5

FIRST CHARACTER: A

SECOND CHARACTER: C

THIRD CHARACTER: X

OUTPUT:

A C C C A

C X X X C

C X X X C

C X X X C

A C C C A

Example 3:

INPUT: N = 12

OUTPUT: SIZE OUT OF RANGE

Program [4]

Write a program to input a natural number less than 1000 and display it in words.

Test your program for the given sample data and some random data.

INPUT: 29

OUTPUT: TWENTY NINE INPUT: 17001

OUTPUT: OUT OF RANGE INPUT: 119

OUTPUT: ONE HUNDRED AND NINETEEN INPUT: 500

OUTPUT: FIVE HUNDRED

Program [5]

Write a  program  to  declare  a  square  matrix  A[  ][  ]  of  order  MxM  where  'M'  is  the  number  of  rows  and  the  number  of  columns,  such  that  M  must  be  greater  than  2  and    less  than 10.  Accept  the  value  of  M  as  user  input.  Display  an  appropriate  message  for    an  invalid  input.  Allow  the  user  to  input  integers   into   this   matrix.   Perform   the   following tasks:

(a) Display the original matrix.

(b) Rotate the matrix 90° clockwise as shown below:

Original matrix

1 2 3

4 5 6

7 8 9

Rotated matrix

7 4 1

8 5 2

9 6 3

(c) Find the sum of the elements of the four comers of the matrix.

Test your program with the sample data and some random data: Example 1

INPUT : M = 3

3 4 9

2 5 8

1 6 7

OUTPUT :

ORIGINAL MATRIX

3 4 9

2 5 8

1 6 7

MATRIX AFTER ROTATION

1 2 3

6 5 4

7 8 9

Sum of the corner elements = 20

Example 2

INPUT : M = 4

1 2 4 9

2 5 8 3

1 6 7 4

3 7 6 5

OUTPUT :

ORIGINAL MATRIX

1 2 4 9

2 5 8 3

1 6 7 4

3 7 6 5

MATRIX AFTER ROTATION
3 1 2 1
7 6 5 2
6 7 8 4
5 4 3 9
Sum of the corner elements = 18

Program [6]

Write a Program to search for given ITEM in a given array X[n] using liner search technique. If the ITEM is found, move it at top of array.if  the ITEM is not found, insert it at the end of the array.