Java Number Exercises Overview
Java Number Exercises Overview
Exercise No Topic
Exercise 1 Armstrong
Exercise 2 Palindrome
Exercise 3 Pronic Number
Exercise 4 Special Number
Exercise 5 Pascals Triangle
Exercise 6 String Tokenizer
Exercise 7 Print the Initial
Exercise 8 Frequency of palindrome
Exercise 9 Matrix Multiplication
Exercise 10 Rows in Ascending order
Exercise 11 Sum of Diagonal
Exercise 12 Sum of digits using functions
Exercise 13 String Palindrome with Function
Date:
AIM:
To write a java program to check whether the number is Armstrong
number or not.
ALGORITHM:
1. Start
2. Input scanner package.
3. Declare the class.
4. Use scanner class .
5. Input a variable n.
6. Initialise sum=0 .
7. Use while loop to check number of digits.
8. Use another while loop to check whether is it an Armstrong number.
9. Inside while loop use a variable d =n%10 to separate the digits.
10. Find the sum of digits increased to their third power.
11. Use n=n/10 to return the value of n after using the function n
%10.
12. Check if sum is equal to the input number.
13. If n and sum are same display the number is Armstrong
number if it is not equal then display not an Armstrong number.
14. Stop
Program :
import [Link].*;
{
Scanner in= new Scanner ([Link]);
int n= [Link]();
int c=0;
double sum=0;
int m=n;
int a=n;
while(n>0)
c++;
n=n/10;
while(m>0)
int d = m%10;
sum= sum+[Link](d,c);
m=m/10;
if (sum ==a)
else
}
VARIABLE DESCRIPTION:
[Link] Variable Datatype Purpose
1 in int Scanner object
2. n int To store the input variable
3. c int To store number of
variable
4. sum double To store the sum of digits
5. m int Temporary variable
6. a int Temporary variable
7. d Int Temporary variable
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Palindrome
Exercise No.: 2
Date:
AIM:
To write a java program to check whether the given number is a
Palindrome number or not.
ALGORITHM:
1. Start
2. Create the class.
3. Input any number and store in the variable a.
4. Initilise a variable rev=0.
5. Use another variable be to store a.
6. Use while loop and check is greater than zero or not.
7. If it is greater than zero use (a%10) to split the digits.
8. Use (rev*10+digit) to find the reverse number.
9. Use a=a/10 to store the value after splitting the number.
10. Check is (rev=b) then print palindrome.
11. If (rev not equal to b) print not a palindrome.
12. Stop
Program:
import [Link].*;
int a=[Link]();
int rev=0;
int b=a;
while(a>0)
int digit=a%10;
rev=rev*10+digit;
a=a/10;
if (rev==b )
else
VARIABLE DESCRIPTION
[Link] Variable Data Type Purpose
1 in int Scanner Object
2. a int Store reverse value
3. b int Store value of a
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Pronic Number
Exercise No.: 3
Date:
AIM:
To check whether the number is pronic number or not.
ALGORITHM:
1. Start
2. Use Scanner class to get a variable.
3. Initialise the variable and store as n.
4. Use for loop to get the numbers from 1 to less than n.
5. Initialise a variable c=0.
6. Use if condition (i*(i+1)==n) to check whether it is pronic number .
7. If true then store c==1.
8. Use another for loop to check is c==1 or not.
9. If (c==1) display Pronic number.
10. Otherwise display not a pronic number.
11. Stop
Program:
import [Link].*;
int n=[Link]();
int c=0;
if (i*(i+1)==n)
c=1;
break;
if (c==1)
[Link]("pronic number");
else
VARIABLE DESCRIPTION
[Link] Variable Data Type Purpose
1. in Int Scanner Object
2. i int For lop variable
3. c int To store counter number
4. n int To store the input number
Output:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Special Number
Exercise No.: 4
Date:
AIM:
To check whether the given number is a special number or not.
ALGORITHM:
1. Start
2. Use Scanner class to get input.
3. Store it n a variable n.
4. Initialize a temporary variable m=n.
5. Use while loop with (a>0).
6. Find factorial of individual digits and add them together.
7. For loop use condition as (for (int i=1;i<=d;i++).
8. At the end of while loop re initialize the factorial sum as 1.
9. If the sum is equal to the input then print it is a special number.
10. If the sum is not equal to input then print it is not a special
number.
11. Stop
Program :
import [Link].*;
int a=[Link]();
int s;
int sum=0;
int c=a;
while(a>0)
int digit=a%10;
s=1;
s=s*i;
sum=sum+s;
s=1;
a=a/10;
}
if (sum==c)
else
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. a int To store input value
3. s int To store factorial of digits
4. sum int To store the sum of factorial of each
digits
5. C int To store value of a in c
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Pascals Triangle
Exercise No.: 5
Date:
AIM:
To write a java program to print the pascal triangle according to the
input by the user.
ALGORITHM:
1. Start
2. Input a number n from the user (this determines the number of rows
for the upper part of the pattern).
3. Upper Half Construction (including middle row):
Loop i from 1 to n:
Print (n - i + 1) spaces.
Print numbers incrementing from 1 to i.
Print numbers decrementing from (i - 1) to 1.
Move to the next line.
4. Lower Half Construction:
Initialize x = n - 1, a = n - 2.
Loop j from 1 to n - 1:
Print (j + 1) spaces.
Print numbers incrementing from 1 to x.
Print numbers decrementing from a to 1.
Move to the next line.
Decrement x and a by 1.
5. Stop
Program:
import [Link].*;
int n=[Link]();
[Link](" ");
}
for (int k=1;k<=i;k++)
[Link](k);
[Link](l-1);
[Link]();
int x=n-1;
int a=n-2;
[Link](" ");
{
[Link](i);
[Link](k);
[Link]();
x--;
a--;
VARIABLE DESCRIPTION:
[Link] Variable Data type Description
1. in Scanner Used to take user input from the console.
2. n int Input number that determines the number of
rows in the upper half of the pattern.
3. i int Loop counter used to iterate through rows of the
upper half of the pattern.
4. j int In the first loop: controls spaces before numbers
in each row (decreasing spaces). In the second
loop: controls the rows of the lower half of the
pattern.
5. k int Used to print ascending numbers from 1 to i in
each row.
6. l int Used to print descending numbers from i-1 to 1
to form the mirrored number pyramid.
7. m int A loop that always runs once to print a single
space (could be omitted).
8. x int Controls the upper limit of the ascending
numbers in the lower half (starts from n-1 and
decreases).
9. a int Controls the upper limit for descending numbers
in the lower half (starts from n-2 and decreases).
10. c int Used to print increasing spaces before numbers
in the lower half.
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
String Tokenizer
Exercise No.: 6
Date:
AIM:
To write a java program to find number of words by using string
tokenizer.
ALGORITHM:
1. Start
2. Import scanner class package.
3. Declare the class name.
4. Use scanner class to get an string.
5. Store the value in str.
6. Create the object for string tokenizer and store the value in st.
7. Use the function ([Link]) to count number of tokens.
8. Print the number of tokens.
9. Stop
Program:
import [Link].*;
[Link]([Link]());
}
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. str String To store input value
3. st String String tokenizer object
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Print the Initial
Exercise No.: 7
Date:
AIM:
To write a java program to print the initial letter by using String
Tokenizer.
ALGORITHM:
1. Start
2. Get the input for the user and store in str.
3. Create object for String Tokenizer and store the value in st.
4. Check whether the string has more tokens using the function ([Link]
MoreTokens()).
5. Extract the next token by using the statement [Link]() and
store it in are1.
6. Use ([Link] At(0)) to print the initial letter.
7. Stop
Program:
import [Link].*;
{
Scanner in= new Scanner ([Link]);
while( [Link] () )
[Link]([Link](0));
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. st String String Tokenizer object
3. str String Store the input value
4. Str1 String To store next token
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Frequency of palindrome
Exercise No.: 8
Date:
AIM:
To write a java program to find the frequency of the palindrome.
ALGORITHM:
1. Start
2. Get the String from the user by using scanner class and store the
value in str.
3. Store a variable int c=0.
4. Create object for string Tokenizer and store it in st.
5. Use while loop and check whether the st has more tokens.
6. If it has more token then declare a variable str1 and store the value of
next token.
7. Use for loop and get the reverse of the input value and store it in str2.
8. Check whether the input value and the reverse is same by using if
([Link] (str1)).
9. If both the input value and reverse value are same the increment as
(C++).
10. Display the frequency of the palindrome which is store in c.
11. Stop
Program:
import [Link].*;
int c=0;
String str2;
while([Link]())
String str1=[Link]();
str2="";
char ch = [Link](i);
str2=ch+str2;
}
if ([Link](str1))
[Link](str1);
c++;
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. st String String Tokenizer object
3. str String To store the input value
4. Str1 String To store next token
5. Str2 String To store the reverse of the input value
6. c int To store the number of palindrome
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Matrix Multiplication
Exercise No.: 9
Date:
AIM:
To write a java program to multiply two matrix.
ALGORITHM:
1. Start
2. Use scanner class and get the number of row and column of the
matrix.
3. Store the number of row and column of first array in m and n.
4. Store the number of row and column of second array in p and q.
5. Store array elements in arr1 and arr2 using for loop.
6. Check whether (n==p).
7. Then us the operation (arr3[i][j]=arr3[i][j]+arr[i][k]*arr2[k][j]) to
find the product of two matrix.
8. Then display arr3 which is the product of two matrix.
9. if (n!=p) Display that multiplication is not possible.
10. Stop
Program:
import [Link].*;
int m= [Link]();
int n= [Link]();
int p=[Link]();
int q= [Link]();
if (n==p)
{
[Link]("Enter the array elements of first array");
arr1 [i][j]=[Link]();
arr3 [i][j]=0;
arr3[i][j]=arr3[i][j]+arr1[i][k]*arr2[k][j];
}
[Link](arr3[i][j]+" ");
[Link]();
else
[Link]("Not possible");
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. m int Store [Link] rows of first arrary
2. n int Store [Link] column of first array
3. p int Store [Link] rows of second array
4. q int Store [Link] column of second array
5. arr1 int To store first array elements
6. arr2 int To store second array elements
7. arr3 int To store the product of both array
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Date:
AIM:
To write a java program to get the row of array in ascending order.
ALGORITHM:
1. Start
2. Use scanner class to get number of rows and column and store it in
the variable m and n.
3. Use for loop and store array element in arr.
4. Use three nested for loops.
5. Inside the inner most for loop check if the number in a row is greater
than a previous number.
6. If it is greater then swap.
7. Continue the process until the row is sorted.
8. Then display the swapped array.
9. Stop
Program:
import [Link].*;
int m= [Link]();
int n= [Link]();
if (arr [i][j]>arr[i][k])
arr[i][k]= temp;
[Link]();
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. m int To store number of rows of array
2. n in To store number of columns of array
3. arr int To store array elements
4. temp int To store a temporary variable
5. in int Scanner object
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Sum of Diagonal
Exercise No.: 11
Date:
AIM:
To write a java program to find the sum of left and right diagonal of an
array.
ALGORITHM:
1. Start
2. Use scanner class and get number of rows and column of the array.
3. Store number of rows and column in the variable m and n.
4. Store array elements using for loop in arr.
5. Check whether (i==j) then store right diagonal sum S1= S1+arr[i][j].
6. Check whether (i+j==m-1) then store left diagonal sum S2=S2+arr[i]
[j].
7. Display left diagonal sum S1 and right diagonal sum S2.
8. Stop
Program:
import [Link].*;
int m= [Link]();
int n= [Link]();
int s1=0;
int s2=0;
if (i==j)
s1= s1+arr[i][j];
if (i+j==m-1)
s2= s2+arr[i][j];
{
for (int j=0;j<n;j++)
[Link](arr[i][j]+" ");
[Link]();
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner Object
2. m int Store [Link] rows of array
3. n int Store [Link] column of array
4. arr int Store array elements
5. S1 int Store sum of right diagonal
6. S2 int Store sum of left diagonal
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Date:
AIM:
To write a java program to find the sum of digits using function.
ALGORITHM:
1. Start
2. Use scanner class and get a value store it in n.
3. Use function sod (int x) and proceed the program.
4. Use while loop and check whether x>0.
5. If it is true the split the digits using x%10 and store it in digits.
6. Store sum = sum+ digit.
7. Use (x= x/10) to return the value after separating the last digit.
8. Create object for class.
9. Display the sum of digits by ([Link](n)).
10. Stop.
Program:
import [Link].*;
int sum=0;
while(x>0)
sum= sum+digit;
x=x/10;
return sum;
int n= [Link]();
[Link]([Link](n));
}
}
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. x int Formal parametric variable
3. n int To store the input value
4. digit int To store digit of number
5. sum int To store the sum of digits
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Date:
AIM:
To write a java program to find if a string is palindrome or not by using
functions.
ALGORITHM:
1. Start
2. Create a function get() and use it to get an input from the user.
3. Creature a function palin().
4. Inside the function use the for loop (int i=0;i<[Link]();i++).
5. split the words and reverse it.
6. Create main class and object and call the function get().
7. If the output from palin() is true then print it is a palindrome else it is
not a palindrome.
8. Stop
Program:
import [Link].*;
String word;
void getdata()
word= [Link]();
String str="";
int c=0;
char ch = [Link](i);
str=ch+str;
}
if ([Link](word))
c=1;
else
c=0;
return c;
boolean palin()
int m= palin(word);
if (m==1)
return true;
else
return false;
[Link]();
if ( obj. palin()==true)
[Link]("It is a palindrome");
else
}
}
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. word String To store input value
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
AIM:
To write a java program to find he frequency of words and blank space in
a string.
ALGORITHM:
1. Start
2. Use the function getsent() and get the value from the user and store it
in word.
3. Use the function findword() to check number of words in str.
4. By using string tokenizer with the condition [Link] we can
find number of words and store it in c.
5. Use the function findspace() to check number of space.
6. Inside the function using for loop, for(int i=0;i<[Link]();i++).
7. split the words by char ch=[Link](i).
8. Check if(ch==1) then store s++.
9. Stop
Program:
import [Link].*;
String word;
int s;
int c;
int p;
void getSent()
int findword()
c=[Link]();
return c;
int findspace()
if (ch==' ')
s++;
return s;
[Link]();
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Factorial using recursion
Exercise No.: 15
Date:
AIM:
To write a java program to find the factorial of a number using recursion.
ALGORITHM:
1. Start
2. Use scanner class to get and input from the user and store it in.
3. inside the function (int x).
4. Check if (x==1) then return 1.
5. else return x*fact (x-1).
6. Call the object.
7. Print the factorial by [Link](a).
8. Stop
Program:
import [Link].*;
int fact(int n)
if (n==1)
return 1;
else
return n*fact(n-1);
int a = [Link]();
VARIABLE DESCRIPTION:
[Link] Varaible Data Type Purpose
1. in int Scanner object
2. a int To store input value
3. n int Function parametric
variable
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Date:
AIM:
To write a java program to find the sum of digits using recursion.
ALGORITHM:
1. Start
2. Get an input from user and store the value in x.
3. Inside the function sum(int n).
4. Check if (n==1) then return 1.
5. Else return (n%10+sum(n/10).
6. Call the object.
7. Print the sum of digits by [Link](x).
8. Stop
Program:
import [Link].*;
int sum(int n)
if (n==0)
return 0;
else
return n%10+sum(n/10);
int x= [Link]();
VARIABLE DESCRIPTION :
[Link] Varaible Data Type Purpose
1. in int Scanner object
2. x int To store input value
3. n int Function parametric
variable
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Date:
AIM:
To write a java program to check whether the number is both
palindrome and prime number.
ALGORITHM:
1. Start
2. Create a function get() and use it to get an input.
3. Creature a function palin().
4. Inside the function use the for loop (int i=0;i<[Link]();i++)..
5. split the words and reverse it
6. if the reversed word is equal to the orginal word return true else
false.
7. Create a function prime() and check the number of factors of number
using while loop.
8. If the number of factors is equal to return true else false
9. Create main function and object.
10. If the output from palin() and prime () is true then print prime
palindrome.
11. Stop
Program:
import [Link].*;
int n= [Link]();
boolean x= [Link](n);
boolean y= [Link](n);
if (x==y&&x==true)
boolean isprime(int a)
int c=0;
if (a%i==0)
c++;
if(c==2)
return true;
else
return false;
boolean ispaline(int a)
while (a>0)
rev= rev*10+digit;
a=a/10;
if (b==rev)
return true;
else
return false;
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. n int To store input value
3. x boolean To receive true or false from main function
4. y boolean To receive true or false from main fuction
5. c int To store number of factore
6. a int Function parametric variable
7. b int Temporary variable
8. rev int To store the reversed value
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Date:
AIM:
To write a java program to calculate the sum of exponential series
(1+x^1/1!+x^2/2!+...+x^n/n!).
ALGORITHM:
1. Start
2. Get input from the user by using scanner class and store the value in
a and b.
3. Store the sum value equal to 1.
4. Use the function series(int x, int y).
5. Create a nested for loop.
6. Inside the inner for loop find the factorial of the number.
7. To find sum of series use the statement sum=sum+[Link](x,i)/f.
8. Create the object for the class and call the function.
9. Print the sum.
10. Stop
Program:
import [Link].*;
double sum=1;
double f=1;
f=f*j;
sum= sum+[Link](x,i)/f;
return sum;
int a= [Link]();
int b= [Link]();
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. x int Formal parameterized
variable
3. y int Formal parameterized
variable
4. a int To store input value
5. b int To store input value
6. sum double To store sum of series
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Linear Search
Exercise No.: 19
Date:
AIM:
To write a java program to search an element in array by using linear
search.
ALGORITHM:
1. Start
2. Use scanner class and get the number of array elements and store it
in n.
3. Using for loop store the array element given by the user.
4. Input a element to be search and store it in k.
5. Initialize a variable int c=0.
6. Use for (int i=k;i<n;i++) and check if k==arr[i] then store in c.
7. Check if (c==1) the print the element is found.
8. Otherwise print element is not found.
9. Stop
Program:
import [Link].*;
int n= [Link]();
int k= [Link]();
int c=0;
if (k==arr[i])
c=1;
if (c==1)
else
[Link]("The element is not present");
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. n int To store number of elements in array
3. Arr[] int To store array element
5. k int To store the element to be searched
6. c int Temporary variable
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Binary Search
Exercise No.: 20
Date:
AIM:
To write a java program to check whether an element is present in the
array by using binary search.
ALGORITHM:
1. Start
2. Get the number of array element by using scanner class and store it
in n.
3. Use for loop and store the array element in arr[].
4. Get the element to be searched from the user and store it in k.
5. Create a while loop with condition (l<h).
6. Find the middle of the array and store it in mid.
7. If the mid element equal to the k change c value to one.
8. If the mid element is greater than k the upper limit is mid-1.
9. Else the lower limit is mid+1.
10. If (c= 1) element is found else the element is not found.
11. Stop
Program:
import [Link].*;
int n= [Link]();
int k=[Link]();
while (l<h)
{
mid = (l+h)/2;
if (arr [mid]==k)
c=1;
break;
else if (arr[mid]>k)
h= mid -1;
else
l=mid+1;
if (c==1)
[Link]("Element found");
else
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in Scanner Used to take input from the user.
2. n int Stores the size of the array (i.e., number of
elements).
3. arr[] int[] Integer array to hold the elements entered by
the user.
4. i int Loop control variable used to iterate over the
array.
5. k int The element to be searched in the array.
6. l int The lower bound index used in binary search.
7. h int The upper bound index used in binary search.
8. c int A flag variable to indicate whether the
element is found (1) or not (0).
9. mid int Stores the middle index of the current search
range in binary search
.
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Clock
Exercise No.: 21
Date:
AIM:
To write a java program to convert time in numbers to time in words.
ALGORITHM:
1. Start
2. Use scanner and get the input form user and store hours in h and
minutes in m.
3. In a string s[] store the elements 0 to 29 in words.
4. check if (m==30) then print half past s[h].
5. Check if (m==15) then print quarter past s[h].
6. Check if (m==45) then print quarter to s[h+1].
7. Check if (m<30) then print (s[m]+ “min past” +s[h]).
8. Check if (h==12 && m>30) then print (s[60-m]+ “ min to 1”).
9. Check is (m>30) then print (s[60-m] + “mins to “ +s [h+1].
10. Stop
Program:
import [Link].*;
int h= [Link]();
int m= [Link]();
String s[]={"
","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Ele
ven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eight
een","Ninteen","Twenty","Twenty one", "Twenty Two","Twenty
Three","Twenty Four","Twenty Five","Twenty Six","Twenty
Seven","Twenty Eight","Twenty Nine"};
if (m==30)
else if (m==45)
if (h==12)
else
else if (m<30)
else if (h==12&&m>30)
else if (m>30)
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. h int To store hour
3. m int To store minute
4. s String To store numbers in words
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Number in words
Exercise No.: 22
Date:
AIM:
To write a java program to Convert the number in words.
ALGORITHM:
1. Start
2. Using string w[] and store the numbers in words.
3. Use scanner class and get the number form the user and store it in n.
4. Use arr [4] and store the input value in arr[] by using arr[--c]= n%10;
n=n/10 inside the for loop.
5. Check if (arr[0]!=0) then print(w[arr[0]]+ “ Thousand”).
6. Check if (arr [1]!=0) then print (w[arr[1]]+ “Hundred”).
7. Inside the if condition if (arr [2]1=0).
8. Check if ([2]>=2) then print (w[arr[2]+18).
9. Check if ([3]!=0) Then print w [arr[3]]) and close the if condition.
10. Check if ( arr[2]==1) then print s[arr[s]+10).
11. Check if (arr[2]==0 && arr [3]!=0) then print (w[arr[3]]).
12. Stop
Program:
import [Link].*;
String w[]={"
","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Ele
ven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eight
een","Ninteen","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty"
,"Ninty"};
int n=[Link]();
int c=4;
while(n!=0)
arr[--c]=n%10;
n=n/10;
while(c!=0)
arr[--c]=0;
if (arr[0]!=0)
if (arr[1]!=0)
if (arr[2]!=0)
if (arr[2]>=2)
[Link](w[arr[2]+18]+" ");
if (arr[3]!=0)
[Link](w[arr[3]]+" ");
}
if (arr[2]==1)
[Link](w[arr[3]+10]+" ");
else
if (arr[2]==0&&arr[3]!=0)
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. in int Scanner object
2. w String store number is words
3. arr int To store the input value in array
4. n int To store input value
5. c int Used in accessing the element of the array
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Overloading
Exercise No.:23
Date:
AIM:
To write a java program to find the area of circle, square and rectangle
by using overloading.
ALGORITHM:
1. Start
2. Create a function and store a parameterized variable int x.
3. Return x*x for this function.
4. Create another function int area and store int x and int y and
parameterized variable.
5. Return x*y for this function.
6. Create another function with double area and store double x as a
parameterized variable .
7. Retune 3.14*x*y for this function.
8. Create the object for the class and class the function.
9. Stop
Program:
public class Overloading
return x*x;
}
return x*y;
return 3.14*x*x;
[Link]("Area of rectangle="+b);
[Link]("Area of Circle=+"+a);
} }
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. x int Parameterized variable
2. y int Parameterized variable
3. a double Calls the function
4. b int Calls the function
5. c int Calls the function
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Overriding
Exercise No.: 24
Date:
AIM:
To write a java program to demonstrate over riding concept.
ALGORITHM:
1. Start
2. Define class ABC.
3. Declare integer variable x and initialize it to 5.
4. Define method display():
5. Print the value of variable x of class ABC.
6. Define class XYZ which extends ABC.
7. Declare integer variable x and initialize it to 4.
8. Override method display():
9. Print the value of variable x of class XYZ.
10. Define method display1():
11. Call [Link]() (this calls ABC's display() method).
12. Call display() (this calls XYZ's overridden display() method).
13. Create an object of class XYZ.
14. Call display1() using the object.
15. Execution flow for display1().
16. [Link]() prints value of x from ABC (which is 5).
17. display() prints value of x from XYZ (which is 4).
18. Stop
Program:
public class ABC
int x= 5;
void display()
[Link](x); }
int x=4;
void display()
[Link](x);
void display1 ()
{
[Link]();
display();
} }
VARIABLE DESCRIPTION :
[Link] Variable Data Type Purpose
1. x int To store a value
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Selection Sort
Exercise No.: 25
Date:
AIM:
To write a java program to arrange the element in the array by using
selection sort.
ALGORITHM:
1. Start
2. Create a Scanner object to read user input.
3. Display message: "Enter any number of elements".
4. Read an integer n (size of the array).
5. Declare an integer array arr of size n.
6. Display message: "Enter the array elements".
7. Loop from i = 0 to n - 1:
8. Read arr[i] from the user.
9. Sort the array in ascending order:
Loop i from 0 to n - 1:
Loop j from i + 1 to n - 1:
If arr[i] > arr[j]:
Swap arr[i] and arr[j] using a temporary variable.
10. Display message: "Sorted array is".
11. Loop from i = 0 to n - 1:
12. Print arr[i].
13. Stop
Program:
import [Link].*;
int n= [Link]();
arr [j]=temp;
[Link](arr[i]+" ") ;
VARIABLE DESCRIPTION:
S.N Variable Data Type Purpose
o
1. in Scanner Used to take input from the user.
2. n int Stores the number of elements the user wants
to enter in the array.
3. arr int[] Array of integers that stores n elements
entered by the user.
4. i int Loop control variable used for traversing the
array in outer loop
5. j int Loop control variable used for comparing
elements in the inner loop.
6. Temp int Temporary variable used to swap values of
array elements during sorting.
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Bubble sort
Exercise No.: 26
Date:
AIM:
To write a java program to arrange the element in the array by using
bubble sort.
ALGORITHM:
1. Start
2. Create a Scanner object to take input from the user.
3. Display "Enter the size of the array".
4. Read the size of the array and store it in variable n.
5. Declare an integer array arr of size n.
6. Display "Enter array elements".
7. Use a loop to read n integers from the user and store them in the
array arr.
8. Perform Bubble Sort:
9. Repeat the following steps for i = 0 to n-1:
For j = 0 to n-2:
If arr[j] > arr[j+1], then:
Swap arr[j] and arr[j+1].
10. Display "Sorted array is".
11. Use a loop to print the elements of the sorted array.
12. Stop
Program:
import [Link].*;
int n= [Link]();
{
arr [i]=[Link]();
arr[j]= arr[j+1];
arr[j+1]= temp;
[Link](arr[i]+" ");
VARIABLE DESCRIPTION:
S.N Variable Data Type Purpose
o
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Insertion sort
Exercise No.: 27
Date:
AIM:
To write a java program to arrange the element in the array by using
insertion sort.
ALGORITHM:
1. Start
2. Declare an integer array a[] and integer variables n, i, j, and temp.
3. Create a Scanner object for input.
4. Prompt the user to enter the size of the array (n).
5. Initialize array a of size n.
6. Assign a sentinel value -999999999 to a[0].
7. Prompt the user to input n - 1 array elements (from index 1 to n - 1)
and store them in a[i].
8. Perform insertion sort on array elements from index 1 to n - 1:
For each index i from 1 to n - 1:
Store a[i] in temp.
Set j = i - 1.
While j >= 0 and temp < a[j], do:
Shift a[j] to the right: a[j+1] = a[j].
Decrement j.
Insert temp at a[j+1].
9. Print the sorted array elements from index 1 to n - 1.
10. Stop
Program:
import [Link].*;
{
public static void main (String args [])
int n= [Link]();
a[0]=-999999999;
int j;
a[i]=[Link]();
for ( j=i-1;j>=0&&temp<a[j];j--)
a[j+1]=a[j];
a[j+1]=temp;
VARIABLE DESCRIPTION:
S.N Variable Type Purpose
o
1. in Scanner Used to take input from the user via the
console.
2. n int Stores the size of the array as entered by
the user.
3. a[] int[] Integer array that holds the elements to
be sorted.
4. i int Loop control variable for iterating
through array elements.
5. j int Used to traverse the sorted part of the
array during insertion sort.
6. Temp int Temporarily stores the current element
to be inserted in its correct position.
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified.
Kaprekar Number
Exercise No.: 28
Date:
AIM:
To write a java program to check whether a given number is a Kaprekar
number or not.
ALGORITHM:
1. Start
2. Accept a number a from the user
3. Assign d = a and e = a for later use
4. Compute the square of the number (though b = [Link](a, 2) is not
used further in this program)
5. Count the number of digits in a
Initialize c = 0
While a > 0:
Extract last digit using a % 10
Increment digit count c++
Divide a by 10 (remove last digit)
6. Reverse the digits of the original number
Use d as a copy of the original number
Initialize num = 0
For i = 1 to c:
Extract last digit: digit = d % 10
Add digit to reverse: num = num * 10 + digit
Remove last digit from d: d = d / 10
7. Reverse num again and store in rev
Initialize rev = 0
While num > 0:
Extract digit: digit = num % 10
Append to rev: rev = rev * 10 + digit
Remove last digit from num: num = num / 10
8. Compare original number e and reversed number rev
If equal:
Print "The number is Kaprekar number"
Else:
Print "The number is not a Kaprekar number"
9. Stop
Program:
import [Link].*;
Constructor (int m)
int n=m;
}
int a= [Link]();
int d= a;
int c=0;
int num=0;
int rev=0;
int e=a;
while(a>0)
c++;
a=a/10;
num= num*10+digit;
d=d/10;
}
while (num>0)
rev= rev*10+digit;
num= num/10;
if ( e==rev)
else
VARIABLE DESCRIPTION:
S.N Variable Data Purpose
o Type
1. a int Stores the user-input number and is used for
digit counting.
2. b double Stores the square of the number
3. c int Counts the number of digits in the number.
4. d int A copy of the original number used for
reversing.
5. digit int Temporarily stores the last digit during
reversal and counting.
6. num int Stores the reversed number in the first
reversal step.
7. rev int Stores the final reversed number
8. e int Another copy of the original number used for
comparison.
9. in Scanner Scanner object used to take user input.
10. args String[] Command-line argument array
11. m int Parameter of the constructor Constructor(int
m)
12. n int Local variable inside constructor
OUTPUT:
RESULT:
Thus the java program compiled successfully without any error and
output verified.
GCD
Exercise No.: 29
Date:
AIM:
To write a Java program to find the Greatest Common Divisor (GCD) of
two numbers using a recursive function.
ALGORITHM:
1. Start
2. Prompt the user to input two integers.
3. Read the two integers, say a and b.
4. Call a recursive method GCD(int x, int y):
If x == y, return x as the GCD.
If x > y, call GCD(x - y, y).
Else, call GCD(x, y - x).
5. Display the returned result as the GCD of the two numbers.
6. Stop
Program:
import [Link].*;
if(x==y)
return x;
else if(x>y)
return GCD(x-y,y);
else
return GCD(x,y-x);
{
Scanner in=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
} }
VARIABLE DESCRIPTION:
[Link] Variable Data Type Purpose
1. a int Stores the first number input by the user.
2. b int Stores the second number input by the user.
3. x int Parameter used in the recursive method to
hold one of the current values being processed
for GCD.
4. y int Parameter used in the recursive method to
hold the other current value for GCD
computation.
5. in Scanner Scanner object used to take input from the
user.
6. ob GCDRecurse Object of the class GCDRecurse used to call the
GCD method.
OUTPUT:
RESULT:
Thus the java program executed successfully without any error and
output verified
Harshad Number
Exercise No.: 30
Date:
AIM:
To write a Java program that checks whether a given number is a
Harshad Number or not.
ALGORITHM:
1. Start
2. Create a method Harshad(int num) to perform the check.
3. Store the original number in a variable x for later use.
4. Initialize a variable sum to 0 to store the sum of the digits.
5. Use a loop to extract each digit of num:
Get the last digit using num % 10
Add the digit to sum
Remove the last digit using num / 10
6. After the loop, check if the original number x is divisible by sum:
If x % sum == 0, print "It is a Harshad Number"
Else, print "It is not a Harshad Number"
7. In the main method:
Take input from the user.
Call the Harshad() method with the entered number.
8. Stop
Program:
import [Link].*;
{
int x=num;
int sum=0;
while(num>0)
int digit=num%10;
sum=sum+digit;
num=num/10;
if(x%sum==0)
else
[Link]("Enter a number");
int n=[Link]();
[Link](n);
}
VARIABLE DESCRIPTION:
S.N Variable Data Type Purpose
o
1. num int The number entered by the user, which is
checked for being a Harshad Number.
2. x int A copy of the original number (num) used
for final divisibility check.
3. sum int Stores the sum of the digits of the number.
4. digit int Stores the last digit of the number during
each iteration of the loop.
5. n int Variable used in main() to store user input
before passing it to the Harshad() method.
6. in Scanner Scanner object used to take input from the
user.
7. ob HarshadNum Object of the class HarshadNum used to
call the Harshad() method.
OUTPUT:
RESULT:
Thus the java program compiled successfully without any error and
output verified.