Krishna File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 113

NAME:SHIKHA CHOWDHARY

CLASS:-XII-A (SCIENCE)
ROLL NO:-11
SUBJECT:-COMPUTER
APPLICATION FILE
SUBMITTED TO:-
ACKNOWLEGEMENT
MR.ASIF ISMAIL

1|Page
I am very thankful to my subject teacher Mr. Asif Ismail Sir for
giving me this project.
The project has been designed in an effective manner to provide
information on the subject. In this project the emphasis has
shifted from basic and general computer application to a specific
programming language. Moreover the emphasis in this project is
on object oriented programming through java in Bluej
Environment.
The project has computer programs on String
Manipulation,Number programs and Double-Dimensional Arrays.
I have tried my best to make the project as useful to its readers as
possible .I hope this project meets all your expectations.

→KRISHNA VOHRA
→CLASS- XII-B

PREFACE
2|Page
This project consist of some java programs which are done using array,
string, etc. In the whole project I have discussed about java and its
programs.

Although I have tried our best level to make this project error free and
upto the mark.I highly appreciate your suggestions and feedback to
improve this project in areas which have gone unnoticed.

INDEX
PROGR PROGRAM NAME PAGE
AM NO.1 NO.

3|Page
1. Display sum of the element above and below left diagonal 6
2. Display the elements above the left diagonal 9
3. Print the sum of its left and right diagonal elements 11
4. Display the sum of inner as well as outer elements of the 13
matrix
5. Print multipied matrix. 15
6. Display original matrix as mirror image of it 17
7. Transpose matrix 19
8. Highest value of each row of the given matrix 21
9. Display array after converting the diagonal element’s to 0 23
10. Display symmetric array 25
11. Display sum of both rows and columns 27
12. Display left and right diagonal of matrix as diagonal wise 29
13. Print boundary elements of the matrix 31
14. Print elements of array in ascending order 33
15. Display octal array of given array 36
16. Display array 90 degree anticlockwise 39
17. Display array 90 degree clockwise 41
18. Display multiple spaces from the matrix 43
19. Display string after reversing each word of the string 45
20. Display deleted first and last letter of the string 47
21. Display encode and decode of the string 49
22. Display longest word of the given string 52
23. Display the frequency of each word 54
24. Accept string in mixed case and display it in reversed case. 56
25. Display the string after removal of all vowels 58
26. Change case of each letter of the string 60
27. Modified the string 62
28. Replace first letter of each word with ‘x’ 64
29. Print frequency of each character present in the sentence 66
30. Display non-palindrome word to palindrome word 68

4|Page
31. Interchange first and last letter of each word of the string 70
32. Display robbin round word 72
33. Display rearrange word 74
34. Piglatin word 76
35. Goldbach number 78
36. Happy number 81
37. Twin prime number 83
38. Kaprekar number 85
39. Smith number 87
40. Composite number 90
41. Evil number 93
42. Unique digit integer 95
43. Teach number 97
44. Adam number 99
45. Luck number 101
46. Disarium number 103
47. IBSN number 105
48. Emirp number 107
49. Fascinating number 109
50. Palprime number 111

ARRAY PROGRAMS
PROGRAM 1:Write a program in java to store numbers in Double Dimensional
Array.Display the sum of the elements that are above and below left diagonal of
the matrix. .
ALGORITHM:

5|Page
Step 1:Defining the class name as”above_below”.
Step 2:Intialise int i,j,s1=0,s2=0;
Step 3:Accept the array int n[][]=new int[4][4];
Step 4: Enter and print the elements in the array n[][].
Step 5:Loop for sum of the above elements of left diagonal for(i=0;i<4;i++)
{
for(j=0;j<4;j++) {
if(j>i)
s1=s1+n[i][j];
}}
Step 6: print the sum of the above elements of left diagonal.
Step7:loop for sum of the below elements of left diagonal for(i=0;i<4;i++)
{
for(j=0;j<i;j++) {
s2=s2+n[i][j];
}}
Step 8: print the sum of the below elements of left diagonal
SOLUTION:
import java.util.*;
public class above_below
{//display the elements above right diagonal
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
int i,j,s1=0,s2=0;
int n[][]=new int[4][4];//accept the array
System.out.println("Enter the elemnets in the matrix");
for(i=0;i<4;i++)
{//enter the elements in the matrix
for(j=0;j<4;j++)
{
n[i][j]=sc.nextInt();
}
}

6|Page
System.out.println("original matrix ");
for(i=0;i<4;i++)
{//print the elements in the matrix
for(j=0;j<4;j++)
{
System.out.print(n[i][j]+" ");
}
System.out.println();
}
for(i=0;i<4;i++)
{//find sum of the above elements of left diagonal
for(j=0;j<4;j++)
{
if(j>i)
s1=s1+n[i][j];
}
}
System.out.println("the sum of the elements above left diagonal="+ s1);
for(i=0;i<4;i++)
{//find sum of the below elements of left diagonal
for(j=0;j<i;j++)
{
s2=s2+n[i][j];
}
}
System.out.println("the sum of the elements below left diagonal="+ s2);
}
}

7|Page
Output 1:

Program 2:Write a program in java to store number in 4*4 matrix in Double


Dimensional Array.Display the elements above the left diagonal.
AGLORITHM:

8|Page
Step 1: Define a class name by”above_elements”.
Step2:create scanner class object.
Step 3: Initialize a matrix int num[][]=new int[4][4];
Step 4:Accept and print elements in the matrix num[][]
Step 5:to print elements above left diagonal
for(i=0;i<4;i++) {
for(j=0;j<4;j++) {
if(j>i)
System.out.print(num[i][j]+" "); }
System.out.println(); }
Step 6: print the elements above left diagonal .
SOLUTION:
import java.util.*;
public class above_elements
{//display the elements above right diagonal
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
int i,j;
int num[][]=new int[4][4];//accept the array
System.out.println("Enter the elements in the matrix");
for(i=0;i<4;i++)
{//enter the elements in the matrix
for(j=0;j<4;j++)
{
num[i][j]=sc.nextInt();
}
}
System.out.println("original matrix ");
for(i=0;i<4;i++)
{//print the elements in the matrix
for(j=0;j<4;j++) {
System.out.print(num[i][j]+" "); }
System.out.println();
}
System.out.println("the elements above left diagonal");
for(i=0;i<4;i++)
9|Page
{// print elements above left diagonal
for(j=0;j<4;j++)
if(j>i)
System.out.print(num[i][j]+" ");
else
System.out.println(" ");
}
}
} Output 2:

PROGRAM 3: Write a
program to enter the
numbers in 4 by 4

10 | P a g e
matrix in double dimensional array and print the sum of its left and right diagonal
elements.
ALGORITHM::
Step 1: define a class by “diagonal”.
Step 2: Input the elements in array m[4][4]
Step 3: Loop for left diagona elements from i=0 to i<4.
Step 4: to find the sum of the left diagonal elements ld=ld+m[i][i]
Step 5:Loop for right diagonal from i=0 toi<4
Step 6:to find the sum of the right diagonal elements rd =rd+M[i][k]
Step 7 display the sum of the left diagonal elements “ld”.
Step 8:display the sum of the elements of right diagonal “rd”.
SOLUTION:
import java.util.*;
class diagonal
{//to find sum of the left and right diagonal
public static void main(String args[])
{//create Scanner class object
int j,i,ld=0,rd=0;
int m[][]=new int[4][4];//acceptin the array from user
Scanner in=new Scanner(System.in);
System.out.println("Enter the matrix");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{//enter the elements in the array
m[i][j]=in.nextInt();
}
}
System.out.println("Enter the elements in the array ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)

11 | P a g e
{//enter the elements in the array
System.out.print(m[i][j]+" " );
}
System.out.println();
for(i=0;i<4;i++)
{ //sum of the left diagonal
for(j=0;j<4;j++)
{ if(i==j)
ld=ld+m[i][j];
} System.out.println("The sum of left diagonal elements = "+ld);
}//print the sum of left diagonal
for(i=0;i<4;i++)
{//sum of the right diagonal
for(j=0;j<4;j++) Output 3:
if((i+j)==(3-1))
rd=rd+m[i][j];}//print sum of the right
diagonal
System.out.println("The sum of right diagonal
elements = "+rd);
}
}
}

12 | P a g e
PROGRAM 4: Write a program to create a Double Dimension Array whose
dimensions are decided by the user and accept the integer elements in it. Find the
sum of inner as well as outer elements.
Algorithm:
Step 1: A class of name “sumelements” is defined.
Step 2: Input of number of rows is taken from the user.
Step 3: An array of variable of name ‘a’ is defined. Numbers are accepted in that
array. And the original array is printed on the screen.
Step 4: A combination of multiple loops are used to perform the task. The sum of
inner as well as outer elements and are printed on the output screen.
SOLUTION:
import java.util.*;
public class sumelements
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of row in the array.");
int r=sc.nextInt();//Enter the number of the row
int a[][]=new int[r][r];// accept the array
int i,p,o,j,sum=0,sum_in=0;
System.out.println("Enter number in the array.");
for(i=0;i<r;i++)
{//enter numbers in the array
for(j=0;j<r;j++)
a[i][j]=sc.nextInt();
}
System.out.println("The original array is:-");
for(i=0;i<r;i++)
{//printing the original array
for(j=0;j<r;j++)
{//find the sum of outer elements
sum=sum+a[i][j];

13 | P a g e
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println();
p=r/2;
o=p+1;
for(i=p;i<=o;i++)
{//find the sum of the inner elements
for(j=p;j<=o;j++)
sum_in=sum_in+a[i][j];
}//print total sum , sum of outer and inner elements in the array
System.out.println("Total sum of elements = "+sum);
System.out.println("Total sum of outer elements = "+(sum-sum_in));
System.out.println("Total sum of inner elements = "+sum_in);
}
}
Output 4:

