0% found this document useful (0 votes)
23 views100 pages

COMPUTER SCIence Project

The document is a project report by Prajjwal Sharma from Dr. Virendra Swarup 21st Century School, focusing on various Java programming projects. It includes acknowledgments, an index of programs, and detailed implementations of algorithms such as checking for Keith numbers, printing unique numbers, and generating lucky numbers. The project aims to enhance programming skills and knowledge in computer science.

Uploaded by

Amit Nigam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views100 pages

COMPUTER SCIence Project

The document is a project report by Prajjwal Sharma from Dr. Virendra Swarup 21st Century School, focusing on various Java programming projects. It includes acknowledgments, an index of programs, and detailed implementations of algorithms such as checking for Keith numbers, printing unique numbers, and generating lucky numbers. The project aims to enhance programming skills and knowledge in computer science.

Uploaded by

Amit Nigam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 100

2016-2017

Dr. VIRENDRA SWARUP 21ST


CENTURY SCHOOL

COMPUTER
SCIENCE
--------------------------
- - - - - - - - PROJECT WORK

Presented by:
PRAJJWAL SHARMA
XII ‘A’
Acknowledgement
|
I would like to express my special thanks of gratitude
to my teacher ꞋꞋMR. AMIT NIGAMꞋꞋ who gave me the
golden opportunity to do this wonderful project work
(which includes program of different types based on
Java programming language), which helped a lot in
developing my programming skills.

I also came across many new and different logics which


I was unaware of.

I am really thankful to him.

Secondly I would like to thank my parents and friends


who helped me a lot in finishing this project within
the limited time.

I have made this project not only to gain marks but to


also gain knowledge.
THANKS AGAIN TO ALL THOSE WHO HELPED ME.

-PRAJJWAL SHARMA
XII (A)

Index
Sr. Program* Pg#
1. Keith Number 4
2. Unique Numbers in a Range 6
3. Lucky Numbers 8
4. Mobius Function 10
5. Printing a matrix in circular form as(for n=3) 13
SAMPLE OUTPUT:

1
Prajjwal Sharma
1 8 7
2 9 6
3 4 5
6. Printing a matrix in circular form as(for n=3) 15
SAMPLE OUTPUT:
1 2 3
8 9 4
7 6 5
7. Printing a matrix in circular form as(for n=3) 17
SAMPLE OUTPUT:
7 6 5
8 9 4
1 2 3
8. Printing a matrix in circular form as(for n=3) 19
SAMPLE OUTPUT:
7 6 5
8 9 4
1 2 3
9. Printing a matrix in circular form as(for n=3) 21
SAMPLE OUTPUT:
3 4 5
2 9 6
1 8 7
10. Printing a matrix in circular form as(for n=3) 23
SAMPLE OUTPUT:
3 2 1
4 9 8
5 6 7
11. Printing a matrix in circular form as(for n=3) 25
SAMPLE OUTPUT:
5 4 3
6 9 2
7 8 1
12. Printing a matrix in circular form as(for n=3) 27
SAMPLE OUTPUT:
5 6 7
4 9 8
3 2 1
13. Subsets of a set. 29
14. Smith number 31
15. Multiplication of two matrices 33
16. Evil number 36
17. Fascinating number 38
18. Bouncy number 41
19. Twin Primes in a range 44
20. Print DDMMYYYY in the form of date, month name 46
and year

2
Prajjwal Sharma
21. Lower Triangular Matrix 48
22. Upper Triangular matrix 50
23. Diagonal matrix 52
24. Scalar matrix 54
25. Symmetric matrix 56
26. Creating a Magic matrix 59
27. Saddle point of a matrix 62
28. XOR and XNOR truth tables 64
29. Pendulum like rotation of elements of array 66
30. Anagrams of a word 68
31. Validation of an IMEI number 70
32. Fibonacci series using Recursion 73
33. Circular prime number 76
34. Arranging words according to their potential 79
35. Decoding an encrypted code 83
36. Binary search using recursion. 86
37. Rotate a matrix 90o clockwise 90
38. Merge arrays inside an object 92
39. Converting decimal numbers into roman numbers 95
40. Pronic numbers 96
XLI. APPLET(a moving ball) 97

*Details of each program are mentioned at its respective pages.

1. Program in Java to check for KEITH NUMBER.


#Info: A Keith number is an integer N with d digits with the following property.
If a Fibonacci-like sequence (in which each term in the sequence is the sum of the
d previous terms) is formed, with the first d terms being the decimal digits of the
number N, then N itself occurs as a term in the sequence.
Example: 197 is a Keith number since it generates the sequence 1, 9, 7, 17, 33,
55, 107, 197
import java.util.*;
class keith

3
Prajjwal Sharma
{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int c=n;
String s=Integer.toString(n);
int d=s.length();
int kt[]=new int[n];
for(int a=(d-1);a>=0;a--)
{
kt[a]=c%10;
c=c/10;
}
int j=d,sum=0;
while(sum<n)
{
sum=0;
for(int k=1;k<=d;k++)
{sum=sum+kt[j-k];}
kt[j]=sum;
j++;
}
if(sum==n)
System.out.println("Yes, you’ve entered a correct number. It is a
Keith number!");
else
System.out.println("Sour luck, better try next time!");
}

4
Prajjwal Sharma
}

2. Program in Java to print UNIQUE NUMBERS in a range


entered by the user.
#info: A number is a unique number if no digit in it is repeated.
Example: 2546 is unique but 4456 is not.
Several Unique numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15..... and so

import java.util.*;
class unique
{

5
Prajjwal Sharma
String check(String a)
{
int e=0,b=0;
b=a.length();
for(int c=0;c<b;c++)
{e=0;
for(int d=0;d<b;d++)
{
if(a.charAt(c)==a.charAt(d))
e++;
}
if(e>1)
break;
}
if(e!=1)
return "1";
else
return "2";
}
public static void main()
{
int i=0,f=0,g=0;
String x="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the range");
System.out.print("\nLower limit = ");
f=sc.nextInt();
System.out.print("\nUpper Limit = ");
g=sc.nextInt();

6
Prajjwal Sharma
unique ob=new unique();
for(int h=f;h<=g;h++)
{
x=Integer.toString(h);
if(ob.check(x)== "2")
{
System.out.println(x);
i++;
}
}
System.out.println("Frequency="+i);
}
}

3. Program in Java to print LUCKY numbers.


#Info: Consider the sequence of natural numbers: 1, 2, 3, 4 , 5, 6, 7, 8,
9,10.................. Removing every SECOND number produces: 1, 3,
5, 7, 9......... Removing every third
number produces the sequence: 1, 3, 7, 9.......
This process continues indefinitely by removing fourth, fifth ... and so on, till after a
fixed number of steps, certain natural numbers remain indefinitely. These are
import java.util.*;

7
Prajjwal Sharma
class lucky
{
public static void main()
{
int a,f=0,i=2;
Scanner sc=new Scanner(System.in);
System.out.println("enter the value");
a=sc.nextInt();
int b[]=new int[a];
for(int c=0;c<a;c++)
b[c]=c+1;
for(int d=0;d<a;d++)
{
for(int e=0;e<a;e++)
{
if(b[e]==0)
continue;
else
f++;
if(f%i==0)
b[e]=0;
}
i++;f=0;
}
for(int j=0;j<a;j++)
{
if(b[j]==0)
continue;
else

8
Prajjwal Sharma
System.out.print(b[j]+",");
}}}

4. Design a class MobiusFn and implement the following:


#info: class: MobiusFn
Data members: int n
Member functions:
MobiusFn( ):default constructor

