COMPUTER
PROJECT
EFFORTS BY: ADITYA KASAUDHAN
12 F
1
INDEX
1. Program 1: HCF Using Recursion
2. Program 2: Factorial of Very Large Numbers
3. Program 3: Sum of Principle Diagonal and Secondary Diagonal of a Matrix
4. Program 4: Exact Division of very Large Numbers
5. Program 5: Fibonacci Series using Recursion
6. Program 6: Hexadecimal to Decimal Number System
7. Program 7: Decimal to Octal Number System
8. Program 8: Multiplication of Very Large Numbers
9. Program 9: Addition of Very Large Numbers
10. Program 10: Hanoi’s Tower Without Recursion
11. Program 11: Decimal to Binary Number System
12. Program 12: Power of Very Large Numbers
13. Program 13: Toggle of a String
14. Program 14: Hexadecimal to Binary Number System
15. Program 15: Octal to Binary Number System
16. Program 16: Insertion Sort
17. Program 17: Selection Sort
18. Program 18: Frequency of Substring
19. Program 19: Mean, Median and Mode
20. Program 20 Evil Number
21. Program 21: Perfect Number
22. Program 22: Spiral Matrix
23. Program 23: Deleting Word from a String
24. Program 24: Common Elements in two arrays
25. Program 25: Anti-rotated Matrix
Program: 1
Algorithm:
1. The program takes two integer inputs ‘n’ and ‘n1’.
2. Finds the HCF of ‘n’ and ‘n1’ using recursion.
3. Prints the HCF.
Syntax:
import java.util.*; //entering util package
class hcfrecursion // declaring class name
int hcf(int a,int b) // formal parameter
if(b==0) // condition
return a; // return statement
return hcf(b,a%b); // actual parameter
public static void main(String args[])
{ // start of program body
hcfrecursion ob=new hcfrecursion();
Scanner SS=new Scanner(System.in);
System.out.println("Enter number"); // output
int n=SS.nextInt(); // input
System.out.println("Enter number"); // output
int n1=SS.nextInt(); // input
System.out.println(ob.hcf(n,n1)); // output
} /// end of program body
} // end of program
Output:
Enter number
Enter number
1
Program: 2
Algorithm:
1. Takes an integer input ‘n’,
2. Creates three arrays ‘A’, ’B’ and ‘C’ with size ‘99999’.
3. Copies the digits of ‘n’ in array ’A’ and ‘B’
4. Multiplies the numbers in array from ‘n’ to ‘1’ till all the elements in ‘A’ become ‘0’
5. Stores the factorial in ‘C’
6. Prints the Factorial.
Syntax:
import java.util.*; // entering util library
class Factorial // declaring class name
void Convert( int n, int A[] ) // formal parameter
int d=n; // declaring variables
for ( int i=A.length-1;i>=0;i--,d=d/10) // loop 1
A[i]=d%10;
if ( d==0 )
break;
int Start ( int A[] ) // formal parameter
int d=0;
for ( int i=0;i<A.length;i++ )
if (A[i]!=0)
d=i;
break;
}
return d;
void Print( int A[], int d ) // formal parameter
for ( int i=d;i<A.length;i++ )
System.out.print(A[i]);
void Multiply ( int A[], int B[], int C[], int sA, int sB, int sC ) // formal parameter
int k=0;
for (int i=B.length-1;i>=sB;i-- )
int d=C.length-1-k;
for ( int j=A.length-1;j>=sA;j-- )
C[d]=(A[j]*B[i])+C[d];
d--;
for ( int j=C.length-1;j>0;j-- )
int t=C[j]/10;
C[j]=C[j]%10;
C[j-1]=C[j-1]+t;
k++;
}
void Transfer( int A[],int C[], int sC) // formal parameter
for ( int i=0;i<C.length;i++ )
A[i]=C[i];
C[i]=0;
public static void main (String args[])
Scanner ss=new Scanner ( System.in );
Factorial tt=new Factorial();
System.out.println(" Enter any number "); // output
int n=ss.nextInt();
int A[]=new int[99999];
int B[]=new int[99999];
int C[]=new int[99999];
int sC=0;
tt.Convert(n,A); // actual parameter
for ( int i=n-1;i>=1;i-- ) // loop
int sA=tt.Start(A);
tt.Convert(i,B);
int sB=tt.Start(B);
tt.Multiply(A,B,C,sA,sB,sC);
sC=tt.Start(C);
if ( i!=1 )
tt.Transfer(A,C,sC);
}
System.out.print("Factorial= "); // output
tt.Print(C,sC); // actual parameter
} // end of program body
} // end of program
Output:
Enter any number
216
Factorial=
10020433703146107793945530580431357292843659510789309313207995121373860606589847
38594586556013218928672958163164677181629207674869948050899420589135173534684617
58403997110091804762634610538654307063026030385478147013159069764729604284802083
33486967446000761264392925098038248675260009865190052218097905890960111329206361
43818911473651510763293786452440515608576000000000000000000000000000000000000000
0000000000000
Program: 3
Algorithm:
1. Takes an integer input in ‘c’.
2. Creates a double dimensional array ‘arr’ with ‘c’ rows and ‘r’ columns where c=r.
3. Fills the array with random numbers.
4. Finds the sum of all the elements of the principle diagonal and stores it in ‘sumd1’.
5. Prints the value of ‘sumd1’
6. Finds the sum of all the elements of the secondary diagonal and stores it in ‘sumd2’.
7. Prints the value of ‘sumd2’
Syntax:
//Primary Diagonal arr[i][i]
//Secondary Diagonal i+j=n-1 ie,arr[i][n-1-i]
import java.util.*; // entering util library
class diagonalMatrixClass11 // declaring class name
public static void main(String args[])
Scanner SS=new Scanner(System.in);
System.out.println("Enter Rows");
int r=SS.nextInt();
int c=r;
int sumd1=0,sumd2=0;
int arr[][]=new int[r][c];
for(int i=0;i<r;i++) // loop
for(int j=0;j<c;j++)
arr[i][j]=(int)(Math.random()*547)%11;
for(int i=0;i<r;i++) // loop
for(int j=0;j<c;j++)
System.out.print(arr[i][j]+"\t");
System.out.println();
}
for(int i=0;i<r;i++) // loop
sumd1+=arr[i][i];
System.out.println("Sum of Primary Diagonal "+sumd1);
for(int i=0;i<r;i++) // loop
for(int j=0;j<c;j++)
if(i+j==r-1)
sumd2+=arr[i][r-1-i];
System.out.println("Sum of Secondary Diagonal "+sumd2);
} // end of program body
} // end of program
Output:
3 4 7 4
5 7 6 0
5 0 8 6
7 1 1 9
Sum of Primary Diagonal 27
Sum of Secondary Diagonal 17
Program: 4
Algorithm:
1. Takes two string inputs in ‘String1’ and ‘String2’.
2. Takes an integer input ’n’.
3. Converts the numbers stored in string into integer arrays.
4. Divides the two numbers till ‘n’ decimal places.
5. Prints the quotient till ‘n’ decimal places.
Syntax:
import java.util.*; // entering util library
class Division // declaring class name
static Scanner ss=new Scanner ( System.in );
static String Divisor="",Dividend="";
static int DIVISOR[],DIVIDEND[],Dummy1[],Dummy2[],Dummy3[];
static int N=0,l=0,d1=0,d2=0;
Division ( String S1, String S2, int n ) // formal parameter
Divisor=S1;
Dividend=S2;
N=n;
d1=Divisor.length();
d2=Dividend.length();
DIVISOR=new int[S1.length()];
DIVIDEND=new int[S2.length()+n];
Dummy1=new int[S1.length()];
Dummy2=new int[S1.length()];
Dummy3=new int[S1.length()];
void Convert ( String S, int A[] ) // formal parameter
for ( int i=0;i<S.length();i++ )
{
A[i]=S.charAt(i)-48;
void Print ( int A[] ) // formal parameter
for ( int i=0;i<A.length;i++ )
System.out.print(A[i]);
void Multiply ( int n ) // formal parameter
for ( int i=Dummy1.length-1;i>=0;i-- )
Dummy1[i]=0;
for ( int i=DIVISOR.length-1;i>=0;i-- )
Dummy1[i]=DIVISOR[i]*n;
for ( int i=Dummy1.length-1;i>0;i-- )
Dummy1[i-1]+=Dummy1[i]/10;
Dummy1[i]=Dummy1[i]%10;
boolean Compare ( int A[], int B[] ) // formal parameter
boolean t=true;
for ( int i=0;i<A.length;i++ )
{
if ( A[i]<B[i] )
t=false;
break;
else if ( A[i]>B[i] )
t=true;
break;
return t;
void Divide () // formal parameter
for ( int k=0;l!=DIVIDEND.length-1;k++ )
int j=0;
for ( int i=9;i>=0;i-- )
Multiply(i);
if ( Compare(Dummy2,Dummy1)==true )
j=i;
break;
if (d1==d2 && k==1)
System.out.print(".");
}
if ( d2>d1 && k==d2-d1+1 )
System.out.print(".");
if ( d1>d2 && k==2 )
System.out.print(".");
if (j!=0)
System.out.print(j);
Subtract();
Copy(l);
Shift(Dummy2,++l);
else
System.out.print("O");
Shift(Dummy2,++l);
if ( d1>d2 )
System.out.print("X ((10)^"+(d2-d1-1)+")");
void Subtract () // formal parameter
for ( int i=Dummy2.length-1;i>0;i--)
{
if ( Dummy2[i]>=Dummy1[i] )
Dummy3[i]=Dummy2[i]-Dummy1[i];
else
Dummy2[i-1]=Dummy2[i-1]-1;
Dummy3[i]=Dummy2[i]+10-Dummy1[i];
void Copy(int l) // formal parameter
for ( int i=0;i<Dummy1.length;i++ )
Dummy2[i]=0;
for ( int i=0;i<Dummy1.length;i++ )
Dummy2[i]=Dummy3[i];
void Shift (int A[], int l) // formal parameter
for ( int i=1;i<A.length;i++ )
A[i-1]=A[i];
Dummy2[Dummy2.length-1]=DIVIDEND[l];
}
public static void main ( String args [] )
System.out.println(" Enter Divisor ");
String S1="0"+ss.nextLine();
System.out.println(" Enter Dividend ");
String S2="0"+ss.nextLine();
System.out.println(" Enter Number ");
int n=ss.nextInt();
Division tt=new Division(S1,S2,n);
tt.Convert(Divisor,DIVISOR);
tt.Convert(Dividend,DIVIDEND);
for (int i=0;i<Dummy1.length;i++ )
Dummy1[i]=DIVISOR[i];
Dummy2[i]=DIVIDEND[i];
l=i;
tt.Divide();
} // end of program body
} // end of program
Output:
Enter Divisor
123241232341234321
Enter Dividend
786787678789789789879768968796798967876585795
Enter Number
100
6384126999O818214813879497O9.49469O95842O61748213O5O451398OO9116611694196153432
727O92O382518698776284316544897OO6O6736O6713O2533
Program: 5
Algorithm:
1. The program takes input of as an integer ‘lim’.
2. Finds the terms of the Fibonacci series till ‘lim’ terms using recursion.
3. Prints the series.
Syntax:
import java.util.*; // entering util library
class recursionfibonacci // declaring class name
void print(int a,int b, int limit) // formal parameter
if(limit==0)
return;
System.out.print(a+b+" ");
print(b,a+b,limit-1); // actual parameter
public static void main(String args[])
recursionfibonacci ob=new recursionfibonacci();
Scanner SS=new Scanner(System.in);
System.out.print("Enter limit");
int lim=SS.nextInt();
ob.print(0,1,lim); // actual parameter
} // end of program body
} // end of program
Output:
Enter limit20
1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
Program: 6
Algorithm:
1. The program takes input as a string.
2. Converts the hexadecimal number to decimal number system.
3. Prints the decimal equivalent.
Syntax:
import java.util.*; // entering util library
class Hexadecimal_to_Decimal // declaring class name
public static void main ( String args [] )
{ // start of program body
Scanner ss=new Scanner (System.in);
Scanner tt=new Scanner (System.in);
String n="";
int t=1,s=0;
System.out.println(" Note: Enter the alphabets in BLOCK LETTERS ");
System.out.println(" Enter the number of base 16 ");
n=tt.nextLine();
for( int i=(n.length()-1);i>=0;i-- ) // loop
if ( ((int)n.charAt(i))<=57 && ((int)n.charAt(i))>=48 )
s=s+((int)n.charAt(i)-48)*t;
else if ( ((int)n.charAt(i))<=70 && ((int)n.charAt(i))>=65 )
s=s+((int)n.charAt(i)-55)*t;
else
System.out.println(" Wrong input ");
break;
}
t=t*16;
System.out.println(s);
} // end of program body
} // end of program
Output:
Enter the number of base 16
3A46
14918
Program: 7
Algorithm:
1. The program takes input as a double.
2. Converts the decimal number to octal number system.
3. Prints the octal equivalent.
Syntax:
import java.util.*; // entering util library
class Decimal_to_Octal // declaring class name
public static void main ( String args [] )
Scanner ss=new Scanner (System.in);
double n=0,nad=0,l=0;
long nbd=0,g=1,s=0,t=0;
System.out.println(" Enter the number of base 10 ");
n=ss.nextDouble();
nbd=(long)n;
nad=n-(double)nbd;
for(long i=nbd;i!=0;i/=8 ) // loop
s=i%8;
t=(s*g)+t;
g=g*10;
System.out.print(t+".");
l=nad;
for(long i=1;i<=10;i++ ) // loop
l=l*8;
System.out.print((long)l);
l=l-(long)l;
}
} // end of program body
} // end of program
Output:
Enter the number of base 10
34567
103407.0000000000
Program: 8
Algorithm:
1. Takes two string inputs ‘n1’ and ‘n2’,
2. Creates two arrays ‘A1’ and ’A2’.
3. Copies the digits of ‘n1’ in array ’A1’ and ‘n2’ in array ‘A2’.
4. Multiplies the numbers in arrays.
5. Stores the answer in ‘A3’
6. Prints the answer.
Syntax:
import java.util.*; // entering util library
class Multiplication // declaring class name
void Extract ( String n, int A[] ) // formal parameter
int s=n.length()-1;;
for ( int j=A.length-1;j>=0;j-- )
A[j]=((int)n.charAt(s))-48;
s--;
void Multiply_1( int A[], int B[], int C[], int l,int r ) // formal parameter
int d=0;
for ( int k=0;k<l;k++ )
if ( (k+1)%2==0 )
int s=0;
for ( int m=0;m<=k;m++ )
d=A[m]*B[k-m];
s=s+d;
}
C[k+1]=s;
r--;
else
int s=0;
for ( int m=0;m<=k;m++ )
if ( m!=(k/2))
d=A[m]*B[k-m];
s=s+d;
else
s=s+(A[k/2]*B[k/2]);
C[k+1]=s;
void Multiply_2( int A[], int B[], int C[], int l,int r ) // formal parameter
int f=0,d=0,g=C.length-1;
for ( int k=l-1;k>=1;k-- )
int s=0;
f=k;
for ( int m=l-1;m>=k;m-- )
{
d=A[m]*B[f];
s=s+d;
f++;
C[g]=s;
g--;
void Addition ( int C[] ) // formal parameter
int p=0,q=0;
for ( int o=C.length-1;o>0;o-- )
p=C[o]/10;
q=C[o]%10;
C[o]=q;
C[o-1]=C[o-1]+p;
public static void main ( String args [] )
Scanner ss=new Scanner ( System.in );
Scanner uu=new Scanner ( System.in );
Multiplication tt=new Multiplication();
int l=0,r=0;
String n1="",n2="";
System.out.println("Enter two numbers");
n1=uu.nextLine();
n2=uu.nextLine();
l=Math.max(n1.length(),n2.length());
int A1[]=new int[l];
int A2[]=new int[l];
int A3[]=new int[l*2];
tt.Extract(n1,A1); // actual parameter
tt.Extract(n2,A2);// actual parameter
tt.Multiply_2(A1,A2,A3,l,r); // actual parameter
tt.Multiply_1(A1,A2,A3,l,r);// actual parameter
tt.Addition(A3); // actual parameter
System.out.print("Product= ");
for ( int k=0;k<A3.length;k++ )
System.out.print(A3[k]);
} // end of program
Output:
Enter two numbers
1234567890
0987654321
Product= 01219326311126352690
Program: 9
Algorithm:
1. Takes two string inputs ‘Number_1’ and ‘Number_2’,
2. Creates two arrays ‘Number_1’ and ’ Number_2’.
3. Copies the digits of ‘Number_1’ in array ’ Number_1’ and ‘Number_2’ in array ‘Number_2’.
4. Adds the numbers in arrays.
5. Stores the answer in ‘S’
6. Prints the answer.
Syntax:
import java.util.*; // entering util library
class Addition // declaring class name
void Extract ( String S, int A[], int n ) // formal parameter
int c=n-1;
for ( int i=S.length()-1;i>=0;i-- )
A[c]=((int)(S.charAt(i)))-48;
c--;
void Add ( int A1[], int A2[], int S[] )// formal parameter
for ( int i=A1.length-1;i>=0;i-- )
S[i+1]=A1[i]+A2[i];
for ( int i=S.length-1;i>0;i-- )
S[i-1]=S[i-1]+(S[i]/10);
S[i]=S[i]%10;
}
void Print ( int A [] ) // formal parameter
for ( int i=0;i<A.length;i++ )
System.out.print(A[i]);
public static void main ( String args [] )
Scanner rr=new Scanner ( System.in );
Scanner ss=new Scanner ( System.in );
Addition tt=new Addition();
String NUMBER_1="",NUMBER_2="";
int x=0;
System.out.println(" Enter the FIRST NUMBER");
NUMBER_1=ss.nextLine();
System.out.println(" Enter the SECOND NUMBER ");
NUMBER_2=ss.nextLine();
x=Math.max(NUMBER_1.length(),NUMBER_2.length());
int Number_1[]=new int[x];
int Number_2[]=new int[x];
int Sum[]=new int[x+1];
tt.Extract(NUMBER_1,Number_1,x);
tt.Extract(NUMBER_2,Number_2,x);
tt.Add(Number_1,Number_2,Sum);
tt.Print(Sum);
} // end of program body
} // end of program
Output:
Enter the FIRST NUMBER
324124124132421424242344123
Enter the SECOND NUMBER
56756786856776677685678657867545
056757110980900810107102900211668
Program: 10
Algorithm:
1. Inputs the number of discs ‘n’.
2. Finds the solution of Hanoi’s tower with ‘n’ discs and 3 rods.
3. Prints the solution.
Syntax:
import java.util.*; // entering util package
class Hanoi_1 // declaring class name
int Top_N ( int A[][], int n, int T ) // formal parameter
int g=0;
for ( int i=0;i<n;i++ )
if ( A[i][T]!=0 )
g=A[i][T];
break;
return g;
int Top_R ( int A[][], int n, int T ) // formal parameter
int g=n-1;
for ( int i=0;i<n;i++ )
if ( A[i][T]!=0 )
g=i;
break;
}
}
return g;
void Print ( int A[][] , int n ) // formal parameter
for ( int i=0;i<n;i++ )
for ( int j=0;j<3;j++ )
System.out.print(A[i][j]);
System.out.println();
public static void main ( String args [] )
Scanner ss=new Scanner ( System.in );
Hanoi_1 tt=new Hanoi_1();
int n=0,Final=0,Initial=0,D=0,T1=0,T2=0,T3=0;
System.out.println(" Enter the number of discs ");
n=ss.nextInt();
int A[][]=new int[n][3];
for ( int i=0;i<n;i++ )
A[i][0]=i+1;
tt.Print(A,n);
for ( int i=1;i<((int)(Math.pow(2,n)));i++ )
if ( i%2==1 ) // condition
{
for ( int j=0;j<3;j++ )
if ( tt.Top_N(A,n,j)==1 && j!=2 ) // condition
Initial=tt.Top_R(A,n,j);
Final=tt.Top_R(A,n,j+1);
if ( Final!=n-1 ) // condition
Final=Final-1;
else if ( Final==n-1 && tt.Top_N(A,n,j+1)!=0 ) // condition
Final=Final-1;
A[Final][j+1]=1;
A[Initial][j]=0;
break;
else if ( tt.Top_N(A,n,j)==1 && j==2 )
Initial=tt.Top_R(A,n,2);
Final=tt.Top_R(A,n,0);
if ( Final!=(n-1) )
Final=Final-1;
else if ( Final==n-1 && tt.Top_N(A,n,0)!=0 )
Final=Final-1;
A[Final][0]=1;
A[Initial][2]=0;
break;
else // condition
for ( int j=0;j<3;j++ ) // loop
if ( tt.Top_N(A,n,j)==1 ) // condition
D=j;
break;
if ( D==0 ) // condition
T2=tt.Top_N(A,n,1);
T3=tt.Top_N(A,n,2);
if ( T2>T3 && T3!=0) // condition
Initial=tt.Top_R(A,n,2);
Final=tt.Top_R(A,n,1)-1;
A[Final][1]=T3;
A[Initial][2]=0;
else if ( T3>T2 && T2!=0 ) // condition
Initial=tt.Top_R(A,n,1);
Final=tt.Top_R(A,n,2)-1;
A[Final][2]=T2;
A[Initial][1]=0;
else if ( T2>T3 && T3==0 ) // condition
Initial=tt.Top_R(A,n,1);
Final=tt.Top_R(A,n,2);
A[Final][2]=T2;
A[Initial][1]=0;
else if ( T3>T2 && T2==0 ) // condition
Initial=tt.Top_R(A,n,2);
Final=tt.Top_R(A,n,1);
A[Final][1]=T3;
A[Initial][2]=0;
if ( D==1 ) // condition
T1=tt.Top_N(A,n,0);
T3=tt.Top_N(A,n,2);
if ( T1>T3 && T3!=0 ) // condition
Initial=tt.Top_R(A,n,2);
Final=tt.Top_R(A,n,0)-1;
A[Final][0]=T3;
A[Initial][2]=0;
else if ( T3>T1 && T1!=0 ) // condition
Initial=tt.Top_R(A,n,0);
Final=tt.Top_R(A,n,2)-1;
A[Final][2]=T1;
A[Initial][0]=0;
else if ( T1>T3 && T3==0 ) // condition
Initial=tt.Top_R(A,n,0);
Final=tt.Top_R(A,n,2);
A[Final][2]=T1;
A[Initial][0]=0;
else if ( T3>T1 && T1==0 ) // condition
Initial=tt.Top_R(A,n,2);
Final=tt.Top_R(A,n,0);
A[Final][0]=T3;
A[Initial][2]=0;
if ( D==2 ) // condition
T2=tt.Top_N(A,n,1);
T1=tt.Top_N(A,n,0);
if ( T2>T1 && T1!=0 ) // condition
Initial=tt.Top_R(A,n,0);
Final=tt.Top_R(A,n,1)-1;
A[Final][1]=T1;
A[Initial][0]=0;
else if ( T1>T2 && T2!=0 ) // condition
{
Initial=tt.Top_R(A,n,1);
Final=tt.Top_R(A,n,0)-1;
A[Final][0]=T2;
A[Initial][1]=0;
else if ( T2>T1 && T1==0 ) // condition
Initial=tt.Top_R(A,n,1);
Final=tt.Top_R(A,n,0);
A[Final][0]=T2;
A[Initial][1]=0;
else if ( T1>T2 && T2==0 ) // condition
Initial=tt.Top_R(A,n,0);
Final=tt.Top_R(A,n,1);
A[Final][1]=T1;
A[Initial][0]=0;
System.out.println();
tt.Print(A,n); // actual parameter
} // end of program body
} // end of program
Output:
Enter the number of discs
100
200
300
000
200
310
000
000
312
000
001
302
000
001
032
000
000
132
000
020
130
010
020
030
Program: 11
Algorithm:
1. Takes an integer input in ‘n’.
2. Converts it into binary equivalent.
3. Prints the binary equivalent.
Syntax:
import java.util.*; // entering util library
class DectoBin // declaring class name
public static void main(String arg[])
Scanner sc=new Scanner(System.in);
System.out.println("Enter a decimal number");
int n=sc.nextInt();
int bin[]=new int[100];
int i = 0;
while(n > 0) // loop
bin[i++] = n%2;
n = n/2;
System.out.print("Binary number is : ");
for(int j = i-1;j >= 0;j--) // loop
System.out.print(bin[j]);
} // end of program body
} // end of program
Output:
Enter a decimal number
456
Binary number is : 111001000
Program: 12
Algorithm:
1. Takes two string inputs ‘N’ and ‘P’,
2. Creates two arrays ‘d1’ and ’d2’ with size ‘9999’.
3. Copies the digits of ‘N’ in array ’n’ and ‘P’ in array ‘p’.
4. Multiplies the numbers in arrays till ‘p’ becomes same as P’
5. Stores the answer in ‘d2’
6. Prints the answer.
Syntax:
import java.util.*; //entering util library
class Power // declaring class name
static int d1[],d3[],d2[],n[],p[];
Power (String N, String P) // formal parameter
d1=new int[P.length()];
d1[P.length()-1]=1;
d2=new int[9999];
d3=new int[9999];
n=new int [N.length()];
p=new int [P.length()];
for ( int i=0;i<P.length();i++ )
p[i]=P.charAt(i)-48;
for ( int i=0;i<N.length();i++ )
n[i]=N.charAt(i)-48;
int j=d2.length-1;
for ( int i=N.length()-1;i>=0;i-- )
d2[j--]=n[i];
}
void Multiply () // formal parameter
int k=0;
for ( int i=n.length-1;i>=0;i-- )
for ( int j=d2.length-1;j>First(d2)-10;j-- )
d3[j-k]=d3[j-k]+(d2[j]*n[i]);
for ( int j=d2.length-1;j>First(d2)-10;j-- )
d3[j-1]=d3[j-1]+(d3[j]/10);
d3[j]=d3[j]%10;
k++;
Copy();
int First (int A[]) // formal parameter
int k=0;
for ( int i=0;i<A.length;i++ )
if (A[i]!=0 )
k=i;
break;
}
return k;
void Increase (int d1[]) // formal parameter
d1[d1.length-1]=d1[d1.length-1]+1;
for ( int i=d1.length-1;i>0;i-- )
d1[i-1]=d1[i-1]+(d1[i]/10);
d1[i]=d1[i]%10;
void Print ( int A[] ) // formal parameter
for ( int i=First(A);i<A.length;i++ )
System.out.print(A[i]);
System.out.println();
void Copy () // formal parameter
for ( int i=0;i<d2.length;i++ )
d2[i]=d3[i];
d3[i]=0;
boolean Check () // formal parameter
boolean c=true;
for ( int i=0;i<d1.length;i++ )
{
if (d1[i]!=p[i])
c=false;
break;
return c;
public static void main ( String args [] )
Scanner ss=new Scanner ( System.in );
System.out.println(" Enter number ");
String N=ss.nextLine();
System.out.println(" Enter power ");
String P=ss.nextLine();
Power tt=new Power(N,P);
for ( int i=0;tt.Check()!=true;i++ )
tt.Multiply();
tt.Increase(d1);
tt.Print(d2);
} // end of program body
} // end of program
Output:
Enter number
32342
Enter power
27
58037081498405924025078784795569516217791289658780460584566572231628025915864210
627748637715074300302192921605384802664448
Program: 13
Algorithm:
1. Takes a string input in ‘str’.
2. Extracts each letter from the word.
3. If the letter is in Block letters it converts it into small letters and vice-versa.
4. And adds the next alphabet in the format to the ‘newstr’.
5. Prints the toggle stored in ‘newstr’.
Syntax:
import java.util.*; // entering util library
class Toggle // declaring class name
static Scanner ss=new Scanner ( System.in );
static Toggle tt=new Toggle();
String str,newstr;
int len;
Toggle () // formal parameter
str="";
newstr="";
len=0;
void readword() // formal parameter
System.out.println("Enter any word");
str=ss.nextLine();
len=str.length();
void toggle () // formal parameter
for ( int i=0;i<len;i++ )
char ch=str.charAt(i);
if ( (int)ch<=56 && (int)ch>=48 )
newstr=newstr+((char)(((int)ch)+1));
else if ( ch=='0' )
ch='1';
newstr=newstr+ch;
else if ( ch=='z' )
ch='A';
newstr=newstr+ch;
else if ( ch=='Z' )
ch='a';
newstr=newstr+ch;
else if ( (int)ch<=121 && (int)ch>=97 )
newstr=newstr+((char)(((int)ch)-31));
else if ( (int)ch<=89 && (int)ch>=65 )
newstr=newstr+((char)(((int)ch)+33));
void display() // formal parameter
{
System.out.println("Original Word="+str);
System.out.println("Toggled Word="+newstr);
public static void main ( String args [] )
tt.readword();
tt.toggle();
tt.display();
} // end of program body
} // end of program
Output:
Enter any word
Pneumonoultramicroscopicsilicovolcanoconiosis
Original Word=Pneumonoultramicroscopicsilicovolcanoconiosis
Toggled Word=qOFVNPOPVMUSBNJDSPTDPQJDTJMJDPWPMDBOPDPOJPTJT
Program: 14
Algorithm:
1. The program takes input as a String ‘n’.
2. Extracts each character from ‘n’, converts it into an integer and checks whether it lies
between ‘0’ and ‘9’ and ‘A’ and ‘F’ or not.
3. If the character lies in the range, it extracts the binary equivalent of the integer from the
array ’A’ and keeps on adding it in string ‘s’.
4. Prints the string ’s’
5. If the character does NOT lie in the range, prints ‘INVALID INPUT’.
Syntax:
import java.util.*; // entering util library
class Hexadecimal_to_Binary // declaring class name
public static void main ( String args [] )
Scanner ss=new Scanner (System.in);
Scanner tt=new Scanner (System.in);
String n="",s="";
String
A[]={"0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","110
1","1110","1111"};
System.out.println(" Note: Enter the alphabets in BLOCK LETTERS ");
System.out.println(" Enter the number of base 16 ");
n=tt.nextLine();
for ( int i=0;i<n.length();i++ ) // loop
if ( ((int)n.charAt(i))<=57 && ((int)n.charAt(i))>=48 )
s=s+A[((int)n.charAt(i))-49];
else if ( ((int)n.charAt(i))<=70 && ((int)n.charAt(i))>=65 )
s=s+A[((int)n.charAt(i))-56];
}
else
System.out.println(" Wrong input ");
break;
System.out.println(s);
} // end of program body
} // end of program
Output:
Enter the number of base 16
34AE5
00110100101011100101
Program: 15
Algorithm:
1. The program takes input as a String ‘n’.
2. Extracts each character from ‘n’, converts it into an integer and checks whether it lies
between ‘0’ and ‘7’ or not.
3. If the character lies in the range, it extracts the binary equivalent of the integer from the
array ’A’ and keeps on adding it in string ‘s’.
4. Prints the string ’s’
5. If the character does NOT lie in the range, prints ‘INVALID INPUT’.
Syntax:
import java.util.*; // entering util library
class Octal_to_Binary // declaring class name
public static void main ( String args [] )
Scanner ss=new Scanner (System.in);
Scanner tt=new Scanner (System.in);
String n="",s="";
String A[]={"001","010","011","100","101","110","111"};
System.out.println(" Note: Enter the alphabets in BLOCK LETTERS ");
System.out.println(" Enter the number of base 8 ");
n=tt.nextLine();
for ( int i=0;i<n.length();i++ )
if ( ((int)n.charAt(i))<=55 && ((int)n.charAt(i))>=48 )
s=s+A[((int)n.charAt(i))-49];
else
System.out.println(" Wrong input ");
break;
}
}
System.out.println(s);
} // end of program body
} // end of program
Output:
Enter the number of base 8
1234321
001010011100011010001
Program: 16
Algorithm:
1. The program takes input as an integer ‘n’.
2. Creates an array ‘arr’ with size ‘n’.
3. Inputs ‘n’ elements in array ‘arr’.
4. Arranges the elements in ascending order using selection sort.
5. Prints the array ‘arr’.
Syntax:
import java.util.*; // entering util library
class insertionSort // declaring class name
public static void main(String args[])
Scanner SS=new Scanner(System.in);
System.out.println("Enter no of terms");
int n=SS.nextInt();
int arr[]=new int[n];
System.out.println("Enter terms");
for(int i=0;i<n;i++)
arr[i]=SS.nextInt();
for (int i = 1; i < n; i++)
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
arr[j+1] = arr[j];
j=j-1;
arr[j+1] = key;
for(int i=0;i<n;i++)
System.out.print(arr[i] + " ");
System.out.println();
} // end of program body
} // end of program
Output:
Enter no of terms
Enter terms
12
56
54
12
34
12 12 34 54 56
Program: 17
Algorithm:
1. Takes an integer input in ‘s’.
2. Creates an array ‘arr’ with ‘s’ elements.
3. Fills the array ‘arr’ with ‘s’ random integers.
4. Finds the least element in the unsorted part of ’arr’ and then places it in ascending order in
the sorted array.
5. Prints the array ‘arr’.
6. Repeats the steps (4) and (5) until the array is completely sorted.
Syntax:
import java.util.*; // entering util library
class selectionSort // declaring class name
int arr[],size;
selectionSort(int s) // formal parameter
size=s;
arr=new int[s];
void accept() // formal parameter
int i;
for(i=0;i<size;i++)
arr[i]=(int)(Math.random()*5454)%51;
void sort() // formal parameter
int i,p,t;
for(i=0;i<size;i++)
p=indexOfMin(i);
t=arr[i];
arr[i]=arr[p];
arr[p]=t;
for(int i1=0;i1<size;i1++)
System.out.print(arr[i1] + " ");
System.out.println();
int indexOfMin(int j) // formal parameter
int p=j;
for(;j<size;j++)
if(arr[j]<arr[p])
p=j;
return p;
void display() // formal parameter
int i;
for(i=0;i<size;i++)
System.out.print(arr[i] + " ");
System.out.println();
public static void main(String ar[])
System.out.println("Enter Size");
Scanner sc=new Scanner(System.in);
int s=sc.nextInt();
selectionSort ob=new selectionSort(s);
ob.accept();
ob.display();
ob.sort();
ob.display();
} // end of program body
} // end of program
Output:
Enter Size
14 6 4 47 13 21
4 6 14 47 13 21
4 6 14 47 13 21
4 6 13 47 14 21
4 6 13 14 47 21
4 6 13 14 21 47
4 6 13 14 21 47
4 6 13 14 21 47
Program: 18
Algorithm:
1. Inputs a sentence in string ’str’.
2. Inputs a word in string ’sub’.
3. Counts the frequency of substring ‘sub’ int the string ‘str’ and prints it.
Syntax:
import java.util.*; // entering util library
class frequencyofsubstring // declaring class name
int freq(String str,String sub) // formal parameter
if(str.length()<sub.length())
return 0;
else if(str.substring(0,sub.length()).equals(sub))
return 1+freq(str.substring(1),sub);
else
return freq(str.substring(1),sub);
public static void main(String args[])
frequencyofsubstring ob=new frequencyofsubstring();
Scanner SS=new Scanner(System.in);
System.out.println("Enter String");
String str=SS.nextLine();
System.out.println("Enter Sub String");
String sub=SS.nextLine();
System.out.println(ob.freq(str,sub)); // actual parameter
} // end of program body
} // end of program
Output:
Enter String
my boy my boy he said to the boy.
Enter Sub String
boy
3
Program: 19
Algorithm:
1. The program takes input as a integer ‘n’.
2. Creates a single dimensional double array ‘A’.
3. Inputs ‘n’ elements in ‘A’.
4. Arranges the elements of ‘A’ in ascending order.
5. Prints the arranged data.
6. Finds the sum of all elements of ‘A’, divides that by ‘n’ and prints it as mean.
7. If ‘n’ is even, prints the (‘n/2’th element+‘(n/2)+1’th element)/2 as the median.
8. If ‘n’ is odd, prints the ‘(n-1)/2’th element as the median.
9. Find the maximum occurring entity in the data and prints it as the mode.
Syntax:
import java.util.*; // entering util library
class Project_1 // declaring class name
void arrange (double A[], int n) // formal parameter
int i,j;
for ( i=0;i<n;i++ )
for ( j=0;j<n;j++ )
if (A[j]>A[j+1])
double t=A[j];
A[j]=A[j+1];
A[j+1]=t;
System.out.println(" Arranged Data : ");
for ( i=0;i<n;i++ )
System.out.print(A[i]+" ");
}
System.out.println();
void mememo( double A[], int n ) // formal parameter
arrange(A,n);
double s=0,d=0,f=0,mean=0,median=0,mode=0;
for ( int i=0;i<n;i++ )
s=s+A[i];
mean=s/n;
System.out.println("Mean= "+mean);
if ( n%2==0 )
median=((A[n/2])+A[(n-2)/2])/2;
else
median =A[(n-1)/2];
System.out.println("Median= "+ median );
for (int i=0;i<n;i++ )
d=A[i];
int c=1;
for ( int j=0;j<n;j++ )
if ( A[j]==d )
c++;
}
if (c>f)
mode=d;
f=c;
System.out.println(" Mode= "+ mode);
public static void main ( String args [] )
Scanner ss=new Scanner (System.in );
Project_1 tt=new Project_1();
int n=0;
System.out.println(" Enter the number of terms ");
n=ss.nextInt();
double A[]=new double[n];
System.out.println( " Enter " + n + "numbers ");
for ( int i=0;i<n;i ++)
A[i]=ss.nextDouble();
tt.mememo(A,n);
} // end of program body
} // end of program
Output:
Enter the number of terms
Enter 5numbers
1
2
Mean= 2.4
Median= 3
Mode = 3
Program: 20
Algorithm:
1. The program takes input as an integer ‘n’.
2. Checks whether the number lies between ‘2’ and ‘100’ or not.
3. If the number ‘n’ lies between ‘2’ and ‘100’, stores the binary equivalent of ‘n’ in ‘k’.
4. Counts the number of ‘1’s in ‘k’.
5. If the number of ‘1’s in ‘k’ is even, prints ‘EVIL NUMBER’
6. If the number of ‘1’s in ‘k’ is odd, prints ‘NOT EVIL NUMBER’
7. If the number ‘n’ does not lie between ‘2’ and ‘100’, prints ‘Number out of range’.
Syntax:
import java.util.*; // entering util library
class Evil // declaring class name
int fn(int n)
if(n==0)
return 0;
else
return n%2+fn(n/2)*10;
public static void main(String args[])
System.out.println("Input number");
Scanner SS=new Scanner(System.in);
int n=SS.nextInt();
Evil ob=new Evil();
System.out.println("INPUT: N = "+n);
if(n>2 && n<100)
int k=ob.fn(n);
System.out.println("BINARY EQUIVALENT: "+k);
int c=0;
while(k!=0)
{
int a=k%10;
if(a==1)
c++;
k/=10;
if(c%2==0)
System.out.println("NUMBER OF 1’s: "+c);
System.out.println("OUTPUT: EVIL NUMBER");
else
System.out.println("NUMBER OF 1’s: "+c);
System.out.println("OUTPUT: NOT AN EVIL NUMBER");
else
System.out.println("OUTPUT: NUMBER OUT OF RANGE");
} // end of program body
} // end of program
Output:
Input number
11
INPUT: N = 11
BINARY EQUIVALENT: 1011
NUMBER OF 1’s: 3
OUTPUT: NOT AN EVIL NUMBER
Program: 21
Algorithm:
1. Takes an integer input in ‘n’
2. Calculates the sum of factors of ’n’ (excluding ‘n’) itself using recursion.
3. If the sum of factors of ’n’ (excluding ‘n’ itself) is equal to ‘n’ prints ‘The number is a perfect
number’.
4. If the sum of factors of ’n’ (excluding ‘n’ itself) is NOT equal to ‘n’ prints ‘The number is not a
perfect number’.
Syntax:
import java.io.*;
import java.util.Scanner;
class Perfect // declaring class name
private int num;
private int i;
public Perfect(int num)
this.num = num;
i = num-1;
public int sumofFactors(int n)
if(i==0)
return 0;
else if(n%i==0)
return i-- + sumofFactors(n);
else
i--;
return sumofFactors(n);
}
}
public void check()
if(num==sumofFactors(num))
System.out.println(num + " is a Perfect Number");
else
System.out.println(num + " is not a Perfect Number");
public static void main(String args[ ])throws IOException
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number:");
int n = sc.nextInt();
Perfect obj = new Perfect(n);
obj.check();
} // end of program body
} // end of program
Output:
Enter the number:57
57 is not a Perfect Number
Program: 22
Algorithm:
1. Takes two integer inputs in ‘r’ and ‘c’.
2. Creates a double dimensional array with ’r’ rows and ‘c’ columns.
3. Fills the array with numbers from ‘1’ to ‘r*c’ in spiral order in column major.
4. Prints the array ‘arr’.
Syntax:
import java.util.*; // entering util package
class spiralMatrixClass11 // declaring class name
public static void main(String args[])
Scanner SS=new Scanner(System.in);
System.out.println("Enter rows");
int r=SS.nextInt();
System.out.println("Enter columns");
int c=SS.nextInt();
int arr[][]=new int[r][c];
int q=1;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
if(i%2==0)
arr[j][i]=q++;
else
arr[r-j-1][i]=q++;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
System.out.print(arr[i][j]+"\t");
System.out.println();
} // end of program body
} // end of program
Output:
Enter rows
Enter columns
1 6 7
2 5 8
3 4 9
Program: 23
Algorithm:
1. Takes a sting input.
2. Extracts words from the sentence using loop.
3. Checks whether the word is repeated in the sentence or not.
4. If the word is repeated it removes the word and moves to the next word.
5. Prints the sentence with non-repeating words.
Syntax:
import java.util.*; // entering util library
class NoRepeat // declaring class name
String sent;
NoRepeat(String sent) // formal parameter
this.sent=sent;
void display() // formal parameter
System.out.println("Original sentence is");
System.out.println(sent);
void process() // formal parameter
char ch;String y="";
for(int i=0;i<sent.length();i++)
ch=sent.charAt(i);
if(ch!=' ')
y=y+ch;
else
{
if(isUnique(y)==true)
System.out.println(y);
y="";
boolean isUnique(String word) // formal parameter
for(int i=0;i<word.length();i++)
for(int j=i+1;j<word.length();j++)
if(word.charAt(i)==word.charAt(j))
return false;
return true;
public static void main(String args[])
Scanner SS=new Scanner(System.in);
System.out.println("Enter sentence");
String str=SS.nextLine()+" ";
NoRepeat ob=new NoRepeat(str);
ob.display();
ob.process();
} // end of program body
} // end of program
Output:
Enter sentence
i am a good good boy
Original sentence is
i am a good good boy
am
boy
Program: 24
Algorithm:
1. Takes an integer input ‘n’.
2. Creates two objects ‘Common1’ and ‘Common2’.
3. Inputs the elements in both the arrays and prints them.
4. Creates a new object ‘Common3’.
5. Finds the common elements in ‘Common1’ and ‘Common2’ and stores them in ‘Cpmmon3’.
6. Prints ‘Common3’
Syntax:
import java.util.*; // entering util library
class Common // declaring class name
Scanner SS=new Scanner(System.in);
int ar[],size;
int c=0;
Common(int N)
size=N;
ar=new int[size];
void readElements()
System.out.println("Enter Array elements");
for(int i=0;i<size;i++)
ar[i]=SS.nextInt();
void display()
System.out.println("Array elements are");
for(int i=0;i<size;i++)
{
System.out.print(ar[i]+" ");
Common findCommon(Common obj)
Common ob4=new Common(size);
for(int i=0;i<size;i++)
for(int j=0;j<size;j++)
if(ar[i]==obj.ar[j])
ob4.ar[c++]=ar[i];
break;
return ob4;
public static void main(String args[])
Scanner Sc=new Scanner(System.in);
System.out.println("Enter size");
int n=Sc.nextInt();
Common ob1=new Common(n);
ob1.readElements();
ob1.display();
Common ob2=new Common(n);
ob2.readElements();
ob2.display();
Common ob3=new Common(n);
ob3=ob1.findCommon(ob2);
(ob1.findCommon(ob2)).display();
} // end of program body
} // end of program
Output:
Enter size
Enter Array elements
12
34
56
78
98
78
Array elements are
12 34 56 78 98 78 Enter Array elements
78
78
56
121
134
54
Array elements are
78 78 56 121 134 54 Array elements are
0 0 0 56 78 78
Program: 25
Algorithm:
1. Takes an integer input in ‘r’.
2. Creates two double dimensional square arrays ‘arr’ and ‘arn’ with ‘r’ rows and ‘r’ columns.
3. Fills array ‘arr’ with numbers from 1 to ‘r2’ in row major.
4. Prints array ‘arr’ using loop.
5. Rotates the array ‘arr’ anticlockwise 90 o and stores the new array in ‘arn’.
6. Prints array ‘arn’.
Syntax:
import java.util.*; // entering util library
class antirotatedMatrixClass11 // declaring class name
public static void main(String args[])
Scanner SS=new Scanner(System.in);
System.out.println("Enter Rows");
int r=SS.nextInt();
int c=r, k=0;
int sumd1=0,sumd2=0;
int arr[][]=new int[r][c];
int arn[][]=new int[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
arr[i][j]=++k;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
System.out.print(arr[i][j]+"\t");
System.out.println();
for(int i=0;i<c;i++)
{
for(int j=0;j<r;j++)
arn[Math.abs(j-r+1)][i]=arr[i][j];
System.out.println();System.out.println();
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
System.out.print(arn[i][j]+"\t");
System.out.println();
} // end of program body
} // end of program
Output:
Enter Rows
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
5 10 15 20 25
4 9 14 19 24
3 8 13 18 23
2 7 12 17 22
1 6 11 16 21