14 | P a g e
PROGRAM 5: Write a program to create two Double Dimensional Arrays whose
dimensions are same and decided by the user. Print the multiplied matrix .
Algorithm:
Step 1: A class of name “xyz” is defined.
Step 2: Input of number of rows is taken from the user.
Step 3: Threearrays of variable of name ‘a’, ’b’, and ‘c’ is defined. Numbers are
accepted in array a and b. And the multiplied array is stored in c.
Step 4: Array ‘c’ is printed on the output screen.
Step 5: End
SOLUTION:
import java.util.*;
public class xyz
{//Print the multiplied matrix
public static void main(String args[])
{//create scanner object class
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of row in the array.");
int r=sc.nextInt();//enter number of row in the array
int a[][]=new int[r][r];
int b[][]=new int[r][r];
int c[][]=new int[r][r];
int i,j,temp=0,k;
System.out.println("Enter number in the array A.");
for(i=0;i<r;i++)
{//Enter number in the array A
for(j=0;j<r;j++)
a[i][j]=sc.nextInt();
}
System.out.println("Enter number in the array b.");
for(i=0;i<r;i++)
{//Enter number in the array b
for(j=0;j<r;j++)

15 | P a g e
b[i][j]=sc.nextInt();
}
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
c[i][j]=a[i][j]*b[i][j];
}//printing new array
System.out.println();
System.out.println("The new array is:-");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++) OUTPUT 5:
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}

16 | P a g e
PROGRAM 6:Write a program to create an array whose dimensions are decided
by the user. Display the original matrix as well as the mirror image of the
original matrix.
Algorithm:
Step 1: A class of name “mirror image” is defined.
Step 2: Input of number of rows is taken from the user.
int r=sc.nextInt();
int a[][]=new int[r][r];
Step 3: An array of variable of name ‘a’ is defined. Numbers are accepted in that
array. And the original array is printed on the screen.
int a[][]=new int[r][r];{
for(i=0;i<r;i++) { for(j=r-1;j>=0;j--) {
System.out.print(a[i][j]); }
System.out.println(); }
Step 5:End.
SOLUTION:
import java.util.*;
public class mirror_image
{//find the mirror image of the matrix
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][3];
int i,j;
System.out.println("Enter number in the array.");
for(i=0;i<3;i++)
{//enter nubers in the array
for(j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("The original array is:-");
17 | P a g e
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{//print the original matrix
System.out.print(a[i][j]);
}
System.out.println();
}
System.out.println();
System.out.println("The Mirror image of the array is:-");
for(i=0;i<3;i++)
{//mirror image of the array
for(j=3-1;j>=0;j--)
{
System.out.print(a[i][j]);
}
System.out.println();
}//printing the mirror image of the matrix
}
} OUTPUT 6:

18 | P a g e
Program 7:Transpose of a matrix means to change row elements into column
elements into rows.Write a program to store numbers in a 4×4 matrix in Double
Dimensional Array and display the transpose of the matrix.
ALGORITHM:
Step 1: Start by inputing class name “transpose”.
Step 2: Input of 16 elements in a double dimension array is taken by the user.
Step 2: With the help of a loop transposition of 1st row with the 4th row is made.
Step 3: New array is printed with the original array.
Step 5: End.
SOLUTION:
import java.util.*;
public class transpose
{//transpose the elements of column into rows
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];//accept the elements in the array
int i,j,temp;//enter the elements in the matrix
System.out.print("Enter number in the array.");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{//loop for entering the elements fron user
a[i][j]=sc.nextInt();
}
}//printing the original matrix
System.out.println("The original array is:-");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");

19 | P a g e
}
System.out.println();
}//print the Transpose matrix
System.out.println("The transposed array is:-");
for(i=0;i<4;i++)
{//swaping of the elements of array
temp=a[0][i];
a[0][i]=a[3][i];
a[3][i]=temp;
}
for(i=0;i<4;i++)
{ Output 7:
for(j=0;j<4;j++)
{//display tranpose matrix
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

20 | P a g e
PROGRAM 8: Write a program to create a Double Dimension Array whose
dimensions are decided by the user and accept the integer elements in it. Print the
highest value of rows of given matrix.
Algorithm:
Step 1: A class of name “highest” is defined.
Step 2: Input of number of rows is taken from the user.
Step 3: An array of variable of name ‘a’ is defined. Numbers are accepted in that
array. And the original array is printed on the screen.
Step 4: A combination of multiple loops are used to perform the task. First of all
sorting is made and the highest value of each row is printed on the output screen.
Step 5: End.
Solutions:
import java.util.*;
public class highest
{//to find the highest value of the row
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
System.out.println("Enter numbers of row in the array.");
int r=sc.nextInt();//Enter the number of row
int a[][]=new int[r][r];//enter the elements
int i,j,temp=0,k;
System.out.println("Enter number in the array.");
for(i=0;i<r;i++)//Enter number in the array
{
for(j=0;j<r;j++){
a[i][j]=sc.nextInt();
}
System.out.println("The original array is:-");
for(i=0;i<r;i++)//print the original array
{
for(j=0;j<r;j++)
{

21 | P a g e
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(i=0;i<r;i++)
{
for(j=0;j<r-1;j++)
{
for(k=0;k<r-1-j;k++)
if(a[i][k]>a[i][k+1])
{//swapping if the elements
temp=a[i][k];
a[i][k]=a[i][k+1];
a[i][k+1]=temp;
}}}
System.out.println("");
for(i=0;i<r;i++)
{//display the highest value of the row
System.out.println("Highest value of row "+(i+1)+" is ="+a[i][r-1]);
} OUTPUT 8:
}
}
}

22 | P a g e
PROGRAM 9: Write a program to create a Double Dimension Array whose
dimensions are decided by the user and accept the integer elements in it. Print the
array after converting the diagonal element’s to 0.
Algorithm:
Step 1: A class of name “d” is defined.
Step 2: Input of number of rows is taken from the user.
Step 3: An array of variable of name ‘a’ is defined. Numbers are accepted in that
array. And the original array is printed on the screen.
Step 4: A combination of multiple loops are used to change the diagonals zero.
Step 5: New array is printed on the output screen.
Step 6:End.
SOLUTION:
import java.util.*;
public class d
{//replace diagonaal elements with zero
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][3];
int i,j;//enter the elements in the array
System.out.println("Enter number in the array.");
for(i=0;i<3;i++)
{//accepting the elements in the array
for(j=0;j<3;j++)
a[i][j]=sc.nextInt();
}
System.out.println("The original array is:-");
for(i=0;i<3;i++)
{//print the original array
for(j=0;j<3;j++)
System.out.print(a[i][j]);
System.out.println();
}
23 | P a g e
System.out.println();
for(i=0;i<3;i++)
{//replace diagonal with zero
for(j=0;j<3;j++)
if(j==i)
a[i][j]=0;
}
int p=3-1;
for(i=0;i<3;i++)
{
a[i][p]=0;
p--;
}//print new matrix
System.out.println("The original array is:-");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
System.out.print(a[i][j]);
System.out.println(); OUTPUT 9:
}
}
}

24 | P a g e
PROGRAM 10: Write a program to create a Double Dimension Array whose
dimensions are decided by the user and accept the integer elements in it. Check
that the Double Dimension Array is Symmetric or not.
Algorithm:
Step 1: A class of name “AB” is defined.
Step 2: Input of number of rows is taken from the user.
Step 3: An array of variable of name ‘a’ is defined. Numbers are accepted in that
array. And the original array is printed on the screen.
Step 4: A combination of multiple loops are used to to perform the task. If any ith
row and jth column value is not equal to jth row and ith column, the program stops
running. And an appropriate message is displayed.
Step 5:End.
SOLUTION:
import java.util.*;
public class AB
{//Check that the Double Dimension Array is Symmetric or not.
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];//enter the elements in the array
int i,j,K=0;
System.out.println("Enter number in the array.");
for(i=0;i<4;i++)
{//print the numbers in the elements
for(j=0;j<4;j++)
a[i][j]=sc.nextInt();
}//print the original array
System.out.println("The original array is:-");
for(i=0;i<4;i++)
{//printing the elements in the array
for(j=0;j<4;j++)
System.out.print(a[i][j]);
System.out.println();

25 | P a g e
}
System.out.println();
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{//logic to find the non symmetric array
if(a[i][j]!=a[j][i])
{
K=1;
System.out.println("Non Symmetric array....");
}
}
}//to find whether the array is symmetric or not
if(K!=1)
System.out.println(" Symmetric array....");
}
}
Output 10:

26 | P a g e
PROGRAM 11: Write a program to create a Double Dimension Array whose
dimensions are decided by the user and accept the integer elements in it. Print the
sum of both rows and columns.
Algorithm:
Step 1: A class of name “sum” is defined.
Step 2: Input of number of rows is taken from the user.
Step 3: An array of variable of name ‘a’ is defined. Numbers are accepted in that
array. And the original array is printed on the screen.
Step 4: Two loops are required for the sum, one for row and other for column.
Step 5: Sum is printed on the output screen with the original array.
Step 6:End.
SOLUTION:
import java.util.*;
public class sum
{//print the sum of row and column
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of row in the array.");
int r=sc.nextInt();//no. of rows
int a[][]=new int[r][r];//accepting the array
int i,j,sum=0,sum_c=0;
System.out.println("Enter number in the array.");
for(i=0;i<r;i++)
{//entering the elments in the array
for(j=0;j<r;j++)
{//accept the elements in array
a[i][j]=sc.nextInt();
}
}//print the original matrix
System.out.println("The original array is:-");
for(i=0;i<r;i++)
{//display the elements in the array
27 | P a g e
for(j=0;j<r;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
System.out.println();
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{//sum of rows
System.out.print(a[i][j]+" ");
sum=sum+a[i][j];
}
System.out.print(sum);
sum=0;
System.out.println(); Output 11:
}
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{//sum of the columns
sum_c=sum_c+a[j][i];
}
System.out.print(sum_c+" ");
sum_c=0;
}
}
}

28 | P a g e
PROGRAM 12: Write a program to accept elements the diagonals of a 4*4 matrix.
Display the left and right diagonals as diagonal wise .
ALGORITHM:
Step 1: Define a class by name “as”.
Step 2: accept elements in array a[][].
Step 3: With the help of two loops and one counter variable whose value is
decreasing by1.Then the value of diagonals are printed on the output screen.
Step 4: End.
SOLUTION:
import java.util.*;
public class as
{//print diagonals of the matrix
public static void main(String args[])
{//create scanner object
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];//accept the array fom user
int i,j,sum=0;
System.out.println("Enter 16 numbers in the array.");
for(i=0;i<4;i++)
{//enter 16 elements in the array
for(j=0;j<4;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]);
}
System.out.println();

