Program 1:
TO INPUT AN ARRAY AND SEARCH FOR AN ELEMENT IN THE ARRAY.
CHECK IF THE ELEMENT IS PRIME.
Algorithm:
Step 1: Input the elements in the array.
Step 2: Print the array elements.
Step 3: Take an element as an input from user and check if it is present in the
array or not.
Step 4: If the element is present in the array, check whether it is prime or not.
Step 5: If element is prime, display an appropriate message, or else display
negative message.
Source Code:
import java.util.*;
public class SearchPrime //Class Name
public static void main(String args[]) //Main Method
Scanner sc=new Scanner(System.in);
int m[]=new int[10];
int i,p,k=0,c=0;;
System.out.println("Enter the number of elements in the array: ");
int n=sc.nextInt(); //Array Size
System.out.println("The elements in the array is: ");
for(i=0;i<n;i++)
m[i]=sc.nextInt(); //Array Elements
System.out.println("Enter the number to be searched: ");
p=sc.nextInt(); //Number to be searched
for(i=0;i<n;i++)
if(m[i]==p)
k=1; //The Searching Part
if(k==1)
for(i=1;i<=n;i++)
if(n%i==0)
c=c+1; //The Checking Part (whether it is prime)
if(c==2) //The Printing Part
System.out.println("The Number is present and prime");
else
System.out.println("The Number is not present and not
prime");
else
System.out.println("The Number is not present");
Output:
Program 2:
CHECK IF A MATRIX IS SYMMETRIC OR NOT. THE SIZE OF THE ARRAY
SHOULD NOT BE LESS THAN 3 AND GREATER THAN 10. AND ALSO
FIND THE SUM OF DIAGONAL ELEMENTS.
Algorithm:
Step 1: Input the array size (as per given conditions) and elements and print the
array.
Step 2: Check whether the array Matrix is symmetric or not.
Step 3: If step-2 is positive then print an appropriate message, else print a
negative message.
Step 4: Take a variable and use it to find the Sum of the Diagonal elements.
Step 5: Display the calculated sum.
Source Code:
import java.util.*;
public class Symmetry //Class Name
public static void main(String args[]) //Main Method
Scanner sc=new Scanner(System.in);
int i,j;
int l=0,r=0,c=1;
System.out.println("Enter the size of the square matrix: ");
int m=sc.nextInt(); //Matrix Size
int a[][]=new int[m][m];
if(m<3||m>9)
{
System.out.println("The matrix is out of size.");
return;
System.out.println("enter the array elements: ");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
a[i][j]=sc.nextInt();
if(i==j) //Finding the sum of the diagonal elements
l=l+a[i][j];
if(i+j==m-1)
r=r+a[i][j];
System.out.println("The sum of left diagonal elements is: "+l);
System.out.println("The sum of right diagonal elements is: "+r);
System.out.println("The original matrix is: ");
for(i=0;i<m;i++) //Printing the original Matrix
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+” “);
System.out.println();
for(i=0;i<m;i++)
for(j=0;j<m;j++)
if(a[i][j]!=a[j][i]) //Checking the Symmetry of Matrix
c=0;
break;
if(c==0)
System.out.println("The matrix is not Symmetric");
break; //Terminating the loop
if(c==1) //Printing Part
System.out.println("The matrix is Symmetric");
}
Output:
Program 3:
TO FIND THE HAMMING DISTANCE BETWEEN TWO BINARY NUMBERS.
Algorithm:
Step 1: Enter the two binary numbers, by putting the digits of the binary numbers
in two arrays respectively.
Step 2: Print the two arrays i.e., the two binary numbers.
Step 3: Check that whether at equal positions of the arrays there are the same
digits or not.
Step 4: The Hamming distance between the numbers will be the number of times
at equals positions of the two arrays there will be different or unequal numbers.
Step 5: Print the Hamming Distance between the two binary numbers.
Source Code:
import java.util.*;
public class Hamming //Class Name
public static void main(String args[]) //Main Method
int i,j,d=0;
int m[]=new int[20]; //Array for First Binary Number
int n[]=new int[20]; //Array for Second Binary Number
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of binary digits: ");
int p=sc.nextInt(); //Size of the two binary numbers
System.out.println("Enter the first binary number: ");
for(i=0;i<p;i++)
m[i]=sc.nextInt(); //First Binary Number
System.out.println("Enter the secondary binary number: ");
for(j=0;j<p;j++)
n[j]=sc.nextInt(); //Second Binary Number
System.out.println("The first binary number: ");
for(i=0;i<p;i++)
System.out.print(m[i]);
System.out.println();
System.out.println("The second binary number: ");
for(j=0;j<p;j++)
System.out.print(n[j]);
System.out.println();
for(i=0;i<p;i++)
for(j=0;j<p;j++)
if(m[i]!=n[j])
d=d+1; //Finding the Hamming Distance
i=i+1;
System.out.println("The hamming distance is: "+d); //Printing
}
}
Output:
Program 4:
TO TAKE AN ARRAY AS INPUT AND PRINT THE ARRAY AFTER ‘x’ TIMES OF CYCLIC
SHIFTING.
Algorithm:
Step 1: Take input the array size and array elements from the user.
Step 2: Print the Original Array.
Step 3: Take the value of ‘x’ from the user.
Step 4: Shift the array elements in a cyclic way upto ‘x’ times.
Step 5: Print the array after every cyclic shift.
Source Code:
import java.util.*;
public class Shift //Class Name
public static void main() //Main Method
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Number of Array elements : ");
int n=sc.nextInt(); //Array Size
int a[]= new int[n];
int i,j,t;
System.out.println("Enter the Array elements : ");
for(i=0;i<n;i++)
a[i]=sc.nextInt(); //Array Elements
System.out.println("The Original Array : ");
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ,"); //Printing the Original Array
System.out.println();
System.out.println("Enter the value of x : ");
int x=sc.nextInt(); //No. of times the array will shift in a cycle
for(j=0;j<x;j++) //Cyclic Shifting the array upto ‘x’ times
t=a[n-1];
for(i=n-1;i>=1;i--)
a[i]=a[i-1];
a[0]=t;
System.out.println("The New Array : ");
for(i=0;i<n;i++) //Printing the Shifted Array after every shift
System.out.print(a[i]+" ,");
System.out.println();
}
Output:
Program 5:
TO CHECK IF A NUMBER IS UNIQUE NUMBER OR NOT.
(A NUMBER IS UNIQUE IF IT HAS NO DUPLICATE DIGITS.)
Algorithm:
Step 1: Declare a class for the required process or program.
Step 2: Input a number from user that he or she wants to check. Take a Flagging
variable whose initial value is Zero.
Step 3: Check that whether the digit at the ith position of the number is equal to
the (i+1)th position of the same number or not.
Step 4: If result of Step-3 is positive then increase the value of the Flagging
variable and terminate the loop.
Step 5: If the value of the Flagging variable is equal to One, then the number is
Unique, else it is not.
Source Code:
import java.util.*;
public class Unique //Class Name
public static void main(String args[]) //Main Method
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number you want to check:");
String num= sc.next(); //Number you want to check
int len=num.length(); //No. of Digits in the given Number
int flag=0, i, j;
for(i=0;i<len-1;i++)
for(j=i+1;j<len;j++)
if(num.charAt(i)==num.charAt(j)) //Checking Part
{
flag=1; //Increasing the value of flagging variable
break; //Terminating the loop
if(flag==0) //Printing Part
System.out.println("The number is unique.");
else
System.out.println("The number is not unique.");
Output:
Program 6:
TAKE AN ARRAY AS INPUT AND SORT IT USING BUBBLE SORT.
(DESCENDING SORT)
Algorithm:
Step 1: Declare a class for the given program.
Step 2: Take input the array size and array elements from user and hence, print
the Unsorted or Original Array.
Step 3: Using two loops swap the values if the number at the jth position of the
array is less than that of the number at the (j+1)th position.
Step 4: Repeat Step-3 upto n times.
Step 5: Print the Sorted Array.
Source Code:
import java.util.*;
public class Bubble_Sort //Class Name
public static void main(String args[]) //Main Method
Scanner sc= new Scanner(System.in);
int i,j,n;
int x=0;
System.out.println("Enter the number of inputs you want to give : ");
n= sc.nextInt(); //Array Size
int a[]= new int[n];
for(i=0;i<n;i++)
{
a[i]= sc.nextInt(); //Array Elements
System.out.println("The Unsorted Array : ");
for(i=0;i<n;i++)
System.out.print(a[i]+" ,"); //Printing the Unsorted Array
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]<a[j+1]) //Sorting in Descending Order
x=a[j]; //Swapping Part
a[j]=a[j+1];
a[j+1]=x;
System.out.println();
System.out.println("The Sorted Array : ");
for(i=0;i<n;i++)
System.out.print(a[i]+" ,"); //Printing the Sorted Array
Output:
Program 7:
TAKE A NUMBER FROM USER AND FIND ITS FACTORIAL USING RECURSION.
Algorithm:
Step 1: Declare a class for the given program.
Step 2: Take the number from user as input.
Step 3: Make a separate integer type static function.
Step 4: In the recursive function, check - If the number is less than or equal to
One, then return ‘1’, else return the factorial of that number.
Step 5: Print the factorial by calling the Recursive Function.
Source Code:
import java.util.*;
public class FactorialRecursion //Class Name
static Scanner sc= new Scanner(System.in); //Global static Scanner object
public static void main() //Main Method
System.out.println("Enter the Number : ");
int n=sc.nextInt(); //Number whose factorial we have to find
System.out.print(“Factorial of “+n+” = “+fact(n)); //Calling the RF
public static int fact(int x) //Recursive Function (RF)
if(x<=1) //Base Condition
return 1;
}
return x*fact(x-1); //Recursive Statement
Output:
Program 8:
TAKE A SENTENCE FROM USER AND FIND OUT THE NUMBER OF WORDS IN THE
SENTENCE THAT HAVE EVEN NUMBER OF ALPHABETS.
Algorithm:
Step 1: Declare a class for the given program.
Step 2: Take the sentence from user as input. Initialize a flagging variable equals
to Zero.
Step 3: Extract the words from the sentence.
Step 4: Check whether is length of the word is even or odd. If even, increase the
value of the flagging variable by One.
Step 5: Print the number of words with even with even number of alphabets.
Source Code:
import java.util.*;
public class NOW //Class Name (NOW – Number Of Words)
public static void main(String args[]) //Main Method
Scanner sc= new Scanner(System.in);
System.out.println("Enter a String : ");
String str= sc.nextLine(); //Input String
int len=str.length();
int i,count=0,evencount=0;
String wd="";
for(i=0;i<len;i++)
char ch= str.charAt(i); //Extracting characters from String
if(ch!=' ' && i<len-1) //Extracting the Words
wd=wd+ch;
count++;
else
{
if(i==len-1) //Extracting the Words
wd=wd+ch;
count++;
if(count%2==0) //Checking if no. of alphabets is even
evencount++;
wd="";
count=0;
System.out.println("Number of words with even number of
alphabets is : "+evencount); //Printing Statement
Output:
Program 9:
TAKE A NUMBER FROM USER AND CHECK WHETHER IT IS ARMSTRONG NUMBER
OR NOT USING RECURSION.
(AN ARMSTRONG NUMBER IS A NUMBER WHOSE SUM OF THE CUBE OF ITS
DIGITS IS EQUAL TO THE NUMBER ITSELF)
Algorithm:
Step 1: Declare a class for the given program.
Step 2: Take the number from user as input.
Step 3: Make a separate integer type static function.
Step 4: In the recursive function, check – If the number is greater than zero,
extract its digits and find the sum of the cube of the digits and return it.
Step 5: Print whether the number is an Armstrong Number or not by calling the
Recursive Function.
Source Code:
import java.util.*;
public class ARMSTRONG //Class Name
public static void main() //Main Method
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a Number : ");
int num=sc.nextInt(); //Number which we have to check
int sum=summation(num); //Calling the Recursive Function
if(sum==num) //Printing Part
System.out.println("It is an Armstrong Number.");
else
System.out.println("It is not an Armstrong Number.");
public static int summation(int x) //Recursive Function (RF)
int sum=0;
if(x>0) //Base Condition
int r=x%10;
x/=10;
sum=(r*r*r)+summation(x); //Recursive Statement
return sum;
}
Output:
Program 10:
TAKE TWO BINARY NUMBERS AS INPUT FROM USER AND FIND THE SUMMATION
OF THEM AND PRINT THE SUM.
Algorithm:
Step 1: Declare a class for the given program.
Step 2: Take two binary numbers of equal length from the user.
Step 3: Check whether the two numbers are binary numbers or not.
Step 4: If result of Step-3 is positive, find their sum, else display a negative
message and terminate the loop.
Step 5: Print the summation of the two Binary Numbers.
Source Code:
import java.util.*;
public class BinaryAdd //Class Name
{
public static void main() //Main Method
Scanner sc= new Scanner(System.in);
String s1,s2,st="";
int n,i,c=0,s=0,count=0;
System.out.println("Enter two Binary Strings : ");
s1=sc.next(); //First Binary String
s2=sc.next(); //Second Binary String
n=s2.length(); //Size of the two binary strings
char ch1,ch2;
for(i=n-1;i>=0;i--) //Finding the sum of the two binary strings
ch1=s1.charAt(i);
ch2=s2.charAt(i);
if(ch1=='1'&&ch2=='0' || ch1=='0'&&ch2=='1')
if(c==0)
s=1;
c=0;
}
else
s=0;
c=1;
else if(ch1=='1'&&ch2=='1')
if(c==0)
s=0;
c=1;
else
s=1;
c=1;
else if(ch1=='0'&&ch2=='0')
if(c==0)
{
s=0;
c=0;
else
s=1;
c=0;
else
System.out.println("This is not a Binary String .");
count++;
break; //Terminating the loop
st=s+st; //Concating the sum with the initialized string
if(c!=0)
st=c+st; //Adding the carry (if generated) to the sum
if(count==0) //Printing Part
System.out.println("The Summation String is : "+st);
}
Output:
NAME : ISHAAN DAS
CLASS : XI (Science)
SECTION : A
ROLL NO : 35
SUBJECT : COMPUTER
index
Sl. No. Title Page No.
From To
1 To input an array and search for an element
in the array. Check if the array element is
prime or not.
2 Check if a Matrix is Symmetric or not. The
size of the matrix should not be less than 3 or
greater than 10. And also find the sum of the
Diagonal elements.
3 To find the Hamming Distance between two
Binary Numbers.
4 To take an array as input and print the array
after ‘x’ times of cyclic shifting.
5 To check if a number is a Unique Number or
not.
6 Take an array as input and sort it using
Bubble Sort Technique.(Descending Sort)
7 Take a number from user and find its
Factorial using Recursion.
8 Take a sentence from user and find out the
number of words in the sentence that have
even number of alphabets.
9 Take a number from user and check whether
it is an Armstrong Number or not using
Recursion.
10 Take two Binary Numbers as input from user
and find the summation of them and print
the sum.