9
Prajjwal Sharma
void input():input value of n
int primeFac(): to check and count prime factors of n
void display(): to find and print Mobius of a function

import java.util.*;
class MobiusFn
{
int n;
MobiusFn()
{
n = 0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
n = sc.nextInt();
}
/* The function primefac() either returns '0' if prime factors are
repeated
* or returns the no.of prime factors */
int primeFac()
{
int a=n, i=2, m=0, c=0, f=0;
while(a > 1) // loop to generate prime factors
{
c = 0; // variable to store frequency of every prime factor
while(a%i == 0) // if 'i' is a prime factor
{
c++; // counting frequency of 'i'

1
Prajjwal Sharma 0
f++; // counting no of prime factors
a=a/i;
}
i++;
if(c > 1) // returning '0' if prime factors are repeated
return 0;
}
return f; // returning no. of prime factors
}
void display() // function to display value of mobius function
{
int mob,x;
if(n == 1) // condition 1
mob = 1;
else
{
x = primeFac();
if(x == 0) // condition 2
mob = 0;
else // condition 3
mob = (int)Math.pow(-1,x);
}
System.out.println("Value of Mobius Function : "+mob);
}
public static void main(String args[])
{
MobiusFn ob = new MobiusFn();
ob.input();
ob.display();

1
Prajjwal Sharma 1
}
}

5. Program in Java to take an input as integer from


the user and print the matrix in the following format:
import java.util.*; SAMPLE INPUT: SAMPLE
class spiral01 OUTPUT:
int n=3; 1 8 7
2 9 6
1
3 4 5
Prajjwal Sharma 2
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(ꞋꞋEnter the order of matrixꞋꞋ);
int n=sc.nextInt();
int a[][]=new int[n][n];
int d=0,cnt=1;
for(int j=0;j<=(n/2);j++)
{
for(int i=d;i<(n-d);i++)
{a[i][d]=cnt;
cnt++;
}
for(int i=d+1;i<(n-d);i++)
{
a[n-d-1][i]=cnt;
cnt++;
}
for(int i=(n-d-2);i>=d;i--)
{a[i][n-d-1]=cnt;
cnt++;
}
for(int i=(n-d-2);i>=(d+1);i--)
{
a[d][i]=cnt;
cnt++;
}
d++;

1
Prajjwal Sharma 3
}
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
System.out.print (a[k][l]+ ꞋꞋ\tꞋꞋ);
}
System.out.println ();
} } }

6. Program in Java to take an input as integer from the


user and print the matrix in the following format:
import java.util.*;
SAMPLE INPUT: SAMPLE
class spiral02 OUTPUT:
int n=3; 1 2 3
8 9 4 1
Prajjwal Sharma 7 6 5 4
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(ꞋꞋEnter the order of matrixꞋꞋ);
int n=sc.nextInt();
int a[][]=new int[n][n];
int d=0,cnt=1;
for(int j=0;j<=(n/2);j++)
{
for(int i=d;i<(n-d);i++)
{a[i][d]=cnt;
cnt++;
}
for(int i=d+1;i<(n-d);i++)
{
a[n-d-1][i]=cnt;
cnt++;
}
for(int i=(n-d-2);i>=d;i--)
{a[i][n-d-1]=cnt;
cnt++;
}
for(int i=(n-d-2);i>=(d+1);i--)
{
a[d][i]=cnt;
cnt++;
}
d++;

1
Prajjwal Sharma 5
}
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
System.out.print (a[l][k]+ ꞋꞋ\tꞋꞋ);
}
System.out.println ();
} } }

7. Program in Java to take an input as integer from the


user and print the matrix in the following format:
import java.util.*; SAMPLE INPUT: SAMPLE
OUTPUT:
int n=3;
7 6 5 1
Prajjwal Sharma 8 9 4 6
1 2 3
class spiral03
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println(ꞋꞋEnter the order ꞋꞋ);
int n=sc.nextInt();
int a[][]=new int[n][n];
int d=0,cnt=1;
for(int j=0;j<=(n/2);j++)
{
for(int i=d;i<(n-d);i++)
{
a[n-d-1][i]=cnt;
cnt++;
}
for(int i=(n-d-2);i>=d;i--)
{
a[i][n-d-1]=cnt;
cnt++;}
for(int i=(n-d-2);i>=d;i--)
{a[d][i]=cnt;
cnt++;}
for(int i=(d+1);i<(n-d-1);i++)
{a[i][d]=cnt;
cnt++;
}
d++;}
for(int k=0;k<n;k++)

1
Prajjwal Sharma 7
{
for(int l=0;l<n;l++)
{
System.out.print (a[k][l]+ ꞋꞋ\tꞋꞋ);
}
System.out.println ();
} } }

8. Program in Java to take an input as integer from the


user and print the matrix in the following format:

SAMPLE INPUT: SAMPLE 1


Prajjwal Sharma OUTPUT: 8
int n=3;
7 8 1
6 9 2
5 4 3
import java.util.*;
class spiral04
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println(ꞋꞋEnter the order ꞋꞋ);
int n=sc.nextInt();
int a[][]=new int[n][n];
int d=0,cnt=1;
for(int j=0;j<=(n/2);j++)
{
for(int i=d;i<(n-d);i++)
{
a[n-d-1][i]=cnt;
cnt++;
}
for(int i=(n-d-2);i>=d;i--)
{
a[i][n-d-1]=cnt;
cnt++;}
for(int i=(n-d-2);i>=d;i++)
{a[d][i]=cnt;
cnt++;}
for(int i=(d+1);i<(n-d-1);i++)
{a[i][d]=cnt;
cnt++;
}
d++;}

1
Prajjwal Sharma 9
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
System.out.print (a[l][k]+ ꞋꞋ\tꞋꞋ);
}
System.out.println ();
} } }

2
Prajjwal Sharma 0
9. Program in Java to take an input as integer from the
user and print the matrix in the following format:
import java.util.*; SAMPLE INPUT: SAMPLE
OUTPUT:
class spiral05 int n=3;
3 4 5
{ 2 9 6
public static void main() 1 8 7

{
Scanner sc=new Scanner (System.in);
System.out.println(ꞋꞋEnter the order ꞋꞋ);
int n=sc.nextInt();
int a[][]=new int[n][n];
int d=0,cnt=1;
for(int j=0;j<=(n/2);j++)
{
for(int i=(n-d-1);i>=d;i--)
{
a[i][d]=cnt;
cnt++;
}
for(int i=(d+1);i<(n-d);i++)
{
a[d][i]=cnt;
cnt++;}
for(int i=(d+1);i<(n-d);i++)
{a[i][n-d-1]=cnt;
cnt++;}
for(int i=(n-d-2);i>=(d+1);i--)
{a[n-d-1][i]=cnt;
cnt++;
}
2
Prajjwal Sharma 1
d++;}
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
System.out.print (a[k][l]+ ꞋꞋ\tꞋꞋ);
}
System.out.println ();
} } }

2
Prajjwal Sharma 2
10.Program in Java to take an input as integer from the
user and print the matrix in the following format:
import java.util.*;
SAMPLE INPUT: SAMPLE
class spiral06 OUTPUT:
int n=3;
{ 3 2 1
4 9 8
public static void main()
5 6 7
{
Scanner sc=new Scanner (System.in);
System.out.println(ꞋꞋEnter the order ꞋꞋ);
int n=sc.nextInt();
int a[][]=new int[n][n];
int d=0,cnt=1;
for(int j=0;j<=(n/2);j++)
{
for(int i=(n-d-1);i>=d;i--)
{
a[i][d]=cnt;
cnt++;
}
for(int i=(d+1);i<(n-d);i++)
{
a[d][i]=cnt;
cnt++;}
for(int i=(d+1);i<(n-d);i++)
{a[i][n-d-1]=cnt;
cnt++;}
for(int i=(n-d-2);i>=(d+1);i--)
{a[n-d-1][i]=cnt;
cnt++;
}
2
Prajjwal Sharma 3
d++;}
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
System.out.print (a[l][k]+ ꞋꞋ\tꞋꞋ);
}
System.out.println ();

} } }