29 | P a g e
}
int p=4;
for(i=0;i<4;i++)
{
p-=1;
for(j=0;j<4;j++)
{
if((j==i)||(j==p))
{//To print the array
System.out.print(a[i][j]);
else
System.out.print(" "); Output 12:
}
System.out.println();
}
}
}

30 | P a g e
PROGRAM 13: Write a Program in Java to input a 2-D array of size ‘4*4’ and print
its boundary (border) elements.
ALGORITHM:
Step 1: A class of name “boundary ” is created.
Step 2: Number are accepted from the user in a 4X4 matrix.
for(i=0;i<4;i++) {
for(j=0;j<4;j++) {
System.out.print("Enter the elements: ");
A[i][j]=sc.nextInt(); } }
Step 3:With the help of conditions ,loops and situations to select the boundary
elements.
for(i=0;i<4;i++) {
for(j=0;j<4;j++) {
if(i==0 || j==0 || i == 4-1 || j == 4-1) //condition for accessing boundary
elements
System.out.print(A[i][j]+"\t");
else
System.out.print(" \t") }
System.out.println(); } } }
Step 4:Now the selected elements are printed on the output.

SOLUTION:
import java.util.*;
class boundary
{//To print the boundary elements
public static void main(String args[])
{//create scanner class object
int i,j,m,n;//initialise variable required
Scanner sc=new Scanner(System.in);
int A[][]=new int[4][4];//accepting the array
for(i=0;i<4;i++)
{//accept the elements in the array from user
for(j=0;j<4;j++)

31 | P a g e
{
System.out.print("Enter the elements: ");
A[i][j]=sc.nextInt();
}
}//print boundary elements
System.out.println("The Boundary Elements are:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==0 || j==0 || i == 4-1 || j == 4-1) //condition for accessing boundary
elements
System.out.print(A[i][j]+"\t");
else
System.out.print(" \t"); Output 13:
}
System.out.println();
}
}
}

32 | P a g e
PROGRAM 14:Write a program in java to store the number in 4*4 matrix in Double
dimensional array. Arrange the numbers of the each row in ascending order and
display the results.
ALGORITHM:
Step 1: Define class name as”a”.
Step 2: Accept and print the elements in the array m[][].
Step 3:loop for arraging in ascending order: for(i=0;i<4;i++) {
for(j=0;j<3;j++) {
for(k=0;k<3-j;k++)
if(m[i][k]>m[i][k+1}) {
t=m[i][k];
m[i][k]=m[i][k+1];
m[i][k+1]=t;
}
Step 4: loop for printing elements after arranging each row in ascending order:
for(i=0;i<4;i++) {//print the elements in the matrix
for(j=0;j<4;j++) {
System.out.print(m[i][j]+" ");
} System.out.println();
}
SOLUTION:
import java.util.*;
public class a
{//display the elements above right diagonal
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
int i,j,k,t;
int m[][]=new int[4][4];//accept the array
System.out.println("Enter the elemnets in the matrix");
for(i=0;i<4;i++)
{//enter the elements in the matrix

33 | P a g e
for(j=0;j<4;j++)
{
m[i][j]=sc.nextInt();
} }
System.out.println("original matrix ");
for(i=0;i<4;i++)
{//print the elements in the matrix
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3-j;k++)
if(m[i][k]>m[i][k+1])
{
t=m[i][k];
m[i][k]=m[i][k+1];
m[i][k+1]=t;
}}}
System.out.println("Elements after arranging each row in ascending order");
for(i=0;i<4;i++)
{//print the elements in the matrix
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
34 | P a g e
}
}
}
OUTPUT 14:

35 | P a g e
Program 15: Write a program to declare a matrix A[][] of order (M x N) where 'M' is
the number of rows and 'N' is the number of columns such that the value of 'M'
must be greater than 0 and less than 10 and the value of 'N' must be greater than 2
and less than 6. Allow the user to input digits (0 - 7) only at each location, such that
each row represents an octal number.
ALGORITHM:
Step 1:Define a class as “octalmatrix”.
Step 2: Create scanner class object
Step 3:Accept number of rows and columns from user.
Step 4:Accept the elements from user:-
for (int i = 0; i < m; i++) {
System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");
for (int j = 0; j < n; j++){
a[i][j] = in.nextInt();//conditions for octal matrix
if (a[i][j] < 0 || a[i][j] > 7) {
System.out.println("INVALID INPUT");
return;}}}
Step 5:Conditions for octal matrix:-
for (int i = 0; i < m; i++) {
int decNum = 0;
for (int j = 0; j < n; j++) {
decNum += a[i][j] * Math.pow(8, n -j-1 );
System.out.print(a[i][j] + " "); }
Solution:
import java.util.Scanner;
public class OctalMatrix
{//to find octal matrix
public static void main(String args[])
{//to create scanner class object
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of rows (M): ");
int m = in.nextInt();//the number of rows
System.out.print("Enter the number of columns (N): ");

36 | P a g e
int n = in.nextInt();//the number of columns
//range for octal matrix
if (m <= 0 || m >= 10 || n <= 2 || n >= 6){
System.out.println("OUT OF RANGE");
return; }
int a[][] = new int[m][n];//to accept an array
//execution of loop
for (int i = 0; i < m; i++)
{
System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": ");
for (int j = 0; j < n; j++)
{
a[i][j] = in.nextInt();//conditions for octal matrix
if (a[i][j] < 0 || a[i][j] > 7) {
System.out.println("INVALID INPUT");
return;}}}
System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT");
//display octal matrix
for (int i = 0; i < m; i++) {
int decNum = 0;
for (int j = 0; j < n; j++) {
decNum += a[i][j] * Math.pow(8, n -j-1 );System.out.print(a[i][j] + " ");

}
System.out.print("\t\t" + decNum);
System.out.println();
}
}}

37 | P a g e
Output15:

38 | P a g e
Program 16: Write a program to enter the numbers in 2dimensional matrix in
double dimensional array and rotate and display the matrix 90 degree
anticlockwise.
ALGORITHM: Step 1:Define a class as “anticlockwise_90”.
Step 2: Create scanner class object
Step 3:Accept number of rows and columns of 2D array from user.
Step 4:Accept the elements of the array from the user.
Step 5: Rotate and display the array anticlockwise 90 degree.
for(i=m-1;i>=0;i--)//printing Anticlockwise 90 rotated matrix
{ for(j=0;j<n;j++){
System.out.print(a[j][i]+" ");
}
System.out.println();
}}
Solution:
import java.util.*;// scanner class package
public class anticlockwise_90
{//creating a class
public static void main(String args[])
{
int i,j;// variables for initialisation of variables
Scanner sc = new Scanner(System.in);// to create scanner class object
System.out.print("Enter the number of rows (M): ");
int m = sc.nextInt();//accept the number of rows from user
System.out.print("Enter the number of columns (N): ");
int n = sc.nextInt();//accept the number of columns from user
int a[][]=new int[m][n];//accepting array
System.out.print("Enter elements of the array");//accepting the elements of the
array
for(i=0;i<m;i++)//loop for rows
{
for(j=0;j<n;j++)//loop for column
{
39 | P a g e
a[i][j]=sc.nextInt();//storing the elements of the array
}
}
System.out.print("original matrix:");//printing original array
for(i=0;i<m;i++)//loop for rows
{
for(j=0;j<n;j++)//loop for columns
{
System.out.print(a[i][j]+" " );
}
System.out.println();
}
System.out.println(" Anticlockwise 90 rotated matrix ");
for(i=m-1;i>=0;i--)//printing Anticlockwise 90 rotated matrix
{
for(j=0;j<n;j++) Output 16:
{
System.out.print(a[j][i]+" ");
}
System.out.println();
}
}
}

40 | P a g e
Program 17: Write a program to enter the numbers in 2dimensional matrix in double
dimensional array and rotate and display the matrix 90 degree clockwise.
ALGORITHM:
Step 1:Define a class as “anticlockwise_90”.
Step 2: Create scanner class object
Step 3:Accept number of rows and columns of 2D array from user.
Step 4:Accept the elements of the array from the user.
Step 5: Rotate and display the array anticlockwise 90 degree.
for(i=0;i<m;i++) //printing clockwise 90 rotated matrix
{
for(j=n-1;j>=0;j--)
{
System.out.print(a[j][i]+" ");}
System.out.println();}}
Solution:
import java.util.*;
public class clockwise_90
{ //creating a class
public static void main(String args[])
{
int i,j;// variables for initialisation of variables
Scanner sc = new Scanner(System.in);// to create scanner class object
System.out.print("Enter the number of rows (M): ");
int m = sc.nextInt();//accept the number of rows from user
System.out.print("Enter the number of columns (N): ");
int n = sc.nextInt();//accept the number of columns from user
int a[][]=new int[m][n];//accepting array
System.out.print("Enter elements of the array");
//accepting the elements of the array
for(i=0;i<m;i++)//loop for rows
{
for(j=0;j<n;j++)//loop for column

41 | P a g e
{
a[i][j]=sc.nextInt();//storing the elements of the array
}
}
System.out.print("original matrix:");//printing original array
for(i=0;i<m;i++)//loop for rows
{
for(j=0;j<n;j++)//loop for columns
{
System.out.print(a[i][j]+" " );
}
System.out.println();
}
System.out.println("clockwise 90 rotated matrix ");
for(i=0;i<m;i++) //printing clockwise 90 rotated matrix
{
for(j=n-1;j>=0;j--) Output 17:
{
System.out.print(a[j][i]+" ");
}
System.out.println();
}
}
}

