11Sc. Srijeeta Sen Computer Practical File
11Sc. Srijeeta Sen Computer Practical File
1. Algorithm 2
2. Source code 30
3. Output 76
4. Variable Description 86
1|Page
ALGORITHM
2|Page
1. A Smith number is a composite number, the sum of whose digits is
the sum of the digits of its prime factors obtained as a result of prime
factorization (excluding 1). The first few such numbers are 4, 22, 27, 58,
85, 94, 121 ………………..
Examples: 666
Prime factors are 2, 3, 3, and 37 Sum of the digits are (6+6+6) = 18 Sum
of the digits of the factors (2+3+3+(3+7)) = 18
3|Page
(d)Store the sum of digits in s.
Step 4: Return the value of s to the main() function.
Step 5: End of algorithm for prime_factors() method.
4|Page
2. 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.
5|Page
Step 2: Start a for loop from i=0 to i=arr.length-1, where i increases by 1
everytime.
Step 3: Input the array elements for the array arr.
Step 4: End of algorithm.
Algorithm for method mix ():
Step 1: Start of algorithm.
Step 2: Create an object C.
Step 3: (a)Start a for loop from i=0 to i=A.length-1, where i increases by 1
everytime.
(b) Store the elements of B.arr[] in C.arr[].
Step 4: (a)Start a for loop from i=0 to i=B.length-1, where i increases by 1
everytime.
(b) Store the elements of B.arr[] in C.arr[].
Step 5: Return C.
Step 6: End of algorithm.
Algorithm for method display():
Step 1: Start of algorithm.
Step 2: (a)Start a for loop from i=0 to i=arr.length-1, where i increases by
1 everytime.
(b) Display the result.
Step 3: End of algorithm.
6|Page
3. 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.
8|Page
4. Write a Program to Input a sentence and arrange the words of the
sentence in alphabetical order.
Input: I live in Kolkata.
Output: I in Kolkata live.
9|Page
5. 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.
Example: 131
311
113
Hence, 131 is a circular prime.
10 | P a g e
6. Write a Program to Input a matrix and check whether it is identity
matrix or not.(Dimension of the matrix to be read from the user).
The dictionary definition of an Identity Matrix is a square matrix in
which all the elements of the principal or main diagonal are 1’s and all
other elements are zeros.
11 | P a g e
7. Write a program to declare a matrix a[][] of order (m × n) where ‘m’ is
the number of rows and ‘n’ is the number of columns such that the
values of both ‘m’ and ‘n’ must be greater than 2 and less than 10. Allow
the user to input integers into this matrix. Perform the following tasks
on the matrix:
▪ Display the original matrix.
▪ Sort each row of the matrix in ascending order using any standard
sorting technique.
▪ Display the changed matrix after sorting each row.
Test your program for the following data and some random data:
Example : INPUT: m = 4
n=3
Enter elements of matrix:
11 -2 3
5 16 7
9 0 4
3 1 8
OUTPUT: Original Matrix:
11 -2 3
5 16 7
9 0 4
3 1 8
Matrix after sorting rows:
-2 3 11
5 7 16
0 4 9
1 3 8
12 | P a g e
Algorithm for main():
Step 1: Start of algorithm.
Step 2: Input the size of the matrix and store it in m and n.
Step 3: Declare and initialise the array A[][].
Step 4: Input the elements of the array.
Step 5: (a)Start a for loop from k=0 to k<m where k increases by 1
everytime.
(b) Start a for loop from i=0 to i<m where i increases by 1 everytime.
(c)Start a for loop from j=0 to j<n-i-1 where j increases by 1 everytime.
(d)Compare and swap the elements accordingly
Step 6: Display the result.
Step 7: End of algorithm.
13 | P a g e
8. Write a program to declare a square matrix A[ ] [ ] of order (M x M)
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) Check if the given matrix is Symmetric or not.
A square matrix is said to be Symmetric, if the element of the ith row
and jth column is equal to the element of the jth row and ith column.
Test your program with the sample data and some random data:
INPUT : M = 3
123
245
356
OUTPUT :
ORIGINAL MATRIX
123
245
356
14 | P a g e
(c) If the element of the ith row and jth column is equal to the element of
the jth row and ith column, increment flag.
Step 6: Display the result.
Step 7: End of algorithm.
15 | P a g e
9. Write a program to create a square matrix of m*m order and find out
the sum of boundary and non boundary elements of the matrix.
16 | P a g e
10. Write a Program in Java to fill a square matrix of size ‘n*n” in a
circular fashion (clockwise) with natural numbers from 1 to n*n, taking
‘n’ as input.
For example: if n = 4, then n*n = 16, hence the array will be filled as
given below.
17 | P a g e
Step 8 : (a) Start a for loop from i = c2-1 to c1, where ‘i’ decreases by 1
every time and perform step (b)
(b) Store the natural numbers in the last row using A[r2][i] = k++
Step 9 : (a) Start a for loop from j = r2-1 to r1+1, where ‘j’ decreases by 1
every time and perform step (b)
(b) Store the natural numbers in the first column using A[j][c1] = k++
Step 10 : Update the variables c1, c2, r1 and r2
Step 11 : Display the circular matrix A[ ]
Step 12 : End of Algorithm
18 | P a g e
11. Write a program to accept a sentence which may be terminated by
either’.’, ‘?’or’!’ only. The words may be separated by more than one
blank space and are in UPPER CASE.
Perform the following tasks:
(a) Find the number of words beginning and ending with a vowel.
(b) Place the words which begin and end with a vowel at the beginning,
followed by the remaining words as they occur in the sentence.
INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW
THAN YOU ARE TODAY.
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A
VOWEL= 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN
YOU TODAY
19 | P a g e
12. Write a Program in Java to input a number and check whether it is an
Evil Number or not.
Evil Number : An Evil number is a positive whole number which has
even number of 1’s in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of
1’s. A few evil numbers are 3, 5, 6, 9….
Design a program to accept a positive whole number and find the binary
equivalent of the number and count the number of 1’s in it and display
whether it is a Evil number or not with an appropriate message. Output
the result in format given below:
INPUT : 15 BINARY EQUIVALENT : 1111 NO. OF 1’s : 4 OUTPUT :
EVIL NUMBER
20 | P a g e
13. 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:
INPUT : THE SKY IS THE LIMIT.
POTENTIAL : THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63
OUTPUT : IS THE THE SKY LIMIT
21 | P a g e
Step 6: (a)Start a for loop from i=0 to i<s.length().
(b) Extract each word and pass a reference of it to the method decode().
Step 7: (a)Start a for loop from i=0 to i<c where i increases by 1
everytime.
(b)Start a for loop from k=0 to k<c-i-1 where k increases by 1 everytime
(c)Compare the potentials of successive elements and swap the elemnts
accordingly.
Step 8: Display the result.
Step 9: End of algorihm.
Algorithm for decode():
Step 1: Start of algorithm
Step 2: Extract each character of the word, add its ASCII code and store
it in res.
Step 3: Return res.
Step 4: End of algorithm.
22 | P a g e
14. Write a Program in Java to input a 2-D square matrix and check
whether it is a Diagonal Matrix or not.
Diagonal Matrix : A diagonal matrix is a matrix (usually a square
matrix) in which the entries outside the main diagonal are all zero. The
diagonal entries themselves may or may not be zero (but all diagonal
entries cannot be zero).
Example:
5000
0100
0000
0007
23 | P a g e
15. Write a program to input a string (word). Convert it into lowercase
letters. Count and print the frequency of each alphabet present in the
string. The output should be given as:
Sample Input: Alphabets
Sample Output: ==========================
Alphabet Frequency
==========================
a 2
b 1
e 1
h 1
l 1
p 1
s 1
t 1
24 | P a g e
16. Write a program to check whether a number is prime or not using
recursive method.
25 | P a g e
17. Write a program to print first ‘n’ fibonacci numbers using recursive
method.
26 | P a g e
18. Write a program to print the greatest common divisor of 2 numbers
using recursive method.
27 | P a g e
19. Write a program to print the reverse of a string using recursive
method.
28 | P a g e
20. Perform Binary search using recursive method
29 | P a g e
SOURCE CODE
30 | P a g e
1. A Smith number is a composite number, the sum of whose digits is
the sum of the digits of its prime factors obtained as a result of prime
factorization (excluding 1). The first few such numbers are 4, 22, 27, 58,
85, 94, 121 ………………..
Examples: 666
Prime factors are 2, 3, 3, and 37 Sum of the digits are (6+6+6) = 18 Sum of
the digits of the factors (2+3+3+(3+7)) = 18
SOLUTION:
import java.util.*;
class Smith //start of class
{
int prime_factors(int x) //method to
{ calculate sum of
int s=0; prime factors
for(int i=2; i<=x ;i++) //outer loop starts
{
while(x%i==0) //inner loop to find
{ prime factors
for(int j=i; j>0;j=j/10) //loop to calculate
s=s+(j%10); sum of digits of factors
x=x/i;
} // inner loop ends
}
return s;
} //end of method
public static void main(String[] args)
{ //start of main()
int sd=0;
Smith ob= new Smith();
31 | P a g e
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int n=sc.nextInt();
for(int i=n; i>0;i=i/10) //loop for calculating sum
sd=sd+(i%10); of digits of number
if(sd== ob.prime_factors(n))
System.out.println("Smith Number");
else
System.out.println("Not a smith number");
} //end of main()
} //end of class
32 | P a g e
2. 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.
SOLUTION:
import java.util.*;
class Mixer //start of class
{
int n; //data members
int arr[]=new int[n];
34 | P a g e
Mixer A=new Mixer(4);
A.accept();
Mixer B= new Mixer(6);
B.accept();
Mixer K= B.mix(A);
K.display();
} //end of main()
} //end of class
35 | P a g e
3. 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.
SOLUTION:
import java.util.*;
class Admission
{ //start of class
int Adno[]= new int [100]; //data member
36 | P a g e
}
void fillArray() //to accept array elements
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the admission numbers of 100
students:");
for(int i=0; i<100; i++)
Adno[i]=sc.nextInt();
}
int binSearch(int l, int u, int v) // to search for a
{ particular admission
int mid= (l+u)/2; number using binary search
if(Adno[mid]==v) and recursive technique
return (1);
else if(l>u)
return (-1);
else if(Adno[mid]>v)
return binSearch(l,mid-1,v);
else
return binSearch(mid+1,u,v);
}
public static void main(String[] args)
{ //start of main()
Admission ob= new Admission();
Scanner sc=new Scanner(System.in);
37 | P a g e
ob.fillArray();
System.out.println("Enter the number to be searched:");
int v=sc.nextInt();
if(ob. binSearch(0,99,v)==1)
System.out.println("number found");
else
System.out.println("number not found");
} //end of main()
} //end of class
38 | P a g e
4. Write a Program to Input a sentence and arrange the words of the
sentence in alphabetical order.
Input: I live in Kolkata.
Output: I in Kolkata live.
SOLUTION:
import java.util.*;
public class Arrange
{ //start of class
public static void main(String[] args)
{ //start of main()
Scanner Sc=new Scanner(System.in);
String str="", tmp="";
int b, count=0;
System.out.println("Enter a sentence:");
str=Sc.nextLine();
str=str+" ";
str=str.toUpperCase();
System.out.println();
System.out.println("INPUT: "+str);
for(int j=0; j< str.length();)
{
b= str.indexOf(' ', j);
count++;
j=b+1;
}
39 | P a g e
String A[]= new String[count];
for(int j=0, i=0; j< str.length();i++)
{ //loop to fill the array with each
b= str.indexOf(' ', j); word of the sentence
A[i]= str.substring(j,b);
j=b+1;
}
40 | P a g e
5. 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.
Example:
131
311
113
Hence, 131 is a circular prime.
SOLUTION:
import java.util.*;
class CircularPrime
{ //start of class
boolean isprime(int p) //to determine
{ whether a number is
int i,pc=0; prime or not
41 | P a g e
return false;
}
public static void main(String[] args)
{ //start of main()
int c=0, cn, flag=0, res=0;
CircularPrime ob= new CircularPrime();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int n=sc.nextInt();
for(int i=n;i>0;i=i/10)
c++;
cn=c;
while(c>0)
{
res=(n % (int) Math.pow( 10 , cn- 1))*10+(n/(int)Math.pow(10,
cn-1));
c--;
if(ob.isprime(res)==true)
flag++;
}
if(flag==cn)
System.out.println(n+" is a circular prime number");
else
System.out.println(n+" is not a circular prime number");
} //end of main()
} //end of class
42 | P a g e
6. Write a Program to Input a matrix and check whether it is identity
matrix or not.(Dimension of the matrix to be read from the user).
The dictionary definition of an Identity Matrix is a square matrix in
which all the elements of the principal or main diagonal are 1’s and all
other elements are zeros.
SOLUTION:
import java.util.*;
class Identity
{ //start of class
public static void main(String[] args)
{ //start of main()
int flag=0 ;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the matrix:");
int n=sc.nextInt();
int A[][]= new int[n][n];
System.out.println("Enter "+(n*n)+" elements for the array :");
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
A[i][j]=sc.nextInt();
System.out.println("INPUT MATRIX:");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
System.out.print(A[i][j]+" ");
43 | P a g e
}
System.out.println();
}
for(int i=0; i<n; i++) //to check whether the matrix is an
{ identity matrix or not
for(int j=0; j<n; j++)
{
if(i==j && A[i][j]==1)
flag++;
else if(i!=j && A[i][j]==0)
flag++;
}
}
if(flag==(n*n))
System.out.println("IDENTITY MATRIX");
else
System.out.println("Not an IDENTITY MATRIX");
} //end of main()
} //end of class
44 | P a g e
7. Write a program to declare a matrix a[][] of order (m × n) where ‘m’ is
the number of rows and ‘n’ is the number of columns such that the
values of both ‘m’ and ‘n’ must be greater than 2 and less than 10. Allow
the user to input integers into this matrix. Perform the following tasks
on the matrix:
▪ Display the original matrix.
▪ Sort each row of the matrix in ascending order using any standard
sorting technique.
▪ Display the changed matrix after sorting each row.
Test your program for the following data and some random data:
Example : INPUT: m = 4
n=3
Enter elements of matrix:
11 -2 3
5 16 7
9 0 4
3 1 8
OUTPUT: Original Matrix:
11 -2 3
5 16 7
9 0 4
3 1 8
Matrix after sorting rows:
-2 3 11
5 7 16
0 4 9
1 3 8
45 | P a g e
SOLUTION:
import java.util.*;
public class Sort
{ //start of class
public static void main()
{ //start of main()
int m,n,tmp=0;
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number of rows and columns for the
array:");
m=sc.nextInt();
n=sc.nextInt();
if(m>2 && m<10 && n>2 && n<10)
{
int arr[][]= new int[m][n];
System.out.println("Enter "+(m*n)+" elements for the array:");
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
arr[i][j]= sc.nextInt();
System.out.println();
System.out.println("INPUT MATRIX:");
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
System.out.print(arr[i][j]+" ");
System.out.println();
for(int k=0; k<m; k++) //sorting of elements
46 | P a g e
for(int i=0; i<m; i++)
for(int j=0; j<n-i-1; j++)
if( arr[k][j+1]<arr[k][j])
{
tmp=arr[k][j];
arr[k][j]=arr[k][j+1];
arr[k][j+1]=tmp;
}
System.out.println();
System.out.println("OUTPUT MATRIX:");
for( int i=0; i<m; i++)
for(int j=0; j<n; j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
else
System.out.println("INVALID INPUT");
} //end of main()
} //end of class
47 | P a g e
8. Write a program to declare a square matrix A[ ] [ ] of order (M x M)
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) Check if the given matrix is Symmetric or not.
A square matrix is said to be Symmetric, if the element of the ith row
and jth column is equal to the element of the jth row and ith column.
Test your program with the sample data and some random data:
INPUT : M = 3
123
245
356
OUTPUT :
ORIGINAL MATRIX
123
245
356
SOLUTION:
import java.util.*;
public class Symmetric
{ //start of class
public static void main(String[] args)
{ //start of main()
int m,flag=0;
48 | P a g e
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number of rows for the array:");
m=sc.nextInt();
if(m>2 && m<10)
{
int arr[][]= new int[m][m];
System.out.println("Enter "+(m*m)+" elements for the array:");
for(int i=0; i<m; i++)
for(int j=0; j<m; j++)
arr[i][j]= sc.nextInt();
System.out.println();
System.out.println("INPUT MATRIX:");
for(int i=0; i<m; i++)
for(int j=0; j<m; j++)
System.out.print(arr[i][j]+" ");
System.out.println();
for(int i=0; i<m; i++) //to check whether the matrix is
for(int j=0; j<m; j++) Symmetric or not
if(arr[i][j]== arr[j][i])
flag++;
if(flag==(m*m))
System.out.println("SYMMETRIC MATRIX");
else
System.out.println("UNSYMMETRIC MATRIX");
}
else
49 | P a g e
System.out.println("INVALID INPUT");
} //end of main()
} //end of class
50 | P a g e
9. Write a program to create a square matrix of m*m order and find out
the sum of boundary and non boundary elements of the matrix.
SOLUTION:
import java.util.*;
public class Sum
{ //start of class
public static void main(String[] args)
{ //start of main()
int m,sb=0,snb=0;
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number of rows for the array:");
m=sc.nextInt();
int arr[][]= new int[m][m];
System.out.println("Enter "+(m*m)+" elements for the array:");
for(int i=0; i<m; i++)
for(int j=0; j<m; j++)
arr[i][j]= sc.nextInt();
System.out.println();
System.out.println("INPUT MATRIX:");
for(int i=0; i<m; i++)
{
for(int j=0; j<m; j++)
{
System.out.print(arr[i][j]+" ");
}
51 | P a g e
System.out.println();
}
for(int i=0; i<m; i++) //to calculate sum of
sb= sb+ arr[0][i]; boundary elements
for(int i=0; i<m; i++)
sb= sb+ arr[m-1][i];
for(int i=1; i<m-1; i++)
sb= sb+ arr[i][0];
for(int i=1; i<m-1; i++)
sb= sb+ arr[i][m-1];
for(int i=1; i<m-1; i++)
for(int j=1; j<m-1; j++) //to calculate sum of
snb= snb+ arr[i][j]; non-boundary elments
System.out.println("THE SUM OF BOUNDARY ELEMENTS IS :
"+sb);
System.out.println("THE SUM OF NON-BOUNDARY ELEMENTS
IS : "+snb);
}
}
52 | P a g e
10. Write a Program in Java to fill a square matrix of size ‘n*n” in a
circular fashion (clockwise) with natural numbers from 1 to n*n, taking
‘n’ as input. For example: if n = 4, then n*n = 16, hence the array will be
filled as given below.
SOLUTION:
import java.util.*;
public class Fill
{ //start of class
public static void main(String[] args)
{ //start of main()
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number of rows for the array:");
int n=sc.nextInt();
int c1=0, c2=n-1, r1=0,r2=n-1, t=1;
int arr[][]= new int[n][n];
while(t<=(n*n))
{
for(int i=c1; i<=c2; i++)
arr[r1][i]=t++;
for(int i=r1+1; i<=r2; i++)
53 | P a g e
arr[i][c2]=t++;
for(int i=c2-1; i>=c1; i--)
arr[r2][i]=t++;
for(int i=r2-1; i>r1; i--)
arr[i][c1]=t++;
c1++;
c2--;
r1++;
r2--;
}
System.out.println();
System.out.println("THE RESULTANT ARRAY IS:");
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
System.out.print(arr[i][j]+" \t");
System.out.println();
System.out.println();
} //end of main()
} //end of class
54 | P a g e
11. Write a program to accept a sentence which may be terminated by
either’.’, ‘?’or’!’ only. The words may be separated by more than one
blank space and are in UPPER CASE.
Perform the following tasks:
(a) Find the number of words beginning and ending with a vowel.
(b) Place the words which begin and end with a vowel at the beginning,
followed by the remaining words as they occur in the sentence.
INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW
THAN YOU ARE TODAY.
OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A
VOWEL= 2
A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN
YOU TODAY
SOLUTION:
import java.util.*;
class Vowel
{
boolean isVowel(String w)
{
int l = w.length();
char ch1 = w.charAt(0); // Storing the first character
char ch2 = w.charAt(l-1); // Storing the last character
if((ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U') &&
(ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U'))
{
return true;
}
55 | P a g e
else
{
return false;
}
}
public static void main(String args[])
{
Vowel ob = new Vowel();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence : ");
String s = sc.nextLine();
s = s.toUpperCase();
int l = s.length();
char last = s.charAt(l-1); // Extracting the last character
/* Checking whether the sentence ends with '.' or '?' or not */
if(last != '.' && last != '?' && last != '!')
{
System.out.println("Invalid Input. End a sentence with either '.',
'?' or '!' only");
}
else
{
int bl=0;
int c = 0;
String w = "", a = "", b = "";
s=s+" ";
56 | P a g e
for(int i=0; i<s.length();)
{
bl=s.indexOf(' ',i);
w = s.substring(i,bl); // Extracting words and saving them in w
if(ob.isVowel(w))
{
c++;
a = a + w + " "; // Saving all words beginning and ending with a
} vowel in variable 'a'
else
b = b + w + " "; // Saving all other words in variable 'b'
i=bl+1;
}
System.out.println("OUTPUT : \nNUMBER OF WORDS
BEGINNING AND ENDING WITH A VOWEL = " + c);
System.out.println(a+b);
}
}
}
57 | P a g e
12. Write a Program in Java to input a number and check whether it is an
Evil Number or not.
Evil Number : An Evil number is a positive whole number which has
even number of 1’s in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of
1’s. A few evil numbers are 3, 5, 6, 9….
Design a program to accept a positive whole number and find the binary
equivalent of the number and count the number of 1’s in it and display
whether it is a Evil number or not with an appropriate message. Output
the result in format given below:
INPUT : 15 BINARY EQUIVALENT : 1111 NO. OF 1’s : 4 OUTPUT :
EVIL NUMBER
SOLUTION:
import java.util.*;
public class Evil
{ //start of class
long Convert(int dec) //method to find binary equivalent
{
long bin;
if (dec == 0)
bin = 0;
else
bin = dec % 2 + 10 * (Convert(dec / 2));
return bin;
}
public static void main(String args[])
{ //start of main()
58 | P a g e
Evil ob= new Evil();
int c=0,n;
long i, res;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:");
n=sc.nextInt();
res= ob.Convert(n);
for(i=res; i>0; i=i/10)
{
if((i%10)==1)
c++;
}
if(c%2==0)
System.out.println(n+" is an Evil number");
else
System.out.println(n+" is not an Evil number");
} //end of main()
} //end of class
59 | P a g e
13. 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:
INPUT : THE SKY IS THE LIMIT.
POTENTIAL : THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63
OUTPUT : IS THE THE SKY LIMIT
SOLUTION:
import java.util.*;
class Deco
{ //start of class
int decode(String w) //method to calculate potential
{
int i,res=0;
for(i=0; i<w.length();i++)
res= res + (int)w.charAt(i);
60 | P a g e
return res;
}
public static void main()
{ //start of main()
Scanner sc=new Scanner (System.in);
Deco ob= new Deco();
String s="",tmp="",w="";
int j=0,c=0,b;
System.out.println("Enter a sentence:");
s=sc.nextLine();
s= s.toUpperCase();
System.out.println("INPUT: "+s);
if(s.endsWith("?")==true ||s.endsWith(".")==true
||s.endsWith("!")==true )
{
s=s.substring(0, s.length()-1)+" ";
for(int i=0; i<s.length();i++)
if(s.charAt(i)==' ')
c++;
String A[]= new String[c];
int arr[]= new int[c];
System.out.println("POTENTIAL: ");
for(int i=0; i<s.length(); )
{
b=s.indexOf(' ',i);
w=s.substring(i,b);
61 | P a g e
A[j]= w;
System.out.println(w+" = "+ob.decode(w));
i=b+1;
j++;
}
62 | P a g e
14. Write a Program in Java to input a 2-D square matrix and check
whether it is a Diagonal Matrix or not.
Diagonal Matrix : A diagonal matrix is a matrix (usually a square
matrix) in which the entries outside the main diagonal are all zero. The
diagonal entries themselves may or may not be zero (but all diagonal
entries cannot be zero).
Example:
5000
0100
0000
0007
SOLUTION:
import java.util.*;
public class Diagonal
{ //start of class
public static void main(String[] args)
{ //start of main()
Scanner sc=new Scanner(System.in);
int i, j,m,cn=0, c=0;
System.out.println("Enter the size of matrix:");
m=sc.nextInt();
int A[][]=new int [m][m];
System.out.println("Enter"+(m*m)+" elements for the array:");
for(i=0; i<m;i++)
for(j=0; j<m;j++)
A[i][j]=sc.nextInt();
63 | P a g e
System.out.println();
System.out.println("INPUT MATRIX:");
for(i=0; i<m;i++)
for(j=0; j<m;j++)
System.out.print(A[i][j]+" ");
System.out.println();
for(i=0; i<m;i++) //to check for Diagonal matrix
for(j=0; j<m;j++)
{
if(i!=j && A[i][j]==0)
c++;
else if(i==j && A[i][j]==0)
cn++;
}
System.out.println();
if(cn<m && c== (m*m)-m)
System.out.println(" DIAGONAL MATRIX ");
else
System.out.println(" NOT A DIAGONAL MATRIX" );
} //end of main()
} //end of class
64 | P a g e
15. Write a program to input a string (word). Convert it into lowercase
letters. Count and print the frequency of each alphabet present in the
string. The output should be given as:
Sample Input: Alphabets
Sample Output: ==========================
Alphabet Frequency
==========================
a 2
b 1
e 1
h 1
l 1
p 1
s 1
t 1
SOLUTION:
import java.util.*;
class Frequency
{ //start of class
public static void main(String[] args)
{ //start of main()
Scanner sc=new Scanner(System.in);
String s="";
int c=0;
System.out.println("Enter a word:");
s=sc.next();
65 | P a g e
s=s.toLowerCase();
System.out.println("INPUT: "+s);
System.out.println("======================================
================");
System.out.println(" ALPHABETS \t\t\t FREQUENCY");
System.out.println("======================================
================");
for(int j=97; j<=122; j++)
{
c=0;
for(int i=0; i<s.length(); i++)
{
char ch= s.charAt(i);
if(ch==(char)j)
c++;
}
if(c!=0)
System.out.println("\t"+(char)j+"\t\t\t\t"+c);
}
} //end of main()
} //end of class
66 | P a g e
16. Write a program to check whether a number is prime or not using
recursive method.
SOLUTION:
import java.util.*;
public class Prime
{ //start of class
int isprime(int n, int i) //method to check prime number
{
if(i<n)
{
if(n%i!=0)
return isprime(n, i+1);
else
return 0;
}
return 1;
}
public static void main(String args[])
{ //start of main()
Prime ob= new Prime();
int n;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:");
n=sc.nextInt();
67 | P a g e
if( ob.isprime(n,2)==1)
System.out.println(n+" is a Prime number");
else
System.out.println(n+" is not a Prime number");
} //end of main()
} //end of class
68 | P a g e
17. Write a program to print first ‘n’ fibonacci numbers using recursive
method.
SOLUTION:
import java.util.*;
public class Fibonacci
{ //start of class
public static int fibonacciRecursion(int n)
{ //method to generate Fibonacci series
if(n == 0)
return 0;
if(n == 1 || n == 2)
return 1;
else
return fibonacciRecursion(n-2) + fibonacciRecursion(n-1);
}
public static void main(String args[])
{ //start of main()
Scanner sc= new Scanner (System.in);
System.out.println("Enter a value for 'n': ");
int n=sc.nextInt();
System.out.print("Fibonacci Series of "+n+" terms: ");
for(int i = 0; i < n; i++)
System.out.print(fibonacciRecursion(i) +" ");
} //end of main()
} //end of class
69 | P a g e
18. Write a program to print the greatest common divisor of 2 numbers
using recursive method.
SOLUTION:
import java.util.*;
public class GCD
{ //start of class
int gcd(int a, int b)
{ //method to calculate greatest common divisor
if(a%b==0)
return b;
else
return gcd(b, a%b);
}
public static void main(String args[])
{ //start of main()
GCD ob= new GCD();
int a,b,g=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 2 numbers:");
a=sc.nextInt();
b=sc.nextInt();
if(a>b)
g=ob.gcd(b,a);
else
g=ob.gcd(b,a);
70 | P a g e
System.out.println("THE GREATEST COMMON DIVISOR IS "+g);
} //end of main()
} //end of class
71 | P a g e
19. Write a program to print the reverse of a string using recursive
method.
SOLUTION:
import java.util.*;
public class REV
{ //start of class
String str="", revstr="";
void getstr(String S)
{
str=S;
}
void reverse( int i) //method to reverse the string
{
if(i<str.length())
{
revstr= str.charAt(i)+revstr;
reverse(++i);
}
}
void display()
{
System.out.println("ORIGINAL STRING: "+str);
System.out.println("REVERSED STRING: "+revstr);
}
72 | P a g e
public static void main(String []args)
{ //start of main()
REV ob= new REV();
Scanner sc=new Scanner(System.in);
String s;
System.out.println("Enter a string:");
s=sc.next();
ob.getstr(s);
ob.reverse(0);
ob.display();
} //end of main()
} //end of class
73 | P a g e
20. Perform Binary search using recursive method.
SOLUTION:
import java.util.*;
class Binary
{ //start of class
int A[]= new int [10];
void fillArray() //to accept elements of array
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 elements for the array:");
for(int j=0; j<10; j++)
{
A[j]= sc.nextInt();
}
}
int binSearch(int l, int u, int v) //performing binary search
{
int mid= (l+u)/2;
if(A[mid]==v)
return (1);
else if(l>u)
return (-1);
else if(A[mid]>v)
return binSearch(l,mid-1,v);
else
74 | P a g e
return binSearch(mid+1,u,v);
}
void display()
{
System.out.println("INPUT MATRIX:");
for(int i=0; i<10; i++)
{
System.out.print(A[i]+" ");
}
}
public static void main(String[] args)
{ //start of main()
Binary ob= new Binary();
Scanner sc=new Scanner(System.in);
ob.fillArray();
System.out.println("Enter the number to be searched:");
int v=sc.nextInt();
ob.display();
System.out.println();
if(ob. binSearch(0,9,v)==1)
System.out.println(v+" is found");
else
System.out.println(v+" is not found");
} //end of main()
} //end of class
75 | P a g e
OUTPUT
76 | P a g e
1.
2.
77 | P a g e
3.
4.
78 | P a g e
5.
6.
79 | P a g e
7.
8.
80 | P a g e
9.
10.
81 | P a g e
11.
12.
13.
82 | P a g e
14.
15.
83 | P a g e
16.
17.
18.
19.
84 | P a g e
20.
85 | P a g e
VARIABLE
DESCRIPTION
86 | P a g e
1.
VARIABLE DATATYPE PURPOSE
s int Stores sum of digits of prime factors
i int Loop variable
x int Reference variable
sd int Stores sum of digits of number
n int Stores the number
2.
VARIABLE DATATYPE PURPOSE
arr int Array name
i int Loop variable
j int Loop variable
nn int Stores size of array
n int Stores the value of nn
3.
VARIABLE DATATYPE PURPOSE
Adno int Array name
i int Loop variable
l int Stores lower limit of array
u int Stores upper limit of array
v int Stores the number to be searched
mid int Stores the middle index of array
4.
VARIABLE DATATYPE PURPOSE
A int Array name
i int Loop variable
j int Loop variable
str String Stores the string
tmp String Used while sorting
b int Stores the index of blank space
count int Stores the number of words
87 | P a g e
5.
VARIABLE DATATYPE PURPOSE
p int Reference variable
i int Loop variable
pc int Counter variable
c int Counter variable
cn int Stores a copy of the number
flag int Counter variable
res int Stores the result
n int Stores the number
6.
VARIABLE DATATYPE PURPOSE
flag int Counter variable
n int Stores the number
A int Array name
i int Loop variable
j int Loop variable
7.
VARIABLE DATATYPE PURPOSE
arr int Array name
i int Loop variable
j int Loop variable
k int Loop variable
m int Stores row size
n int Stores column size
tmp int Used while sorting
8.
VARIABLE DATATYPE PURPOSE
flag int Counter variable
m int Stores the array size
arr int Array name
i int Loop variable
j int Loop variable
88 | P a g e
9.
VARIABLE DATATYPE PURPOSE
sb int Stores sum of boundary elements
snb int Stores sum of nonboundary elements
m int Stores the array size
arr int Array name
i int Loop variable
j int Loop variable
10.
VARIABLE DATATYPE PURPOSE
arr int Array name
i int Loop variable
t int Stores the natural numbers
n int Stores row size
c1 int Stores lower limit of column
c2 int Stores upper limit of column
r1 int Stores lower limit of row
r2 int Stores upper limit of row
11.
VARIABLE DATATYPE PURPOSE
s String Stores the string
a String Used while sorting
b String
w String Stores the word
l int Loop variable
i int Loop variable
bl int Stores index of blank space
c int Counter variable
ch1 char Stores the first character of each word
ch2 char Stores the last character of each word
last char Stores the last character of string
89 | P a g e
12.
VARIABLE DATATYPE PURPOSE
bin long Stores the binary equivalent
i long Loop variable
res long Stores the result of binary conversion
n int Stores the number
dec int Reference variable
c int Counter variable
13.
VARIABLE DATATYPE PURPOSE
s String Stores the string
tmp String Used while sorting
w String Stores the word
i int Loop variable
j int Loop variable
k int Loop variable
b int Stores upper limit of column
c int Counter variable
res int Stores the potential
A String String array
arr int Integer array
14.
VARIABLE DATATYPE PURPOSE
A int Array name
i int Loop variable
j int Loop variable
m int Stores size of array
cn int Counter variable
c int Counter variable
15.
VARIABLE DATATYPE PURPOSE
c int Counter variable
ch char Stores the alphabets
s String Stores the string
i int Loop variable
90 | P a g e
j int Loop variable
16.
VARIABLE DATATYPE PURPOSE
n int Stores the number
i int Stores the factors
17.
VARIABLE DATATYPE PURPOSE
n int Stores the range
i int Loop variable
18.
VARIABLE DATATYPE PURPOSE
a int Stores the number
b int Stores the number
g int Stores the greatest common divisor
19.
VARIABLE DATATYPE PURPOSE
str String Stores the original string
revstr String Stores the reversed string
S String Stores the string entered by user
i int Reference variable
20.
VARIABLE DATATYPE PURPOSE
A int Array name
i int Loop variable
l int Stores lower limit of array
u int Stores upper limit of array
v int Stores the number to be searched
mid int Stores the middle index of array
91 | P a g e