2
Prajjwal Sharma 4
11.Program in Java to take an input as integer from the
user and print the matrix in the following format:
import java.util.*;
SAMPLE INPUT: SAMPLE
class spiral07 OUTPUT:
{ int n=3;
5 4 3
public static void main() 6 9 2
7 8 1
{
Scanner sc=new Scanner (System.in);
System.out.println(ꞋꞋEnter the order ꞋꞋ);
int n=sc.nextInt();
int a[][]=new int[n][n];
int d=0,cnt=1;
for(int j=0;j<=(n/2);j++)
{
for(int i=(n-d-1);i>=d;i--)
{
a[i][n-d-1]=cnt;
cnt++;
}
for(int i=(n-d-2);i>=d;i--)
{
a[d][i]=cnt;
cnt++;}
for(int i=(d+1);i<(n-d);i++)
{a[i][d]=cnt;
cnt++;}
for(int i=(d+1);i<(n-d-1);i++)
{a[n-d-1][i]=cnt;
cnt++;
}
2
Prajjwal Sharma 5
d++;}
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
System.out.print (a[k][l]+ ꞋꞋ\tꞋꞋ);
}
System.out.println ();
} } }

2
Prajjwal Sharma 6
12. Program in Java to take an input as integer from
the user and print the matrix in the following format:
import java.util.*; SAMPLE INPUT: SAMPLE
OUTPUT:
class spiral08 int n=3;
5 6 7
{ 4 9 8
public static void main() 3 2 1

{
Scanner sc=new Scanner (System.in);
System.out.println(ꞋꞋEnter the order ꞋꞋ);
int n=sc.nextInt();
int a[][]=new int[n][n];
int d=0,cnt=1;
for(int j=0;j<=(n/2);j++)
{
for(int i=(n-d-1);i>=d;i--)
{
a[i][n-d-1]=cnt;
cnt++;
}
for(int i=(n-d-2);i>=d;i--)
{
a[d][i]=cnt;
cnt++;}
for(int i=(d+1);i<(n-d);i++)
{a[i][d]=cnt;
cnt++;}
for(int i=(d+1);i<(n-d-1);i++)
{a[n-d-1][i]=cnt;
cnt++;
}
2
Prajjwal Sharma 7
d++;}
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
System.out.print (a[l][k]+ ꞋꞋ\tꞋꞋ);
}
System.out.println ();
} } }

2
Prajjwal Sharma 8
13. Program in java to print SUBSETS of a set.

#Info: A set is defined as a well-defined collection of objects and a subset is


defined as a set that is a part of a larger set. Here we take a set as a collection

then the number of subsets defined are=23=8 which are ϕ or { } (a null set),
of whole numbers. Let a set A= {0, 1, 2},

{0}, {1}, {2}, {0,1}, {1,2}, {0,2} and {0,1,2}


import java.util.*;
class subset
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of elements in the set");
int n=sc.nextInt();
int a[]=new int [n];
System.out.println("Enter the elements of the set");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int b[]=new int[n];
System.out.println("Subsets are");
for(int r=0;r<(Math.pow(2,n));r++)
{
for(int i=0;i<n;i++)
{
b[i]=0;
}
int x=r;
int p=n-1;
while(x>0)
{
int y=x%2;

2
Prajjwal Sharma 9
if(p>=0)
{b[p]=y;
}
x=x/2;
p--;
}
System.out.print("{");
for(int q=0;q<n;q++)
{if(b[q]!=0)
System.out.print(a[q]+",");
}
System.out.print("}");
System.out.println();
} } }

3
Prajjwal Sharma 0
14. Program to check for SMITH number.

#Info: A Smith number is a composite number, the sum of whose digits is the
sum of digits its prime factors obtained as a result of prime factorization
(excluding 1). Example:
666 Prime factors are 2,3,3 and 37.
Sum of digits are (6+6+6)=18.
Sum of digits of the factors (2+3+3+(3+7))=18

import java.util.*;
class Smith
{
int sumDig(int n)
{
int s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
return s;
}
int sumPrimeFact(int n)
{
int i=2,sum=0;
while(n>1)
{
if(n%i==0)
{
sum=sum+sumDig(n);
n=n/i;
}

3
Prajjwal Sharma 1
else
i++;
}
return sum;
}
public static void main()
{Scanner sc=new Scanner(System.in);
Smith ob=new Smith();
System.out.println("Enter a number");
int n=sc.nextInt();
int a=ob.sumDig(n);
int b=ob.sumPrimeFact(n);
if(a==b)
System.out.println("It is a Smith number");
else
System.out.println("It is not a Smith number");
}}

3
Prajjwal Sharma 2
15. Program to multiply two matrices

[ ] [ ]
1 2 2 1 2 2
#Info: Let there be two matrices A= 2 0 1 and B= 2 2 1 ,then their
2 3 0 2 3 0
product is given as shown in the mechanism:

[ ][ ]
1x1+2x2+2x2 1x2+2x2+2x3 1x2+2x1+2x0 9 12 4
AB= 2x1+0x2+1x2 2x2+0x2+1x3 2x2+0x1+1x0 = 4 7 4
2x1+3x2+0x2 2x2+3x2+0x3 2x2+3x1+0x0 8 10 7

import java.util.*;
class Matrix_multiply
{
public static void main()
{
int r1,r2,c1,c2,sum=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and columns for first
matrix");
r1=sc.nextInt();
c1=sc.nextInt();
int first[][]=new int[r1][c1];
System.out.println("Enter the number of rows and columns in second
matrix");
r2=sc.nextInt();
c2=sc.nextInt();
int second[][]=new int [r2][c2];
if(c1!=r2)
System.out.println("Matrices cannot be multiplied");
else
{
int multiply[][]=new int [r1][c2];
3
Prajjwal Sharma 3
System.out.println("Enter the elements of first matrix");
for(int i=0;i<r1;i++)
{ for(int j=0;j<c1;j++)
{ first[i][j]=sc.nextInt();
} }
System.out.println("Enter the elements of second matrix");
for(int i=0;i<r2;i++)
{ for(int j=0;j<c2;j++)
{ second[i][j]=sc.nextInt();
} }
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
for(int k=0;k<r2;k++)
{
sum=sum+first[i][k]*second[k][j];
}
multiply[i][j]=sum;
sum=0;
} }
System.out.println("FIRST MATRIX IS");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
System.out.print(first[i][j]+" ");
}System.out.println();
}

3
Prajjwal Sharma 4
System.out.println("SECOND MATRIX IS");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
System.out.print(second[i][j]+" ");
}System.out.println();
}
System.out.println("PRODUCT OF MATRICES IS");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
System.out.print(multiply[i][j]+" ");
}System.out.println();
}
}
}

3
Prajjwal Sharma 5
16. Program in Java to check whether a number entered
is an evil number or not.
#Info: 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...