42 | P a g e
STRING PROGRAMS
PROGRAM 18: Write a program to accept a string and delete the multiple spaces
from the string which are present in between the words of the string.
Sample input: Understanding computer science
Sample Output: Understanding computer science
algorithm
Step 1: Start by defining class name “s1”.
Step 2: An initial string is taken from the user.
Step 3: The string is broken into words, and then the words are saved in the
condition that they have only one space between them.
Step 4: New string is printed on the output screen.
Step 5: End.
Solution:
import java.util.*;
public class s1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence...");// accepting the sentence from user
String str=sc.nextLine();
int l=str.length();//finding the length of the sentence
int i,j;
String s=" ";//allocating space at the beginning of the sentence
for(i=0;i<l;i++)//loop execute
{
char ch=str.charAt(i);// converting character into integer type data
if(ch!=' ')// checking the condition for multipe conditions
{

43 | P a g e
s=s+ch;
}
else
{
for(j=i;j<l;j++)// else part loop executed
{
char c=str.charAt(j);
if(c!=' ')
{
i=j;
break;
}
}
i=i-1;
s=s+' ';
}
}
System.out.println("Output:- "+s);
}
}
OUTPUT 18:

PROGRAM 19: Write a program to accept a string display it after reversing each
word of the string.
44 | P a g e
Sample Input :- know your worth
Sample Output:- wonk ruoy htrow
ALGORITHM:
Step 1: Start by defining class name “s2”.
Step 2: An initial string is taken from the user.
Step 3: The string is broken into words, and then the words are saved in reverse
order.
Step 4: The new string is printed on the output screen.
Step 5: End.
SOLUTION:
import java.util.*;
public class s2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");//accepting the string from user
String st=sc.nextLine();
String st1=" ",st2=" "; int i;
char chr,chr1;
st=st+" ";//allocating space
int p=st.length();//finding length of the string
for(i=0;i<p;i++)//executing loop
{
chr=st.charAt(i);
if(chr==' ')//checking conditions
{
st2=st2+' '+st1;//reversing the string
st1=" ";
}
else
st1=chr+st1;

45 | P a g e
}
System.out.println("New string -"+st2);//printing the string
}
}
OUTPUT 19:

PROGRAM 20:Write a program to accept a sentence delete the first and the last
character of each word.

46 | P a g e
Sample Input:yesterday is heavy put it own
Sample output: esterda eav u w
ALGORITHM:
Step 1: Start by defining class name “s3”.
Step 2: Initial string is accepted from the user.
Step 3: Each word of the string is broken and the first and the last word of each word
is deleted and the new word is printed on the output screen.
Step 4: End.
SOLUTION :
import java .util.*;
public class s3//defining class name
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s,st="",pw="";int i,len,l,j;//initialising variables
System.out.println("Enter the String");//accepting string from user
s=sc.nextLine();
s=s+" ";//allocating space in the string
String o=" ";
l=s.length();//finding the length of the string
for(i=0;i<l;i++)//executing the loop
{
char c=s.charAt(i);
if(c!=' ')//checking the condition
st=st+c;
else
{
st=st.trim();//trimming the string
len=st.length();//length
for(j=1;j<len-1;j++)//executing another loop
{

47 | P a g e
char ch=st.charAt(j);
pw=pw+ch;
}
System.out.print((pw.trim()+" "));//printing new string
pw=" ";
st=" ";
}
}
}
}
OUTPUT 20:

PROGRAM 21:Write a program to accept a string and decode or encode it as the


user wishes.
ALGORITHM:

48 | P a g e
Step 1: Start by defining class name “s4”.
Step 2: Choice is given to the user in through the main function as he wants to
encode or decode the string. As soon as the user chooses the option ,he is asked to
enter the string.
Step 3: The whole string converted in an uppercase string. The user is asked to enter
the number of character he wants to escape.
Step 4: An object of class eord is created as ob. These values are been send to
another function name (accept), which get the choice, the string, and the number of
character he wants to escape.
Step 5: In this function, a switch case runs according to the user’s choice. In the first
case encoding is done, and decoding in second case. At last the encoded or decoded
string is printed on the terminal window.
Step 6: End.
SOLUTION:
import java.util.*;
public class s4//definning the the class name
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 to Encode a String.");//entering 1 to encode string
System.out.println("Enter 2 to Decode a String.");//entering 2 to decode string
System.out.println("ENTER YOUR CHOICE");// enter your choice
int ch=sc.nextInt();
System.out.println("Enter a string.");//accepting the string
String s=sc.next();
s=s.toUpperCase();//converting to upper case
System.out.println("Enter the number of alphabets to be escape.");
int esc=sc.nextInt();//number of alphabets to be escape
int l=s.length();//finding the length of the string
String str=" ";
int i,t=0;
switch(ch)
{
49 | P a g e
case 1://case 1 block begin
for(i=0;i<l;i++)//loop execute
{
char c=s.charAt(i);
t=(int)c;
t=t+esc;
if(t>90)//checking conditions
t=t-26;
c =(char)t;
str=str+c;//new string
}
System.out.println("New String--"+str);
break;//case 1 terminate
case 2://case 2 block begin
for(i=0;i<l;i++)//looping execute
{
char c=s.charAt(i);
t=(int)c;
t=t-esc;
if(t<65)
t=t+26;
c=(char)t;
str1=str1+c;
}
System.out.println("New String--"+str1);//new string printing
break;
default:
System.out.println("WRONG CHOICE");
}
}
}

50 | P a g e
OUTPUT 21:

51 | P a g e
Program 22: Write a program in Java to enter a String/Sentence and display the
longest word and the length of the longest word present in the String.
Sample Input: “TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN”
Sample Output: The longest word: FOOTBALL: The length of the word: 8
ALGORITHM:
Step 1: accept the string from user.
Step 2 : find length of the string .
Step 3:loop for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
if (ch == ' ') {
if (word.length() > lWord.length())
lWord = word;
word = ""; }
else {
word += ch; } }
Step 4: print the longest word.
Solution:
import java.util.Scanner;
public class LongestWord
{//create scanner class object
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word or sentence:");
String str = in.nextLine();//accept string
str += " "; //Add space at end of string
String word = "", lWord = "";
int len = str.length();//fingd the length of the string

52 | P a g e
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);//int to char conversion
if (ch == ' ')
{
if (word.length() > lWord.length())
lWord = word;
word = "";
}
else {
word += ch;
}
}//print longest word
Sys
tem.out.println( "Th
e longest word: " +
lWord +
": The
length of the
word: " +
lWord.length());
}
}
Output 22:

53 | P a g e
Program 23: Write a program to accept a string and a word. Display the frequency
of that word.
Sample input: Life is so unpredictable
Sample output: unpredictable no of frequency =1
ALGORITHM:
Step 1: An initial string is taken from the user.
Step 2: The string is broken into words, and then the word matches with the word
which is to be searched, a counter variable increases its value by1.
Step 3: The frequency is printed on the output screen.
Step 4: End.
Solution:
import java.util.*;
public class s7//defining the class name
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String word="";//initialsing the variable for word
int sp=0,c=0;
System.out.println("Enter your String"); //accepting the string
String sent =sc.nextLine();
sent=sent+" ";
int len=sent.length();//findining the length of the sentence

54 | P a g e
System.out.println("Enter the word whose frequency is to be found");//enter the
word whose frequency to be found
String search = sc.next();
for(int i=0;i<len;i++) {
char ch=sent.charAt(i);
if (ch==' ')//checking the conditions
{
word =

sent.substring(sp,i);
sp=i+1;
if(word.equalsIgnoreCase(search)==true)//checking the condition
c++;
}
} System.out.println("The number of substring "+search+" in the string is
"+c+"."); //print the number of substring
}
}
Output 23:

55 | P a g e
Program 24: :Write a program in java to accept a string in a mixed case and display
the same in reversed order.
ALGORITHM:
Step1: defining class name by “s8”.
Step2:accepting the string from user and find the length of the string.
Step 3:the string is broken into words,then check is blank is there,then in str3 the
string is extract.
Step4:the print the string in reversed order;
Step5:End.
Solution:
import java.util.*;
public class s8//defining the class name
{
public static void main(String args[])
{
int i,j,l;char ch;String str1,str2=" " ,str3=" ";

56 | P a g e
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");//accepting the string from user
str1=sc.nextLine();
str1=str1+" ";//allocating blank
l=str1.length();
for(i=0;i<l;i++)//execute loop
{
ch=str1.charAt
(i);
if(ch==' ')//checking
condition
{
str3=str2+"
"+str3;
str2=" ";
}
else
{
str2=str2+ch;
}
}
System.out.println("the reversed string:");//print the reversed string
System.out.println(str3);}
}
}
Output 24:

57 | P a g e
Progrm 25: Write a program to accept a sentence and display the new string after
removing all the vowels in the string.
Sample Input: COMPUTER APPLICATIONS
Sample Output: CMPTR PPLCTNS
ALGORITHM:
Step 1:Initialize the required variables.
char c;String str1="";String ch="";
Step 2:Find vowels in the string.If found remove the vowels and add the string.If
vowel not found go to Step 3.
for(int i=0;i<l;i++)
{
c=str.charAt(i);
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
str1=str1+ch;

58 | P a g e
Step 3:If vowel not found just add character to the string.
else
str1=str1+c;
}
Step 4:Display the string.
Step 5:End.
SOLUTION:
import java.util.*;
class s9//defining the class name
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");//accept the string from user
String str=sc.nextLine();
int l=str.length();//find the length of the string
char c;String str1="";String ch="";
for(int i=0;i<l;i++)//execute the loop
{
c=str.charAt(i);
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')//checking whether vowels occur in the
string
str1=str1+ch;

else
str1=str1+c;
}
System.out.println(str1);//print the new string
}
}
Output 25:

59 | P a g e
Program 26: Write a
program to accept a
string and change the case of each letter of the string.Display the new string.
Sample Input: rEAL iS nOT pERFECT
Sample Output: REAL Is Not Perfect
ALGORITHM:
Step 1:Initialize the required variables.
char c1;String str2="";
Step 2:Run a for loop from 0 to l.
for(int i=0;i<l;i++)
Step 3:If the character is lower case,convert into upper case and add.If condition
false
move to step 4.
if( c>='a' && c<='z'){
c1=Character.toUpperCase(c);
60 | P a g e
str2=str2+c1;}
Step 4:If the character is Upper case convert it into Lower case and add.If not move
to
To Step 5.
else if( c>='A' && c<='Z'){
c1=Character.toLowerCase(c);
str2=str2+c1;}
Step 5:If the character is a space or any special character simply add it.
else {
str2=str2+c; }
Step 6:Display the added string.
Step 7:End.
SOLUTION:
import java.util.*;
class s10//defining class name
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string:");//accepting string fron user
String str=sc.nextLine();
int l=str.length();//finding the length of the string
char c1;String str2="";
for(int i=0;i<l;i++)//loop execute
{
char c=str.charAt(i);
if( c>='a' && c<='z')//checking if(c>='a' && c<='z')
{
c1=Character.toUpperCase(c);//converting to upper case
str2=str2+c1;
}
else if( c>='A' && c<='Z')//check else if( c>='A' && c<='Z')

61 | P a g e
{
c1=Character.toLowerCase(c);//convert to lower case
str2=str2+c1;
}
else
str2=str2+c;
}
System.out.println("New String"); Output 26:
System.out.println(str2);
}
}

Program 27:Write a program to accept a string and modify the string by adding
‘hot’ at the beginning of the word.If the string already contains ‘hot’,then the string
remains unchanged.Print the modified string.
Sample Input:CAKE
Sample Output:HOTCAKE
Sample Input:HOTLINE
Sample Output:No change in the
String
ALGORITHM:
Step 1:Initialize the required
variables
String s="HOT";
Step 2:Extract first three characters
of the string.
String h=str.substring(0,3);

62 | P a g e
Step 3:If the entered string contains HOT in the beginning,display the result.If the
condition is false,move to Step 4.
if(h.equals(s)==true) {
System.out.println("No change in String"); }
Step 4:If the string does not contain HOT in the beginning,add it and display
else {
System.out.println("Modified String: ");
System.out.println("HOT"+str); }
Step 5:End.
SOLUTION :
import java.util.*;
class s11//Defining the class name
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string:");//accepting the string from user
String str=sc.nextLine();
int l=str.length();
String s="HOT";
String h=str.substring(0,3);
if(h.equals(s)==true)
{//condition
System.out.println("No change in String");
}
else
{
System.out.println("Modified String: ");
System.out.println("HOT"+str);
}
}
}

63 | P a g e
OUTPUT 27:

Program 28: Write a


program to accept a
string and replace the
first letter of each words
with ‘X’.
ALGORITHM:
Step 1:Define the class name by ”s12”.
Step 2:Accept the string from user. And find the length of the string.
Step3: Extract the words and then replace the first letter of each words with ‘X’.
Step4 :print the new String .
Step 5: End.
SOLUTION:
import java.util.*;

64 | P a g e
public class s12//defining class name
{
public static void main(String args[])
{
int i,l; char ch;String st,st1=" ";//Initialising variables
Scanner sc=new Scanner(System.in);
S
ystem.out.println("En t
er a string");//accept
the string from user
st=sc.nextLine();
st=" "+st;//allocate
space at starting of string
l=st.length();
for(i=0;i<l;i++)//loop execute
{
ch=st.charAt(i);
if(ch==' ')
{
ch=st.charAt(i+1);
ch='X';//assigning 'X' to ch
st1=st1+""+ch;
i=i+1;
}
else
st1=st1+ch;
}
System.out.println("The new string="+" "+st1);//print the new string
}
}
OUTPUT 28:

65 | P a g e
Program 29:Write a program to accept a string and find the frequency of each
character present in the sentence.
ALGORITHM:
Step 1: Start by defining class name “s14”.
Step 2: Input is taken from the user as (String str).
Step 3: By using String functions, the length of the string of measured, and converted
into uppercase string.
Step 4: Two loops are used so that 1 st one can run for each character and the second
for the character of the string.
66 | P a g e
Step 5: If the characters of the loops matches, a variable counts them, and then
displayed on the output screen.
Step 6: End.
SOLUTIONS :
import java.util.*;
public class s14//defining the class name
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string in capital words");//accept the string from user
String str=sc.nextLine();//enter the string
int l=str.length();//find the length of the string
str=str.toUpperCase();
int j,i,k;
System.out.println(" Character -- Frequency");
for(i=65;i<=90;i++)//loop execute
{
char c=(char)i;
k=0;
for(j=0;j<l;j++)//loop another execute
{
char ch=str.charAt(j);
if(c==ch)//check condition
k++;//increment
}
if(k>0)
System.out.println(" "+c+" -- "+k);//print the frequency
k=0;
}
}
}

67 | P a g e
OUTPUT 29:

Program 30:Write a program to convert non-palindrome word into the palindrome


word by concatenating the word by its reverse (excluding the last alphabet ).
Sample Input: HELP
Sample Output: HELPLEH
ALGORITHM:
Step 1:Define a class as ”nonpalin”.
Step 2:Accept the word from user.
Step 3:check whether the number is palindrome or not. If the word is non
palindrome ,them convert into palindrome.

68 | P a g e
Step 4:Now, reverse the word(by omitting the last alphabet ) and by concatenating
both, the new palindrome word is obtain. Thus the word HELP becomes HELPLEP.
Solution:
import java.util.*;
public class nonpalin
{//convert non palindrome word to palindrome word
public static void main(String args[])
{//variable intialisation
String st,st1="",p="",st3="";int i=0,l=0;
Scanner sc=new Scanner (System.in);//create scanner class object
System.out.println("Enter the string");//accepting the sting from user
st=sc.next();
st=st.toUpperCase();//converting to upper case
st=st+" ";
l=st.length();//finding the length of the string
for(i=0;i<l;i++)
{
char ch=st.charAt(i);//accepting the character of the string
if(ch!=' ')
{//extraction ofb= the word from the sentence
st1=st1+ch;
p=ch+p;
st3=st1+p.substring(1);
}
}
System.out.println("original string"+ st1);//original string
System.out.println("non palindrome to palindrome word"+st3);
}//displaying the word from non palindrome word to palindrome word
}
Output 30:

69 | P a g e
Program 31:Write a program to accept a sentence which may be terminated by
either ‘.’,’?’or’!’ only. The words may be separated by more than one blank space
and are in UPPER CASE. Perfrom the following tasks:
 Interchange the first and last letter of the string and concat by without
changing remain part of the string.
Sample Input: happy new year?
Sample output : happy yapph
new wen
year reay
70 | P a g e
ALGORITHM:
Step 1:Define a class as “happynewyear”
Step 2:Accept the sentence from user which may be terminated by either ‘.’,’?’or’!’
only. The words may be separated by more than one blank space and are in UPPER
CASE.
Step 3: Interchange the first and last letter of the string and concat by without
changing remain part of the string.
Step 4: now display the string according to sample output.
Solution:
import java .util.*;
public class happynewyear
{
public static void main(String args[])
{//initailise the variables
String st,wd="";
Scanner sc=new Scanner(System.in);//create scanner class object
System.out.println("Enter a sentence");//enter the sentence
st=sc.nextLine();
int l=st.length();//find the length of the string
char sh=st.charAt(l-1);//last character of the string
if(sh==' '||sh=='.'||sh=='?'||sh=='!')//condition for terminating
{
for(int i=0;i<l;i++)
{
char ch=st.charAt(i);
if(ch==' '||ch=='.'|ch=='?'||ch=='!')
{//extraction of the word from the string
int k=wd.length();
System.out.println(wd +" "+wd.substring(k-1)+wd.substring(1,k-
1)+wd.substring(0,1));
wd="";
}
else

71 | P a g e
{
wd+=ch;//assigning the word
}
}
}
else
{
System.out.println("Invalid");
}
}
}
Output 31:

Program 32:Write a program to accept a string


and pass it to method void replace
(String wd) . The method further replace
each letter by next letter in opposite case the
conversation follows round robbin
system robin round such that z follows a and
A follows z. For example :Abz becomes
bcA
ALGORITHM:
Step 1:Define a class as “round_robbin”.
Step2:Accept the string from user.

72 | P a g e
Step 3: Checking whether the character is in lower case or may be lie between 'a' to
'y' if(ch>='a'&&ch<='y'){
p=(int )(ch-31);
ch=(char)p; }
Step 4: Checking whether the character is in lower case and equal to 'z'
else if(ch=='z'){
p=(int )(ch-57);
ch=(char)p;}
Step 5: Checking whether the character is in upper case or may lie between ‘A’ to ‘Y’.
else if(ch>='A'&&ch<='Y' ){
p=(int )(ch+33);
ch=(char)p;}
Step 6:Checking whether thecharacter is in upper class and equal to 'Z’
else if(ch>='Z') {
p=(int )(ch+7);
ch=(char)p;}
Solution:
import java.util.*;
public class round_robbin
{
public static void main(String args[])
{
String st;// variable to store string
int l,i,p;//variable to store the length and value of loop
Scanner sc=new Scanner(System.in);//creating the scanner class object
System.out.println("Enter the string ");//accept the string from the user
st=sc.nextLine();
l=st.length();
for(i=0;i<l;i++)
{
char ch=st.charAt(i);

73 | P a g e
if(ch>='a'&&ch<='y')//checking whether the character is in lower case or may be
lie between 'a' to 'y'
{
p=(int )(ch-31);
ch=(char)p;
}
else if(ch=='z')//checking whether the character is in lower case and equal to 'z'
{
p=(int )(ch-57);
ch=(char)p;
}
else if(ch>='A'&&ch<='Y')//checking whether the character is in upper case or
may lie between ‘A’ to ‘Y’.
{
p=(int )(ch+33);
ch=(char)p;
}
else if(ch>='Z')//checking whether the character is in upper case or equal to ‘Z’
{
p=(int )(ch+7);
ch=(char)p;
} Output 32:
System.out.print(ch);
}
}
}

Program 33: Write a program to accept a word and convert it into UPPER CASE.Find
the frequency of vowels and
consonants in the word and
rearrange the word by bring the vowels
at the beginning followed by
consonants.Display the original word
alongwith rearrange word.