import java.util.*;
class EvilNumber
{
String toBinary(int n) // Function to convert a number to Binary
{
int r;
String s=""; //variable for storing the result
char dig[]={'0','1'}; //array storing the digits (as characters) in a
binary number system
while(n>0)
{

3
Prajjwal Sharma 6
r=n%2; //finding remainder by dividing the number by 2
s=dig[r]+s; //adding the remainder to the result and reversing at the
same time
n=n/2;
}
return s;
}
int countOne(String s) // Function to count no of 1's in binary number
{
int c = 0, l = s.length();
char ch;
for(int i=0; i<l; i++)
{
ch=s.charAt(i);
if(ch=='1')
{
c++;
}
}
return c;
}
public static void main(String args[])
{
EvilNumber ob = new EvilNumber();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a positive number : ");
int n = sc.nextInt();
String bin = ob.toBinary(n);
System.out.println("Binary Equivalent = "+bin);
int x = ob.countOne(bin);

3
Prajjwal Sharma 7
System.out.println("Number of Ones = "+x);
if(x%2==0)
System.out.println(n+" is an Evil Number.");
else
System.out.println(n+" is Not an Evil Number.");
}}

17. Program to check for Fascinating number.

#Info: 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: Let us take 192,
192x1=192
192x2=384
192x3=576
Concatenating the results: 192384576. It contains all the digits from 1 to 9.
Hence 192 is a fascinating number.

import java.util.*;
class FascinatingNumber
{

3
Prajjwal Sharma 8
boolean isUnique(String q)
{
int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit
from '0' to '9'
int i, flag = 0;
char ch;
for(i=0; i<q.length(); i++)
{
ch = q.charAt(i);
A[ch-48]++;
/* increasing A[5] if ch='5' as '5'-48 = 53-48=5
* (ASCII values of '0' to '9' are 48 to 57) */
}
for(i=1; i<10; i++)
{
//checking if every digit from '1' to '9' are present exactly once or
not
if(A[i]!=1)
{
flag = 1; //flag is set to 1 if frequency is not 1
break;
}
}
if(flag == 1)
return false;
else
return true;
}
public static void main(String args[])
{

3
Prajjwal Sharma 9
Scanner sc = new Scanner(System.in);
FascinatingNumber ob = new FascinatingNumber();
System.out.print("Enter a number : ");
int n = sc.nextInt();
String p = Integer.toString(n); //converting the number to String
if(p.length()<3)
System.out.println("Number should be of atleast 3 digits.");
else
{
String s = Integer.toString(n*1) + Integer.toString(n*2) +
Integer.toString(n*3);
/* Joining the first, second and third multiple of the number
* by converting them to Strings and concatenating them*/
if(ob.isUnique(s))
System.out.println(n+" is a Fascinating Number.");
else
System.out.println(n+" is not a Fascinating Number.");
}
}
}

4
Prajjwal Sharma 0
18. Program in Java to check for a BOUNCY number.
#Info:
Increasing number: Working from left to right if no digit is exceeded by the
digit to its left is called an increasing number; for example, 22344
Decreasing number: Similarly if no digit is exceeded by the digit to its right it
is called a decreasing number; for example, 774410
Bouncy number: We shall call a positive integer that is neither increasing nor
decreasing a “bouncy number”; for example, 155349. Clearly there cannot be
any bouncy numbers below 100.

import java.util.*;
class BouncyNumber
{
boolean isIncreasing(int n) //Function to check whether a number is
Increasing
4
Prajjwal Sharma 1
{
String s = Integer.toString(n);
char ch;
int f = 0;
for(int i=0; i<s.length()-1; i++)
{
ch = s.charAt(i);
if(ch>s.charAt(i+1))// If any digit is more than next digit then we
have to stop checking
{
f = 1;
break;
}
}
if(f==1)
return false;
else
return true;
}
boolean isDecreasing(int n) //Function to check whether a number is
Decreasing
{
String s = Integer.toString(n);
char ch;
int f = 0;
for(int i=0; i<s.length()-1; i++)
{
ch = s.charAt(i);
if(ch<s.charAt(i+1))// If any digit is less than next digit then we
have to stop checking
{
4
Prajjwal Sharma 2
f = 1;
break;
}
}
if(f==1)
return false;
else
return true;
}
void isBouncy(int n)
{
if(isIncreasing(n)==true)
System.out.println("The number " + n + " is Increasing and Not
Bouncy");
else if(isDecreasing(n)==true)
System.out.println("The number " + n + " is Decreasing and Not
Bouncy");
else
System.out.println("The number " + n + " is bouncy");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
BouncyNumber ob = new BouncyNumber();
System.out.print("Enter a number : ");
int n = sc.nextInt();
ob.isBouncy(n);
}
}

4
Prajjwal Sharma 3
19. Program in Java to print twin prime numbers in a
range.

#Info: Twin prime numbers are a pair of numbers which are both prime and
their difference is 2.
Example: Twin prime numbers in the range 1 to 100 are:
(3,5),(5,7),(11,13),(17,19),(29,31),(41,43),(59,61),(71,73)

import java.io.*;
class TwinPrimeRange
{
boolean isPrime(int n) //funton for checking prime
{
int count=0;
4
Prajjwal Sharma 4
for(int i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
return true;
else
return false;
}
public static void main(String args[]) throws IOException
{
TwinPrimeRange ob = new TwinPrimeRange();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the lower range : ");
int p = Integer.parseInt(br.readLine());
System.out.print("Enter the upper range : ");
int q = Integer.parseInt(br.readLine());
if(p>q)
System.out.println("Invalid Range !");
else
{
System.out.println("nThe Twin Prime Numbers within the given range are
: ");
for(int i=p; i<=(q-2); i++)
{
if(ob.isPrime(i) == true && ob.isPrime(i+2) == true)
{
System.out.print("("+i+","+(i+2)+") ");

4
Prajjwal Sharma 5
}}}}}

20. Program in java to enter a date in DDMMYYYY format


and print as shown below.
#Info: If input is 11122023, then the input should be 11 December, 2023

import java.io.*;
class Date_DDMMYY
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int l, y, d, m;
String dd, mm, yy;
//array storing the maximum days of every month

4
Prajjwal Sharma 6
int maxdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
//array storing the month names
String month[]={ "", "January", "February", "March", "April", "May",
"June", "July", "August",
"September", "October", "November", "December" };
System.out.print("Enter any date in 8 digits (ddmmyyyy) format: ");
String date = br.readLine(); //inputting the date in String format
l = date.length(); //finding number of digits in the given input
if(l==8) //performing the task only when number of digits is 8
{
dd = date.substring(0,2); //extracting the day in String format
mm = date.substring(2,4); //extracting the month in String format
yy = date.substring(4); //extracting the year in String format
d = Integer.parseInt(dd); //day in Integer format
m = Integer.parseInt(mm); //month in Integer format
y = Integer.parseInt(yy); //year in Integer format
if((y%400==0) || ((y%100!=0)&&(y%4==0))) // condition for leap year
{
maxdays[2]=29;
}
/* checking whether the day, month and year are within acceptable
range
i.e. there cannot be an input like 35012013 because 35/01/2013 is
unacceptable*/
if(m<0 || m>12 || d<0 || d>maxdays[m] || y<0 || y>9999) // Performing
Date Validation
{
System.out.println("The day, month or year are outside acceptable
limit");
}
else

4
Prajjwal Sharma 7
{
/* First Part */
System.out.println("Date in dd/mm/yyyy format = "+dd+"/"+mm+"/"+yy);
/* Second Part */
System.out.print("Date in dd, month name, yyyy format = "+dd+"
"+month[m]+", "+yy);
}
}
else
System.out.println("Wrong Input");
}
}

21. Program in Java to check whether a matrix is LOWER


TRIANGULAR.

#Info: A square matrix is called a lower triangular matrix if all the entries
above the main diagonal are zero.

import java.util.*;
class LowerTriangularMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();

4
Prajjwal Sharma 8
int A[][]=new int[m][m];
/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}

/* Printing the matrix */


System.out.println("*************************");
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");
int p=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
/* Checking that the matrix is Lower Triangular or not */
if(A[i][j]!=0) // All elements above the diagonal must be zero
{
p=1;
break;

4
Prajjwal Sharma 9
}
}
}
if(p==0)
System.out.println("The matrix is Lower Triangular");
else
System.out.println("The matrix is not Lower Triangular");
}