74 | P a g e
Sample Input: paradise
Smaple output :PARADISE→AAIEPRDS,number of vowels and conssonants =4 &4
respectively.
ALGORITHM:
Step 1:Define a class as “rearrange”.
Step 2:Accept a string from user and convert it into Upper Case.
Step 3: putting condition according to find the frequency of vowels and consonants
in the word .
Step 4:Rearrange the word by bring the vowels at the beginning followed by
consonants.
Step 5:Display the original word alongwith rearrange word.
Solution:
import java.util.*;
public class rearrange
{//creating the class to rearrange the word
public static void main(String args[])
{//variable intialisation
String wrd,st="",st2="",nwrd="";int l=0,c=0,v=0;
Scanner sc=new Scanner(System.in);//create scanner class object
System.out.println("Enter a word");//accepting the word from user
wrd=sc.next();
wrd=wrd.toUpperCase();//converting it into upper case
l=wrd.length();//word length
for(int i=0;i<l;i++)
{
char ch=wrd.charAt(i);
if(ch=='A'||ch=='E'||ch=='O'||ch=='I'||ch=='U')
{//condition for vowels
v++;//frequency of vowels
st=st+ch;}//bringing vowels at the beginning
else
{ c++;//frequency of consonants
st2=st2+ch;//bring the vowels at the beginning followed by consonants.
75 | P a g e
}
nwrd=st+st2;//new word
}
System.out.println("ORIGINAL WORD"+ wrd);//displaying original word
System.out.println("new word"+nwrd);//displaying new word
System.out.println("no of vowels "+ v);//frequency of vowels
System.out.println("no of consonants"+c);//frequency of consonants
}
}
Output 33:

Program 34:Write a program to accept a word and convert it into upper case .
Change word into piglatin form and display the word .And also find the number of
words consonants present in the word.

76 | P a g e
Piglatin word is formed by taking a substring of the original word from that
position till the end and then the letters left before the position are added. After it,
as usual the string ”AY” is attached to the new string word formed.
Sample input: TROUBLE→ Sample output: OUBLETRAY
Algorithm:
Step 1:Define a class as ”piglatin”.
Step 2:Accept the word from the user & converting it into upper case.
Step 3: Extracting the character from the string.
Step 4: if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'){//condition for checking
vowels
break; }
else {
c++;//counting consonants}
Solution:
import java.util.*;// using scanner class package
public class piglatin
{// create a class
public static void main(String args[])
{//initialsing the variables
String w,st="",st1="",st2=""; int i,l=0,c=0;
Scanner sc=new Scanner (System.in);//creating the scanner class object
System.out.println("Enter a sentence");//accepting the sentence from user
w=sc.next();//enter the senetence
w=w.toUpperCase();//converting it into upper case
l=w.length();//to find the the length of the string
for(i=0;i<l;i++)//staring of the loop
{
char ch=w.charAt(i);// extracting the character from the string
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{//condition for checking vowels
break;
}
else
77 | P a g e
{
c++;//counting cononants
}
}
st=w.substring(i);//accepting first letter of the word
st1=w.substring(0,i);//accepting the mid part of the word
st2=st+st1+"AY";//forming piglatin word
System.out.println("Word in piglatin form"+' '+st2);//dislay piglatin word
System.out.println("no of consonants"+c);//display number of the consonants
}
}
Output 34:

NUMBER PROGRAMS
78 | P a g e
PROGRAM 35: A Goldbach number is a positive even integer that can be expressed
as the sum of two odd primes.All even integer numbers greater than 4 are
Goldbachnumbers.Example:6=3+3
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e.
3 and 7, 5 and 5.Write a program to accept an even integer 'N' where N > 9 and N <
50. Find all the odd prime pairs whose sum is equal to the number 'N'.
Sample Input: N = 14
Sample output: PRIME PAIRS ARE: 3, 11 7, 7
ALGORITHM:
Step1:Read or initialize a number N.
Step 2:Store it in another variable.
Step 3:Check if the given number N is even or not.
Step 4:If the number is odd, print "Invalid input".
Step 5:If the number is even, follow the procedure given below:
 Define two arrays one for storing the prime numbers and the other for
calculating the prime number.
 Find all prime numbers till the entered number using a for loop and store it in
an array.
 In the second array, store only odd prime numbers using if statement.Display
the odd prime pairs.
By using the for loop, store the sum of two odd prime numbers in a variable
(sum).Using the if statement, compare the sum with the original number N. If the
sum is equal to the original number, the given number N is Goldbach Number, else
not.
SOLUTION:
import java.util.*;
public class goldbach_no
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int i, j, n, k, b=0, c=0, sum=0;
System.out.println("Enter the Number: ");//accept an integer from the user
int num=sc.nextInt(); //assigning the variable to another variable k

79 | P a g e
k=num;//defining two arrays with capacity equal to the number
int arr1[]=new int[num];
int arr2[]=new int[num];//checks if the number is even or not
if(num%2!=0) //prints if the number is not even
{
System.out.println("Invalid Input, Number is Odd.");
}//executes if the number is even
else
{ //loop used for finding prime numbers
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{ //condition if number is divisible
if(i%j==0)
{ //increments the value of c, initially it is 0
c++;
}
} //condition for odd Prime numbers
if((c==2)&&(i%2!=0))
{ //stores odd prime numbers
arr1[b]=i; //stores odd prime numbers
arr2[b]=i; //increments the value of b by 1
b++; }
c=0; }
System.out.println("Odd and Prime Pairs are: ");
for(i=0;i<b;i++)
{
for(j=i;j<b;j++)
{ //calculates the sum of two odd prime numbers
sum=arr1[i]+arr2[j];
//checks the sum is equal to the given number or not
if(sum==k)
80 | P a g e
{ //prints pair of odd prime numbers
System.out.print(arr1[i]+" , "+arr2[j]);
System.out.println();
} }
System.out.println(k+" is a Goldbach number.");
}
}
}
OUTPUT 35:

PROGRAM 36: Write a program to determine whether the number is happy or not.
Sample Input: 82
81 | P a g e
Sample Output: 82 is happy number
ALGORITHM:
Step 1: To Enter a non-zero, positive number from the keyboard and assign it to the
variable is called number.
Step 2: To calculate the remainder by dividing (%) the given number with 10.
Step 3: Calculate the square of the remaining value and add it to a variable sum.
Step 4: To divide (/) the number by 10.
Step 5: Repeat step: 2 to step: 4 until we get the sum of the square of all digits of the
given number.
Step 6: Then, the final addition value stored in the variable sum.
Step 7: To define a variable called result and initialize it with the value of a number.
Step 8: If the result value is not equal to 1 or 4, we will simply call the created
method to repeat it.
Step 9: If the result value set to 1 then, it prints” it is a Happy Number “; else, it prints
“It is not a Happy Number”.
SOLUTION:
import java.util.Scanner;
public class HappyNumber
{//find whether the number is happy number or not
public static void main(String[] args)
{//cretae scanner object
int n, r = 1, num, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();//accepting number from user
num = n;
while (num > 9)//check condition
{
while (num > 0)
{
r = num % 10;//store num%10
sum = sum + (r * r);//find the sum
num = num / 10;

82 | P a g e
}
num = sum;sum = 0;
}
if (num == 1)//if num is equal to 1 then it is happy number
{
System.out.println("Happy Number");
}
else
{
System.out.println("Not Happy Number");
}
}
}
OUTPUT 36:

PROGRAM 37: Write a program to input two numbers and check whether they are
twin prime numbers or not.Hint: Twin prime numbers are the prime numbers
whose difference is 2.For example: (5,7), (11,13), ……. and so on.
ALGORITHM:

83 | P a g e
Step1 :Get pair of numbers from the user to check whether it is twin prime or not.
Step2:Check whether both the numbers are prime or not.Find the difference between
both numbers.
Step3:If both the numbers are prime and the difference of both the number is 2, print
"numbers are the twin prime numbers".If not, then print "numbers are not the twin
prime numbers."
SOLUTION:

import java.util.Scanner;

public class TwinPrime


{//Twin prime numbers are the prime numbers whose difference is 2.
public void twinPrimeCheck()
{//create scanner object
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();//enter value of a
System.out.print("Enter second number: ");
int b = in.nextInt();//enter value of b
boolean isAPrime = true;
for (int i = 2; i <= a / 2; i++)
{// check (a%i==0)
if (a % i == 0) {
isAPrime = false;
break;
}
}//find difference between two number
if (isAPrime && Math.abs(a - b) == 2)
{//difference between two numbers is 2
boolean isBPrime = true;
for (int i = 2; i <= b / 2; i++)
{
if (b % i == 0)
isBPrime = false;

84 | P a g e
break;
}
if (isBPrime)
System.out.println(a + " and " + b + " are twin prime");
else
System.out.println(a + " and " + b + " are not twin prime");
}
else
System.out.println(a + " and " + b + " are not twin prime");
}
}

OUTPUT 37:

PROGRAM 38: Write a program to check a number is Kaprekar number or not. A


Kaprekar number for a given base is a non-negative integer, the representation of
whose square in that base can be split into two parts that add up to the original
number again. For instance, 45 is a Kaprekar number, because 45 2 = 2025 and
20+25 = 45.
ALGORITHM:

85 | P a g e
Step1: Take a number.
Step2:Finditssquarevalue.
Step3: Count the number of digits in the square.
Step4:Take a iterator variable (i) starting from number_of_digits_in_square – 1
Step 5:Find the first part of the number, as dividing the square value by 10^i
Step 6: Find the second part as the calculating remainder value after dividing 10^i
Step 7:Check any part having only 0, if yes then skip the iteration.
Step8: Calculate the sum of both parts.
Step 9:Compare sum and number, if both are equal then the given number is
kaprekar number.
SOLUTION:
import java.util.*;
class Kaprekar
{//A Kaprekar number is a non-negative integer, whose
public static void main()
{//create scanner object
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
int num = sc.nextInt();//accept number from user
int p = 0;
int digit=0,sq,sum=0;
sq = num*num;
p=sq;
while(sq>0)
{//square in that base can be split into two parts that add up to the original
number
digit++;
sq=sq/10;
}
int k=(int)Math.pow(10,(digit/2));
while(p>0)
{
sum=sum+(p%k);
p=p/k;

86 | P a g e
}
if(sum==num){//to check whether number is kaprekar
System.out.println("Is a kaprekar number");
}
else{
System.out.println("Is not a kaprekar number");
}
}
}
OUTPUT 38:

PROGRAM 39: A Smith number is a composite number, whose sum of the digits is
equal to the sum of its prime factors. For example:4, 22, 27, 58, 85, 94, 121 ……….
are Smith numbers.Write a program in Java to enter a number and check whether it
is a Smith number or not.
SampleInput:666
Sumofthedigits:6+6+6=18
Primefactorsare:2,3,3,37