22. Program to check whether a matrix is UPPER


TRIANGULAR.

#Info: A square matrix is called an upper triangular matrix if all the entries
below the main diagonal are zero.

import java.util.*;
class UpperTriangularMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
5
Prajjwal Sharma 0
int A[][]=new int[m][m];
/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}
/* Printing the matrix */
System.out.println("*************************");
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");
int p=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<i;j++)
{
/* Checking that the matrix is Upper Triangular or not */
if(A[i][j]!=0) // All elements below the diagonal must be zero
{
p=1;
break;
}

5
Prajjwal Sharma 1
}
}
if(p==0)
System.out.println("The matrix is Upper Triangular");
else
System.out.println("The matrix is not Upper Triangular");
}
}

23. Program in java to check whether an entered matrix


is a DIAGONAL MATRIX.

#Info: a matrix having non-zero elements only in the diagonal running from the
upper left to the lower right.

import java.util.*;
class DiagonalMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();

5
Prajjwal Sharma 2
int A[][]=new int[m][m];

/* Inputting the matrix */


for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}
/* Printing the matrix */
System.out.println("*************************");
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");
int p=0, q=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i!=j && A[i][j]!=0) // Checking non-diagonal elements
{
p=1;
break;
}

5
Prajjwal Sharma 3
if(i==j && A[i][j]==0) // Checking diagonal elements
{
q++;
}}}
if(p==0 && q<m)
System.out.println("The matrix is Diagonal");
else
System.out.println("The matrix is not Diagonal");
}
}

24. Program in java to check whether a matrix is


SCALAR.

#Info: A diagonal matrix with all its main diagonal entries equal is a scalar
matrix, that is a scalar multiple yI of the identity matrix I.

import java.util.*;
class ScalarMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");

5
Prajjwal Sharma 4
int m=sc.nextInt();
int A[][]=new int[m][m];
/* Inputting the matrix */
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)
{System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}}
/* Printing the matrix */
System.out.println("*************************");
System.out.println("The Matrix is : ");
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");
int p = 0, q = 0, x = A[0][0]; // 'x' is storing the 1st main diagonal element
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
/* Checking that the matrix is diagonal or not */
if(i!=j && A[i][j]!=0) // All non-diagonal elements must be zero
{
p=1;
break;
}
/* Checking the matrix for scalarity */
// All main diagonal elements must be equal to 'x' and non-zero
if(i==j && (A[i][j]==0 || A[i][j]!=x))

5
Prajjwal Sharma 5
{
q=1;
break;
}}}
if(p==0 && q==0)
System.out.println("The matrix is scalar");
else
System.out.println("The matrix is not scalar");
}
}

25. Program in Java to check whether a matrix is


Symmetric and hence find the sum of right and left
diagonals.
#Info: A symmetric matrix is a square matrix that is equal to its transpose.
import java.io.*;
class SymetricMatrix
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

5
Prajjwal Sharma 6
System.out.print("Enter the number of elements : ");
int m=Integer.parseInt(br.readLine());
int A[][]=new int[m][m];
if(m>2 && m<10) // Checking for valid input of rows and columns size
{
System.out.println("\nInputting the elements in the Matrix: n");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter the elements : ");
A[i][j]=Integer.parseInt(br.readLine());
}
}
/* Printing the Original Matrix */
System.out.println("\nThe Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
/* Checking whether the matrix is symmetric or not */
int flag = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)

5
Prajjwal Sharma 7
{
if(A[i][j] != A[j][i])
{
flag = 1; // Setting flag = 1 when elements do not match
break;
}
}
}
if(flag == 1)
System.out.println("\nThe given Matrix is Not Symmetric");
else
System.out.println("\nThe given Matrix is Symmetric");
/* Finding sum of the diagonals */
int ld = 0, rd = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i == j) // Condition for the left diagonal
{
ld = ld + A[i][j];
}
if((i+j) == (m-1)) // Condition for the right diagonal
{
rd = rd + A[i][j];
}
}
}
System.out.println("The sum of the left diagonal = "+ld);

5
Prajjwal Sharma 8
System.out.println("The sum of the right diagonal = "+rd);
}
else
System.out.println("The Matrix Size is Out Of Range");
}
}

26. Program in Java to create a magic matrix.

#Info: A magic matrix is a n x n matrix in which every row, column, and


diagonal add up to the same number.

import java.io.*;
class Magic_Matrix
{public static void main(String args[])throws IOException

5
Prajjwal Sharma 9
{BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("\n\nEnter the size of the matrix : ");
int n=Integer.parseInt(br.readLine());
if(n>5)
System.out.println("Enter a number between 1 to 5 ");
else
{
int A[][]=new int[n][n]; // Creating the Magic Matrix
int i,j,k,t;
/*Initializing every cell of the matrix with 0 */
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
A[i][j] = 0;
}}
/* When the size of the matrix is Odd */
if(n%2!=0)
{
i=0;
j = n/2;
k = 1;
while(k<=n*n)
{
A[i][j] = k++;
i--; // Making one step upward
j++; // Moving one step to the right
if(i<0 && j>n-1) // Condition for the top-right corner element
{
i = i+2;

6
Prajjwal Sharma 0
j--;
}
if(i<0) // Wrapping around the row if it goes out of boundary
i = n-1;
if(j>n-1) // Wrapping around the column if it goes out of boundary
j = 0;
if(A[i][j]>0) // Condition when the cell is already filled
{
i = i+2;
j--;
}}}
/* When the size of the matrix is even */
else
{k = 1;
/* Filling the matrix with natural numbers from 1 till n*n */
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
A[i][j] = k++;
}
}
j = n-1;
for(i=0; i<n/2; i++)
{
/* swapping corner elements of primary diagonal */
t = A[i][i];
A[i][i] = A[j][j];
A[j][j] = t;
/* swapping corner elements of secondary diagonal */
t = A[i][j];

6
Prajjwal Sharma 1
A[i][j] = A[j][i];
A[j][i] = t;
j--;
}
}
/* Printing the Magic matrix */
System.out.println("The Magic Matrix of size "+n+"x"+n+" is:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}}}}

27. Program in java to find the SADDLE POINT of a


matrix.
#Info: A saddle point of a matrix is an element which is both the largest
element in its column and the smallest in its row.
import java.util.*;
class saddle_point
{

6
Prajjwal Sharma 2
public static void main()
{
int a=0,b=0,d=0,e=0,f=0,g=0,h=0,max=0,min=0;
Scanner sc=new Scanner(System.in);
System.out.println("row");
a=sc.nextInt();
System.out.println("column");
b=sc.nextInt();
int c[][]=new int[a][b];
for(d=0;d<a;d++)
{
for(e=0;e<b;e++)
{System.out.println("ENTER "+(d+1)+" row " +(e+1)+" column");
c[d][e]=sc.nextInt();
}}
for(d=0;d<a;d++)
{for(e=0;e<b;e++)
{min=c[d][f];
for(f=0;f<a-1;f++)
{g=Math.min(min,c[d][f+1]);
min=g;}
f=0;
max=c[f][e];
for(f=0;f<b-1;f++)
{h=Math.max(max,c[f+1][e]);
max=h;}
f=0;
if(max==min)
{if(max==c[d][e])

6
Prajjwal Sharma 3
{System.out.println("Saddle point="+c[d][e]);
h++;
}}}}
if(h==0)
System.out.println("NO SADDLE POINT");
}}

28. Program in Java to print the truth table columns of


XOR and XNOR gates.
import java.util.*;
class truth_table
{

6
Prajjwal Sharma 4
public static void main()
{
int a=0,b=0,c=0,d=0,e=0,x=0,y=0,z=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NO. OF ELEMENT");
a=sc.nextInt();
b=(int)Math.pow(2,a);
x=b/2;
int arr[][]=new int[b][a];
for(c=0;c<a;c++)
{
for(d=0;d<b;d++)
{
if(y%2==0)
{
arr[d][c]=0;
z++;
}
else
{
arr[d][c]=1;
z++;
}
if(z==x)
{
z=0;
y++;
}}
x=x/2;

6
Prajjwal Sharma 5
y=0;
}System.out.println("LAST SECOND COLOUM=XOR && LAST COLOUM=XNOR ");
for(c=0;c<b;c++)
{for(d=0;d<a;d++)
{
System.out.print(arr[c][d]+" ");
e=e+arr[c][d];
}if(e%2!=0)
System.out.print("1 ");
else
System.out.print("0 ");
if(e%2==0)
System.out.print("1 ");
else
System.out.print("0 ");
e=0;
System.out.println();
}}}

29. Program to input a list of integers in an array and


arrange them in away similar to the to-and-fro movement
of a pendulum.
import java.io.*;
6
Prajjwal Sharma 6
class Pendulum_Array
{public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("nEnter number of elements: "); // Inputting the
number of elements
int n = Integer.parseInt(br.readLine());
int A[]=new int[n]; //original array
int B[]=new int[n]; //array for storing the result
/*Inputting the Array*/
for(int i=0; i<n; i++)
{System.out.print("Enter Element "+(i+1)+": ");
A[i] = Integer.parseInt(br.readLine());
}
/*Sorting the Input Array in Ascending Order*/
int t=0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(A[i]>A[j])
{
t=A[i];
A[i]=A[j];
A[j]=t;}}}
/*Printing the Sorted Array*/
System.out.println("\nThe Sorted Array Is");
for(int i=0; i<n; i++)
{System.out.print(A[i]+"\t");}