87 | P a g e
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18
Thus, 666 is a Smith Number.
ALGORITHM:
Step 1:Read or initialize a number from the user.Find the sum of its digits.
Step 2:Find the prime factors of the given number.
Step 3:Determine the sum of digits of its prime factors.
Step 4:Compare the sum of digits with the sum of digits of its prime factors.If they
are equal, the given number is a smith.Else, not a smith number.
SOLUTION:
import java.util.Scanner;
public class SmithNumber
{//A Smith number is a composite number
public static void main(String args[])
{// create scanner object
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();//accept number from user
if (n <= 0) {
System.out.println(n + " is not a Smith Number.");
return;
}
boolean isComposite = false;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
isComposite = true;
break;
}
}
//sum of the digits is equal to the sum of its prime factors
if (isComposite && n != 1) {
int sumDigits = 0;
int t = n;

88 | P a g e
while (t != 0) {
int d = t % 10;
sumDigits += d;
t /= 10;
}
int sumPrimeDigits = 0;
t = n;
for(int i = 2; i < t; i++) {
while(t % i == 0) {
t /= i;
int temp = i;
while (temp != 0) {
int d = temp % 10;
sumPrimeDigits += d;
temp /= 10;
}} }
if(t > 2) {
while (t != 0) {
int d = t % 10;
sumPrimeDigits += d;
t /= 10;
} }//check whether the number is smith
if (sumPrimeDigits == sumDigits)
System.out.println(n + " is a Smith Number.");
else
System.out.println(n + " is not a Smith Number.");
}
else {
System.out.println(n + " is not a Smith Number.");
}
}
}
89 | P a g e
OUTPUT 39:

PROGRAM 40: A Composite Magic number is a positive integer which is composite


as well as a magic number.Composite number is a number which has more than
twofactors.Forexample:Factors of 10 are: 1, 2, 5, 10.
A Magic number is a number in which the eventual sum of the digit 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. Display the number of composite magic integers that are in the
range between m and n (both inclusive) and output them along with frequency, in
the format specified below:

90 | P a g e
SampleInput:m=10,n=100
Output: The composite magic numbers are 10,28,46,55,64,82,91,100
Frequency of composite magic numbers: 8
ALGORITHM:
Step1: define class name .
Step 2:enter the value of n and m from user.
Step3: check condition: if (m < 1 || n < 1 || m > n) //check the condition {
System.out.println("Invalid input");
return; }
Step4: if (i % j == 0) {
isComposite = true;
break; }
Step5: applying conditions while (num > 9) {
int sum = 0;
while (num != 0) {
int d = num % 10;
num /= 10;
sum += d;
Step6:Print the composite magic number.
SOLUTION:
import java.util.Scanner;
public class CompositeMagicNum
{
public static void main(String args[])
{//To find whether the number is composite magic
Scanner in = new Scanner(System.in);
System.out.print("Enter m and n: ");//enter the value of m and n from user
int m = in.nextInt();
int n = in.nextInt();
if (m < 1 || n < 1 || m > n) //check the condition
{
System.out.println("Invalid input");

91 | P a g e
return;
}
System.out.println("The composite magic numbers are:");
int count = 0;
for (int i = m; i <= n; i++)
{
boolean isComposite = false;
for (int j = 2; j < i; j++)
{
if (i % j == 0) //check the condtion for composite magic
{
isComposite = true;
break;
}}
if (isComposite && i != 1)
{
int num = i;
while (num > 9) {
int sum = 0;
while (num != 0) {
int d = num % 10;
num /= 10;
sum += d;
}
num = sum;
}
if (num == 1)
{
count++;
System.out.print(i + " ");
}
}}
92 | P a g e
System.out.println();
System.out.println("Frequency of composite magic numbers: " +
count);//frequency of composite number
}
}
OUTPUT 40:

PROGRAM 41: An Evil number is a positive whole number which has even number
of 1's in its binary equivalent. Example: Binary equivalent of 9 is 1001, which
contains even number of 1's. A few evil numbers are 3, 5, 6, 9…. Design a program
to accept a positive whole number and find the binary equivalent of the number
and count the number of 1's in it and display whether it is a Evil number or not with
an appropriate message. the result Sample Input:3
Sample Output:No of 1’s : 2. Number is evil.
ALGORITHM:
Step1:Define class name and create scanner object.
93 | P a g e
Step2:Take a number and find its Binary equivalent, and store it in a variable “binary”
Step3:Count number of 1’s in the binary
Step 4:If the number of 1’s is even then it is an evil number else the given number is
not an evil number.
SOLUTION:

import java.util.Scanner;

public class EvilNumber


{//find whether the number is evil or not
public static void main(String args[])
{
Scanner in = new Scanner(System.in); //create scanner class object
System.out.print("Enter a positive number: ");
int n = in.nextInt();// enter the number from user
if (n < 0)//check conditions
{
System.out.println("Invalid Input");
return;
}
int count = 0;int p = 0;int binNum = 0;
while (n > 0)
{
int d = n % 2;
if (d == 1)
count++;// increment
binNum += (int)(d * Math.pow(10, p));//find the equivalence binary number
p++;n /= 2; // find the equivalence binary number
}
System.out.println("Binary Equivalent: " + binNum);
System.out.println("No. of 1's: " + count);
if (count % 2 == 0) // check whether the value of count is even or odd
System.out.println("Output: Evil Number");
else
94 | P a g e
System.out.println("Output: Not an Evil Number");
}
}
Output41:

Program 42: A unique-digit integer is a positive integer (without leading zeros) with
no duplicates digits. For example 7, 135, 214 are all unique-digit integers whereas
33, 3121, 300 are not. Given two positive integers m and n, where m < n, write a
program to determine how many unique-digit integers are there in the range
between m and n (both inclusive) and output them. The input contains two positive
integers m and n. Assume m < 30000 and n < 30000. You are to output the number
of unique-digit integers in the specified range along with their values in the format
specified below:

95 | P a g e
SampleInput:m=100andn=120
SampleOutput:TheUnique-Digitintegersare:102,103,104,105,106,107,108,109,120.
Frequency of unique-digit integers is : 9
ALGORITHM:
Step 1:Read a number from the user.
Step 2:Find the last digit o the number.
Step 3:Compare all digits of the number with the last digit.
Step 4:If the digit found more than one time, the number is not unique.Else,
eliminate the last digit of the number.Repeat steps 2 to 5 until the number becomes
zero.
SOLUTION:
import java.util.Scanner;
public class UniqueIntegers
{//A unique-digit integer is a positive integer
public static void main(String args[])
{//create scanner object
Scanner in = new Scanner(System.in);
System.out.print("Enter m: ");//enter value of variable m
int m = in.nextInt();
if (m < 1 || m > 30000)
{
System.out.println("Value of m should be between 1 and 30000");
return;
}
System.out.print("Enter n: ");//enter the value of variable n
int n = in.nextInt();
if (n < 1 || n > 30000) {
System.out.println("Value of n should be between 1 and 30000");
return;
}
if (m > n) {
System.out.println("Value of m should be less than n");
Return; }
System.out.println("The Unique-Digit integers are:");
int count = 0;//initialising count
for (int i = m; i <= n; i++) {
int num = i;

96 | P a g e
boolean visited[] = new boolean[10];
boolean isUnique = true;
while (num != 0) {
int d = num % 10;
if (visited[d]) {
isUnique = false;
break;
}
visited[d] = true;
num /= 10;
}//check whether conditin is true
if (isUnique) {
count++;
System.out.print(i + " ");
}
}
System.out.println();
System.out.println("Frequency of unique-digit integers is: " + count);
}
} OUTPUT 42:

PROGRAM 43:
A tech number
has even
number of
digits. If the
number is split
in two equal
halves, then the square of sum of these halves is equal to the number itself. Write a
program to generate and print all four digits tech numbers.

97 | P a g e
Example:Consider the number 3025
Square of sum of the halves of 3025 = (30 + 25)2= (55)2= 3025 is a tech number.
ALGORITHM:
Step1:Take the number from the user and store it into a variable using Scanner class.
Step 2;Check the number of digits in the input number, if the number of digits is not
even, the input number is not a Tech number else continue with the next step.
Step 3:Split the number in two halves, num1 and num2.
Step 4:Find the sum of the squares of two halves (num1+num2)^2, if it is equal to the
number itself, return “Tech Number” else return “Not a Tech Number
SOLUTION:
import java.util.Scanner;
public class TechNumber
{
public static void main(String args[])
{
int n, num, firstHalf, secondHalf, digits = 0, squareOfSum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check: "); //reads an integer from the user
n = sc.nextInt(); //assign the value of n into num
num = n;//the loop executes until the condition returns false
while (num > 0)
{ //incerements the variable digits by 1
digits++; //removes the last digit of the given number
num = num / 10;
} //check if the given number has an even number of digits or not
if (digits % 2 == 0)
{
num = n;//determines the first half of the given number
firstHalf = num % (int) Math.pow(10, digits / 2);//determines the second half of
number
secondHalf = num / (int) Math.pow(10, digits / 2);//calculate the square of both
the numbers
squareOfSum = (firstHalf + secondHalf) * (firstHalf + secondHalf);

98 | P a g e
//compares the square of the sum with the given number
if (n == squareOfSum)
{
System.out.println(n+" is a tech number.");
}
else
{
System.out.println(n+" is not a tech number.");
}}
else
{
System.out.println(n+ " is not a tech number.");
}
}
} OUTPUT 43:

PROGRAM 44:Write a program to find whether the number is adam or not A


number is called an Adam number if the square of a number and the square of its
reverse are reverse to each other
Sample Input: enter the number : 12

99 | P a g e
Sample Output: 12 is adam number
ALGORITHM:
Step1 :Read or initialize a number N.
Step2 :Find the square of the number.and Find the reverse of the square.
Step 3:Reverse the number N.
Step 4:Find the square of the reversed number.If the reverse of the square of the
number is equal to the square of the reverse of the number, the number is an Adam
number, else not.
SOLUTION:
import java.util.*;
class adam
{//find whether the number is adam or not
public static void main(String args[])
{//create scanner object
Scanner sc=new Scanner(System.in);
int s1=0,s=0,r,z,z1,t;//initialise variables
System.out.println("ENTER A VALUE: ");
t=z1=z=sc.nextInt();//accepting the number from user
z*=z;//square
while(z>0)
{
r=z%10;
s=(s*10)+r;
z=z/10;
}
while(z1>0)//to find the number is adam
{
r=z1%10;
s1=(s1*10)+r;
z1=z1/10;
}
s1*=s1;

100 | P a g e
if(s==s1)//print ehther the number is adam or not
System.out.println(t+" is a adam number");
else
System.out.println(t+" is not a adam number");
}
}
OUTPUT 44:

PROGRAM 45: A Lucky number is a number whose digits when added continuously
till a single digit is obtained is equal to 1.Write a program in java to input a number
and check whether it is lucky or not.
ALGORITHM:

101 | P a g e
Step1 :Define class name.
Step 2: counter := 2 (It is static data, not be initialized again in recursion call)
Step 3:if counter > n, then
Step 4: return 1
if n mod counter = 0, then
Step 5” return 0
n := n - (n / counter)
counter := counter + 1
isLuckyNumber(n)
SOLUTION:
import java.util.*;
public class LuckyNumber
{//lucky number is no. whose digits when added continously till to get 1
public static void main(String args[])
{//create scanner class object
Scanner sc=new Scanner(System.in);
int a,b,c,s;//initialise variables required
System.out.print("Enter the number : ");//Accepting a number from the user
int n=sc.nextInt(); //creates an array of n
s=n;//assignn to s
while(s>9)
{
n=s;s=0;
while(n>0)
{//to find whether the number is lucky
a=n/10;
b=a*10;
c=n-b;
s=s+c;
n=a;
}
}
102 | P a g e
if(s==1)//if sum is equal to 1
System.out.println("No. is Lucky Number ");
else
System.out.println("No. is not lucky number");
}
}
OUTPUT 45:

PROGRAM 46: Write a program to input a number and find whether the number is
a Disarium number or not. A number is said to be Disarium if sum of its digits
powered with their respective positions is equal to the number itself.
SampleInput:135
SampleOutput:135⇒11 +32 +53 =135
So, 135 is a Disarium number.
ALGORITHM:
103 | P a g e
Step 1:calculateLength() counts the digits present in a number.Use a while loop to
check whether the number is not equal to 0.Divide the number by 10 and increment
the variable length by 1 and Return length.
Step2:Define and initialize variable num.
Step3:Make a copy of num by storing the value of num in n.
Step 4:Using while loop calculates remainder rem repeatedly by dividing num with
10.
Step 5:Calculate the value of rem raised to power its position, i.e. remlen and store
the computed value in a variable sum.Check whether sum is equal to number. If yes,
then given number is Disarium number. Else, it is not a Disarium number.
SOLUTION:
import java.util.Scanner;
public class DisariumNumber
{//find number is diasrium or not
public static void main(String args[])
{//create Scanner class
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = in.nextInt();//accept number from user
int orgNum = num;
int digitCount = 0;
while (num != 0)
{
num /= 10;
digitCount++;
}
num = orgNum;
int sum = 0;
while (num != 0) {//check condition for diasrium number
int d = num % 10;
sum += Math.pow(d, digitCount);
digitCount--;
num /= 10;

104 | P a g e
}//check whether the number is diasrium or not
if (sum == orgNum)
System.out.println("Disarium Number");
else
System.out.println("Not a Disarium Number");
}
}
OUTPUT 46:

PROGRAM 47:The International Standard Book Number (ISBN) is a unique numeric


book identifier which is printed on every book. The ISBN is based upon a 10-digit
code.TheISBNislegalif:1 × digit1 + 2 × digit2 + 3 × digit3 + 4 × digit4 + 5 × digit5 + 6 ×
digit6 + 7 × digit7 + 8 × digit8 + 9 × digit9 + 10 × digit10 is divisible by 11.
Example:ForanISBN1401601499Sum = 1 × 1 + 2 × 4 + 3 × 0 + 4 × 1 + 5 × 6 + 6 × 0 + 7 ×
1 + 8 × 4 + 9 × 9 + 10 × 9 = 253 which is divisible by 11.
ALGORITHM:

105 | P a g e
Step 1:Input the ISBN code as a 10-digit integer.
Step 2:If the ISBN is not a 10-digit integer, output the message "Illegal ISBN" and
terminate the program.
Step 3:If the number is divisible by 11, output the message "Legal ISBN". If the sum is
not divisible by 11, output the message "Illegal ISBN".
SOLUTION:
import java.util.Scanner;
public class ISBN
{
public static void main(String args[])
{//create scanner class
Scanner in = new Scanner(System.in);
System.out.print("Enter the ISBN: ");//accept isbn from user
long isbn = in.nextLong();
int sum = 0, count = 0, m = 10;
while (isbn != 0) {//check conditions
int d = (int)(isbn % 10);
count++;//increment
sum += d * m;
m--;//decrement
isbn /= 10;
}//check whether it is ISBN
if (count != 10) {
System.out.println("Illegal ISBN");
}
else if (sum % 11 == 0) {
System.out.println("Legal ISBN");
}//print appropiate
else {
System.out.println("Illegal ISBN");
}
}

106 | P a g e
}
OUTPUT 47:

PROGRAM 48: Write a program to input a number and find whether the number is
an emirp number or not. A number is said to be emirp if the original number and
the reversed number both are prime numbers.
For example, 17 is an emirp number as 17 and its reverse 71 are both prime
numbers.
ALGORITHM:
Step 1:Read or initialize a number (n).
Step 2:First, check the given number (n) is prime or not.If not, break the execution
and exit.If prime, find the reverse (r) of the given number (n).

107 | P a g e
Step 3:Check the reverse number (r) is prime or not.
o If not, print number (n) is not emirp.
o If prime, print the given number (n) as an emirp number.
SOLUTION:
import java.util.Scanner;
public class Emirp
{
public static void main(String args[])
{//create scanner class
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");//accepting the number from user
int num = in.nextInt();
int c = 0;//initialising
for (int i = 1; i <= num; i++)
{//loop executing
if (num % i == 0){
c++;//increment
}
}
if (c == 2)
{///conditin for emirp
int t = num;
int revNum = 0;
while(t != 0)
{
int digit = t % 10;
t /= 10;
revNum = revNum * 10 + digit;
}
int c2 = 0;
for (int i = 1; i <= revNum; i++)
{
108 | P a g e
if (revNum % i == 0) {
c2++;
}
}
if (c2 == 2)//if the original number and the reversed number both are prime
numbers.
System.out.println("Emirp Number");
else
System.out.println("Not Emirp Number");
}
else
System.out.println("Not Emirp Number");
}
} OUTPUT 48:

PROGRAM 49: Write a Program in


Java to input a number and check
whether it is a Fascinating Number
or not.
Fascinating Numbers: Some
numbers of 3 digits or more exhibit a very interesting property. The property is such
that, when the number is multiplied by 2 and 3, and both these products are
concatenated with the original number, all digits from 1 to 9 are present exactly
once, regardless of the number of zeroes. example:Consider the number 192
192 x 1 = 192

109 | P a g e
192 x 2 = 384
192 x 3 = 576
Concatenating the results: 192 384 576
It could be observed that '192384576' consists of all digits from 1 to 9 exactly once.
Hence, it could be concluded that 192 is a Fascinating Number. Some examples of
fascinating Numbers are: 192, 219, 273, 327, 1902, 1920, 2019 etc.
ALGORITHM:
Step 1:First, check the given number consist of three digits or not. If no, print cannot
be a fascinating number.
Step 2:Else, multiply the given number by 2 and 3, separately.
Step 3:Convert the results (from step 2) into a string.
Step 4:Concatenate the strings (from step 3) with the given number (n).
Step 5:Iterate over the string that we get after concatenation and count the
frequency of each digit.Print "not a fascinating number" if any digit is missing or
appeared multiple times. Else, print "fascinating number".
SOLUTION:
import java.util.Scanner;
public class FascinatingNumber
{//to find whether the number is fascinating or not
public static void main(String args[])
{//create scanner class object
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();//accepting the number from user
if (num < 100)
{//displaying the number is not fascinating
System.out.println(num + " is not a Fascinating Number");
return;
}
int num2 = num * 2;
int num3 = num * 3;
boolean isFascinating = true;//boolean is true
String str = "" + num + num2 + num3;
for (char i = '1'; i <= '9'; i++)

110 | P a g e
{
int idx1 = str.indexOf(i);
int idx2 = str.lastIndexOf(i);
if (idx1 == -1 || idx1 != idx2) {
isFascinating = false;
break;
}
}//checking whether the number fascnating or not
if (isFascinating)//print fascinating number
System.out.println(num + " is a Fascinating Number");
else//invalid message display
System.out.println(num + " is not a Fascinating Number");
}
}
OUTPUT 49:

PROGRAM 50: If a number is simultaneously palindromic and prime then it is said


tobea PalindromePrime Number.
Example: Number 313, 353 etc are PalPrime number.
ALGORITHM:
Step1 :Get the number to check for PalPrime Number
Step 2:Hold the number in temporary variable

111 | P a g e
Step 3:Using loop find if it is prime or not.
Step 4:Check if the number is Palindrome or not.
Step 5:If the number is both prime and palindrome print "PalPrime Number"If not
print "Not a PalPrime Number"
SOLUTION:
import java.util.*;
class PalPrime
{//to check the number is prime palindrome or not
public static void main(String args[])
{//create scanner class object
Scanner in= new Scanner(System.in);
int n,p,rev,s=0,i,c=0;
System.out.println("Enter No.");
n= in.nextInt(); // Input number from user
p=n; // store the entered number in "p" variable
for(i=1;i<=p;i++)
{
if(p%i==0)
{
c++;
}
}
while(n>0)
{
rev=n%10; // extract last digit of the number
s=s*10+rev; // store the digit last digit
n=n/10; // extract all digit except the last
}
if(p==s&&c==2) // comparing with original number
{
System.out.println("Number is PalPrime : "+p);
}

112 | P a g e
else
{
System.out.println("Number is not PalPrime : "+p);
}
} // end of main method
} // end of class
OUTPUT 50:

113 | P a g e

You might also like