6
Prajjwal Sharma 7
int mid = (n-1)/2; //finding index of middle cell
int x = 1, lim = n-1-mid;
/*'x' is for accessing elements of array A[] and
'lim' is for the number of times we have to make this to-and-fro
movement*/
/* Pendulum Arrangement Starts Here */
B[mid]=A[0]; //putting the minimum element in the middle cell
for(int i=1; i<=lim; i++)
{
if((mid+i)<n) //going to the right side
B[mid+i]=A[x++];
if((mid-i)>=0) //going to the left side
B[mid-i]=A[x++];
}
/*Printing the Result*/
System.out.println("\n\nThe Result Is");
for(int i=0; i<n; i++)
{System.out.print(B[i]+"\t");}}}

30. Program in java to input a word and print its


anagrams.

#Info: Anagrams are words made up of all the characters present in the
original word by re-arranging the characters. 6
Prajjwal
Example:Sharma
Anagrams of the word TOP are: TOP, TPO, OPT, OTP, PTO and POT 8
import java.util.*;
class Anagrams
{
int c = 0;
void input()throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word : ");
String s = sc.next();
System.out.println("The Anagrams are : ");
display("",s);
System.out.println("Total Number of Anagrams = "+c);
}
void display(String s1, String s2)
{
if(s2.length()<=1)
{
c++;
System.out.println(s1+s2);
}
else
{
for(int i=0; i<s2.length(); i++)
{
String x = s2.substring(i, i+1);
String y = s2.substring(0, i);
String z = s2.substring(i+1);

6
Prajjwal Sharma 9
display(s1+x, y+z);
}
}}
public static void main(String args[])throws Exception
{
Anagrams ob=new Anagrams();
ob.input();
}
}

31. Design a program to accept a fifteen digit number


from the user and check whether it is a valid IMEI
7
Prajjwal Sharma 0

#Info: The International Mobile Station Equipment Identity or IMEI is a


number, usually unique, to identify mobile phones, as well as some satellite
number or not. For an invalid input, display an
appropriate message.

import java.io.*;
class IMEI
{
int sumDig(int n) // Function for finding and returning sum of digits
of a number
{
int a = 0;
while(n>0)
{
a = a + n%10;
n = n/10;}
return a;
}public static void main(String args[])throws IOException

7
Prajjwal Sharma 1
{
IMEI ob = new IMEI();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter a 15 digit IMEI code : ");
long n = Long.parseLong(br.readLine()); // 15 digits cannot be stored
in 'int' data type
String s = Long.toString(n); // Converting the number into String for
finding length
int l = s.length();
if(l!=15) // If length is not 15 then IMEI is Invalid
System.out.println("Output : Invalid Input");
else
{
int d = 0, sum = 0;
for(int i=15; i>=1; i--)
{
d = (int)(n%10);
if(i%2 == 0)
{
d = 2*d; // Doubling every alternate digit
}
sum = sum + ob.sumDig(d); // Finding sum of the digits
n = n/10;
}
System.out.println("Output : Sum = "+sum);
if(sum%10==0)
System.out.println("Valid IMEI Code");
else
System.out.println("Invalid IMEI Code");
}}}
7
Prajjwal Sharma 2
7
Prajjwal Sharma 3
32. A class Recursion has been defined to find the
Fibonacci series up to a limit. Some of the members
of the class are given below:
Class Name: Recursion
Data Members/instance variables: a, b, c, limit (all
integers)
Member functions/methods:
Recursion(): constructor to assign a,b,c with
appropriate values.
void input() : to accept the limit of the series.
int fib(int n) : to return the nth Fibonacci term
using recursive technique.
void genearate_fibseries() : to generate the Fibonacci
series upto the given limit.
Specify the class Recursion giving details of
the constructor, int fib() , void generate_fibseries().
You may assume other functions are written for you and
you need not write the main function.

import java.io.*;
class Recursion
{
static BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int a,b,c,limit;
Recursion() //Constructor
{
a=0;
b=1;
c=0;
limit=0;
}

void input()throws IOException //Function to input the limit


{
System.out.print("Enter the limit : ");
limit=Integer.parseInt(br.readLine());
}
int fib(int n) //Recursive function generating the 'nth' term of
Fibonacci Series

7
Prajjwal Sharma 4
{
if(n<=1)
return a;
else if(n==2)
return b;
else
return (fib(n-1)+fib(n-2));
}
void generate_fibseries() //Function generating all the Fibonacci
Series numbers upto 'n' terms
{
System.out.println("The Fibonacci Series is:");
for(int i=1;i<=limit;i++)
{
c=fib(i);
System.out.print(c+" ");
}
}
public static void main(String args[])throws IOException
{
Recursion ob=new Recursion();
ob.input();
ob.generate_fibseries();
}
}

7
Prajjwal Sharma 5
33. Program in java to check for CIRCULAR PRIME
numbers.

#Info: A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the
leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is
still prime. The process is repeated until the original number is reached again.
A number is said to be prime if it has only two factors 1 and itself.
Example:
131
311
113
Hence, 131 is a circular prime.

import java.util.*;
class CircularPrime
{
boolean isPrime(int n) // Function for checking whether a number is
prime or not
{
int c = 0;
for(int i = 1; i<=n; i++)
{
if(n%i == 0)
c++;
}
if(c == 2)

7
Prajjwal Sharma 6
return true;
else
return false;
}
int circulate(int n) //Function for circulating the digits to form new
number
{
String s = Integer.toString(n);
String p = s.substring(1)+s.charAt(0);
int a = Integer.parseInt(p);
return a;
}
void isCircularPrime(int n) //Function to check for circular prime
{
int f = 0,a = n;
do
{
System.out.println(a);
if(isPrime(a)==false)
{
f = 1;
}
a = circulate(a);
}while(a!=n);
if(f==1)
System.out.println(n+" IS NOT A CIRCULAR PRIME");
else
System.out.println(n+" IS A CIRCULAR PRIME");
}
public static void main(String args[])

7
Prajjwal Sharma 7
{
CircularPrime ob = new CircularPrime ();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
ob.isCircularPrime(n);
}
}

7
Prajjwal Sharma 8
34. Program in Java to arrange the words of a sentence
according to their potential.
The encryption of alphabets are to be done as follows:

A=1
B=2
C=3
.
.
.
Z=26
The potential of a word is found by adding the encrypted value of the alphabets.

Example: KITE

Potential = 11 + 9 + 20 + 5 = 45

Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is


separated by single space. Decode the words according to their potential and arrange them in
ascending order.

Output the result in format given below:

Example 1
INPUT : THE SKY IS THE LIMIT.

POTENTIAL : THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63
7
Prajjwal Sharma 9
import java.util.*;
class WordPotential
{
int findPotential(String s) // Function to find potential of a word
{
s = s.toUpperCase();
int p = 0, l = s.length();
char ch;
for(int i=0; i<l; i++)
{
ch = s.charAt(i);
p = p + (ch-64); // if ch = 'A', then 'A'-64 = ASCII value of 'A' - 64
= 65-64 = 1
}
return p;
}
// Function to sort the words in ascending order of their potential
void sortPotential(String w[], int p[])
{
int n = w.length, t1 = 0;
String t2 = "";
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(p[i]>p[j])
{
t1 = p[i];

8
Prajjwal Sharma 0
p[i] = p[j];
p[j] = t1;
t2 = w[i];
w[i] = w[j];
w[j] = t2;
}}}
printResult(w,p);
}
void printResult(String w[], int p[]) // Function to print the final
result
{
int n = w.length;
String ans = "";
for(int i=0; i<n; i++)
{
ans = ans + " " + w[i];
}
ans = ans.trim();
System.out.println("\nOutput\t\t : \t"+ans);
}
public static void main(String args[])
{
WordPotential ob = new WordPotential();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence : \t");
String s = sc.nextLine();
StringTokenizer str = new StringTokenizer(s," .,?!");
int n = str.countTokens();
String words[] = new String[n];
int potential[] = new int[n];

8
Prajjwal Sharma 1
for(int i=0; i<n; i++)
{
words[i] = str.nextToken(); // Saving words one by one in an array
potential[i] = ob.findPotential(words[i]); // Saving potential of
every word
}
// Printing the words along with their potential
System.out.print("\nPotential\t : \t");
for(int i=0; i<n; i++)
{
System.out.println(words[i]+"\t= "+potential[i]);
System.out.print("\t\t\t");
}
ob.sortPotential(words,potential);
}}

8
Prajjwal Sharma 2
35. Program to decode an encrypted code
#Info: A simple encryption system uses a shifting process to hide a message. The value of the shift
can be in the range 1 to 26. For example a shift of 7 means that A = U, B =V,C = W, etc.i e.
Text : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Code: U V W X Y Z A B C D E F G H I J K L M N O P Q R S T

Fist an extra space is added to the end of the string. To make things little more difficult, spaces within
the original text are replaced with QQ before the text is encrypted. Double Q (QQ) was selected
because no English word ends in Q or contains QQ.

Additionally the coded message is printed in blocks of six characters separated by spaces. The last
block might not contain six characters. Write a program that takes the coded text (less than 100
characters), the shift value and prints the decoded original text.Your program must reject any non-valid
value for shift and display an error message “INVALID SHIFT VALUE)”. Assume all characters are
upper case. Test your program for the following data and some data that you have coded, using the
rules given above:

SAMPLE DATA:
1.INPUT:
CODED TEXT : “UHINBY LKKQCH HYLKK”
SHIFT : 7
OUTPUT:
DECODED TEXT : ANOTHER WINNER
import java.io.*;
public class Decode
{

8
Prajjwal Sharma 3
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter Coded Text : "); // inputting coded text
String s = br.readLine();
int l = s.length();
s = s.toUpperCase(); // converting the coded text into Uppercase
s = s + " "; // adding a space at the end

if(l>=100) // checking whether length of inputted code is less than


100
System.out.println("!!! Invalid Length of Coded Text !!!");

else
{
System.out.print("Enter the Shift Value : ");
int shift = Integer.parseInt(br.readLine());

if(shift<1 || shift>26) // checking whether shift value is between 1


and 26
System.out.println("!!! Invalid Shift Value !!!");

else
{
int a, b;
char ch1, ch2;
String dec=""; //new String for storing the decoded text

for(int i=0; i<l; i++)


{
8
Prajjwal Sharma 4
ch1 = s.charAt(i); // extracting characters one by one
ch2 = s.charAt(i+1); // extracting the next character

/* Below we are adding shift value to the characters


* if ch1 = 'A' and shift = 7,
* then ch1 + shift - 1 will give us: 'A'+7-1 = 65+7-1 = 71
* which is the ASCII value of 'G'
*/
a = ch1 + shift - 1; // storing ASCII values after adding shift to the
current character
b = ch2 + shift - 1; // storing ASCII values after adding shift to the
next character

/* If the currrent character and the next character are both 'Q' then
we have a 'space'
* hence the ASCII value should be 32
*/
if((char)a == 'Q' && (char)b == 'Q')
{
a = 32;
i++;
}

/* If ASCII value after adding the shift becomes more than 90,
* then we subtract 26 from it, to make it circular,
* eg. 'U'+7-1 = 85+7-1 = 91, but we want 'A' whose ASCII value is 65
* so 91-26 will give us 65
*/
if(a>90)
a = a - 26;
if(ch1 != ' ')
8
Prajjwal Sharma 5
dec = dec + (char)a; // finally adding the decoded character to the
new String
}
System.out.println("Decoded Text : "+dec);
}}}}

36. Binary Search using recursion.

#Info: A class Admission contain the admission numbers of 100 students. Some of
the data members/ member functions are given below:
Class name: Admission
Data member/instance variable:

Adno[ ]: Integer array to store admission numbers

Member functions/methods:
Admission(): constructur to initialize the array elements
void fillArray(): to accept the element of the array in ascending order
int binSearch(int l, int u, int v): to search for a particular admission number(v) using
binary search and recursive technique and return 1 if found otherwise returns -1
Specify the class Admission giving details of the constructor, void
fillArrray() and int binSearch(int, int, int). Define the main() function to create
an object and call the functions accordingly to enable task.

import java.util.*;
class Admission
{
static Scanner sc = new Scanner(System.in);
int Adno[];
static int n;
Admission() // Default constructor

8
Prajjwal Sharma 6
{
Scanner k=new Scanner(System.in);
System.out.println("Enter the number of students");
n=k.nextInt();
Adno=new int[n];
for(int i=0; i<n; i++)
{
Adno[i]=0;
}
}
void fillArray()throws Exception // Function to accept elements in
ascending order
{
for(int i=0; i<n; i++)
{
System.out.print("Enter Admission no of student "+(i+1)+": ");
Adno[i] = sc.nextInt();
}
/*Sorting the array in ascending order */
int temp=0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(Adno[i]>Adno[j])
{
temp = Adno[i];
Adno[i] = Adno[j];
Adno[j] = temp;
}

8
Prajjwal Sharma 7
}
}
}

int binSearch(int l, int u, int v) // Recursive function implementing


binary search
{
int mid = (l + u)/2;
if(u < l) // condition if the search is unsuccessful
{
return -1;
}
if(v==Adno[mid]) // condition if the search is successful
{
return 1;
}
else if(v>Adno[mid])
{
return binSearch(mid+1,u,v);
}
else
{
return binSearch(l,mid-1,v);
}
}
public static void main(String args[])throws Exception
{
Admission ob = new Admission();
System.out.println("Enter Admission number in ascending order");
ob.fillArray();

8
Prajjwal Sharma 8
System.out.print("Enter an Admission number to search : ");
int v = sc.nextInt();
int f = ob.binSearch(0,(n-1),v);
System.out.println("*****************************");
if(f == 1)
{
System.out.println("Admission Number found");
}
else
{
System.out.println("Admission Number Not found");
}
}}

8
Prajjwal Sharma 9
37. Rotate matrix 90o clockwise.
Write a program to declare a square matrix A[ ][ ] of order MxM where ‘M’ is the number of
rows and the number of columns, such that M must be greater than 2 and less than 10.
Accept the value of M as user input. Display an appropriate message for an invalid input.
Allow the user to input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Rotate the matrix 90° clockwise as shown below:

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

Example 1
INPUT :
M=3

OUTPUT :

ORIGINAL MATRIX

MATRIX AFTER ROTATION

9
Sum of the corner elements = 20 0
Prajjwal Sharma
import java.util.*;
class Rotation
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
if(m<3 || m>9)
System.out.println("Size Out Of Range");
else
{
int A[][]=new int[m][m];
/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}
/* Printing the original matrix */
System.out.println("*************************");
System.out.println("The Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");

/*Rotation of matrix begins here */


System.out.println("Matrix After Rotation is : ");
for(int i=0;i<m;i++)
{
for(int j=m-1;j>=0;j--)
{
System.out.print(A[j][i]+"\t");
}
System.out.println();
}
System.out.println("*************************");
int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding sum of corner
elements
System.out.println("Sum of the corner elements = "+sum);
}
}
}

9
Prajjwal Sharma 1
38. Merging arrays inside object.
A class Mixer has been defined to merge two sorted integer arrays in ascending order. Some of the
members of the class are given below:

Class name : Mixer


Data members/instance variables:
int arr[] : to store the elements of an array
int n : to store the size of the array
Member functions:
Mixer( int nn) : constructor to assign n = nn
void accept() : to accept the elements of the array in ascending order without any
duplicates
Mixer mix( Mixer A) : to merge the current object array elements with the
parameterized array elements and return the resultant object
void display() : to display the elements of the array
Specify the class Mixer, giving details of the constructor(int), void accept(), Mixer
mix(Mixer) and void display(). Define the main() function to create an object and call the function
accordingly to enable the task.

import java.io.*;
class Mixer
{

9
Prajjwal Sharma 2
int arr[];
int n;
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));

Mixer(int nn)
{
n = nn;
arr = new int[n];
}

void accept()throws IOException


{
System.out.println("\n* Input the Array *\n");
for(int i=0; i<n; i++)
{
System.out.print("Enter Element ["+(i+1)+"] : ");
arr[i] = Integer.parseInt(br.readLine());
}
System.out.println();
}

Mixer mix(Mixer A)
{
int size = this.arr.length + A.arr.length; //size of resulting array
/* 'this' keyword refers to the current object, i.e. the object which calls
mix() function */
Mixer B = new Mixer(size); //object which will store the result of merging

int x = 0;

/* Merging the array of current object with array of parameter object */


for(int i=0; i<size; i++)
{
if(i<A.arr.length)
B.arr[i] = A.arr[i];
else
{
B.arr[i] = this.arr[x];
x++;
}
}

/* Sorting the result*/


int temp=0;
for(int i=0; i<size-1; i++)
{
for(int j=i+1; j<size; j++)
{
if(B.arr[i]>B.arr[j])
{
temp = B.arr[i];
B.arr[i] = B.arr[j];
B.arr[j] = temp;
}

9
Prajjwal Sharma 3
}
}

return B;
}

void display()
{
for(int i=0; i<n; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}

public static void main(String args[])throws IOException


{
System.out.print("Enter size of the 1st array : ");
int p = Integer.parseInt(br.readLine());
Mixer obj1 = new Mixer(p);
obj1.accept();

System.out.print("Enter size of the 2nd array : ");


int q = Integer.parseInt(br.readLine());
Mixer obj2 = new Mixer(q);
obj2.accept();

Mixer obj3 = obj2.mix(obj1); //obj2 is the current object which is referred by


'this' keyword above

System.out.print("The 1st Array is : ");


obj1.display();

System.out.print("The 2nd Array is : ");


obj2.display();

System.out.print("The Merged Array is : ");


obj3.display();
}
}

9
Prajjwal Sharma 4
39. Decimal to Roman number Conversion
Note: The number entered should be within the Range 1-3999

import java.io.*;
class Dec2Roman
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num;
String str="";
System.out.print("Enter a Number : ");
num=Integer.parseInt(br.readLine()); //accepting decimal number

9
Prajjwal Sharma 5
/*Arrays storing the unique symbols of Roman Number System
and also the corresponding decimal equivalents in the second array*/
String roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V",
"IV", "I"};
int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
if(num>0 && num<4000) //checking whether the number entered is within the
range [1-3999]
{
for (int i=0; i<13; i++) // i<13 because the total unique numbers stored in
the above array=13
{
/*The while loop is for printing repeated digits like XXX for 30 etc
and is also calculating the equivalent Roman number by adding the
corresponding
Roman Digits from the Array to the String str*/
while (num >= decimal[i])
{
num = num-decimal[i];
str = str+roman[i];
}
}
System.out.println("Roman Equivalent = "+str); //Printing the Roman equivalent
}

/*Displaying an error message if the number entered is out of range*/


else
System.out.println("You entered a number out of Range. Please enter a number
in the range [1-3999]");
}
}

40. Program to check for Pronic number


#Info: A pronic number is a number which is the product of two consecutive integers i.e., a number of
the form n(n+1)
import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
int flag = 0;

9
Prajjwal Sharma 6
for(int i=1; i<=n; i++)
{
if(i*(i+1) == n)
{
flag = 1;
break;
}
}

if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}
}

APPLET
(Moving a ball)
import java.applet.*;
import java.awt.*;
/*<applet code = "ball" width = 400 height = 200> </applet> */
public class ball extends Applet implements Runnable
{ Thread t;
int x = 0;
int y = 0;

9
Prajjwal Sharma 7
public void start()
{ t = new Thread(this);
t.start();
}
public void paint(Graphics g)
{
g.fillOval(x,y,100,100);
}
public void run()
{try
{for(;;)
{for(;;)
{if(y == 120)
{
break;
}
else if (x == 390)
{ x = 0;
y = 0;
repaint();
}
else
{ y = y +3;
x =x+3;
Thread.sleep(100);
repaint();
}
}
for(;;)
{
if(y==0)
{
break;
}
else if (x == 390)
{
x = 0;
y = 0;
repaint();
}
else
{
y = y-3;
x=x+3;
Thread.sleep(100);
repaint();
}
}
run();
}

9
Prajjwal Sharma 8
}
catch(InterruptedException e)
{ }
}
}

9
Prajjwal Sharma 9

You might also like