Computer Project
Computer Project
PROJECT
SESSION (2019-20)
1 ARRAY 4 - 21
PROGRAMS
2 OBJECT PASSING 21 - 41
PROGRAMS
3 NUMBER SYSTEM 41 - 60
PROGRAMS
4 INHERITANCE 61 - 66
5 DATA STRUCTURE 67 - 78
6 RECURSION 78 - 89
PROGRAMS
7 STRING 90-108
PROGRAMS
SIGNATURE OF SIGNATURE OF
INTERNAL EXAMINER EXTERNAL EXAMINER
__________________ _________________
ARRAY:
QUESTION 1:
//to search an element using linear searching
ALGORITHM
Step 1: Import the input package.
Step 2: Make the main method
Step 3: Use suitable variables to enter data and the number to be searched;
int n,I,a[]
Step 4: Input numbers using for(i=0;i<a.length;i++)
Step 5: Accept the number to be searched in n
Make a copy of the number in another variable (key)
Step 6: Run a for loop from 0 to a.length increasing by one
Step 7: If a[i]==key print found with appropriate message and position (pointer
+1)
Else print not found
PROGRAM
import java.util.*;
class linearsearch
for(i=0;i<a.length;i++)
a[i] = sc.nextInt();
//inputting key
for(i=0;i<a.length;i++)
if(a[i]==key)
System.exit(0);
OUTPUT OF PROGRAM
/*
Enter size : 3
Enter Key :4
*/
QUESTION 2:
//to display magical matrix of given size
ALGORITHM
Step 1: Import input package and create main method
Step 2: Enter size in int n and initialize array a[][]= new int[n][n]
Int I; int r=0; int c=n/2,r1,c1;
Step 3: run for loop from I=1 till I<=n*n
ar[r][c]=I and r1=r , c1=c
Step 4: Reduce r (r - -) and increase c (c++)
Step 5: Check if r=-1 then r=n-1
Check if c>n-1 then c=0
Step 6: Check if ar[r][c]!=0
r=r1 and c=c1
Increase r (r++)
Step 7: Check if r>n-1 then r=0
And close the for loop
Step 8: Display the resultant matrix using for loop
PROGRAM
import java.util.*;
class magicalodd
//inputting size
int n = sc.nextInt();
for(i=1;i<=n*n;i++)
{
a[r][c] = i;
r1=r;c1 = c;
r--;
c++;
if(r==-1)
r = n-1;
if(c>n-1)
c = 0;
if(a[r][c]!=0)
r = r1;
c = c1;
r++;
if(r>n-1)
r = 0;
for(r=0;r<n;r++)
for(c=0;c<n;c++)
System.out.print("\t"+a[r][c]);
System.out.println();
OUTPUT OF PROGRAM
/*
Enter size : 3
MAGICAL MATRIX :
8 1 6
3 5 7
4 9 2
*/
QUESTION 3:
Create a class unit to take an array from the user and check if it is a unit matrix
or not and display the message accordingly.
ALGORITHM
Step 1: crate a class unit and declare variables.
Step 5: define a function check() to check whether array is unit matrix or not by
checking the left diagonal (i==j) for equality with 1 and other elements for
equality with 0 using a flag and the display message accordingly.
Step 6: create a main method to make an object and call the above functions.
End of algorithm.
PROGRAM
import java.util.*;
class unit
int ar[][],i,j,m;
public unit(int a)
m=a;
ar=new int[m][m];
System.out.println("enter");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
ar[i][j]=sc.nextInt();
for(i=0;i<m;i++)
for(j=0;j<m;j++)
System.out.print(ar[i][j]);
}System.out.println();
int flag=-1;
for(i=0;i<m;i++)
for(j=0;j<m;j++)
if(i==j)
{
if(ar[i][j]!=1)
flag=1;
else if(ar[i][j]!=0)
flag=1;
if(flag==-1)
System.out.println("unit matrix");
else
ob.input();
ob.show();
ob.check();
OUTPUT
enter
100
010
001
100
010
001
unit matrix
enter
123
456
789
123
456
789
QUESTION 4:
A class LTMatrix is defined to check whether a double dimensional array is a
lower triangular or not
Lower triangular matrix is the square matrix in which all the entries above the
main diagonal are zero for example
9000
5100
7840
6052
Data members
Member methods
boolean check():to return true if the matrix is lower triangular else return false
void print():to print whether the entered matrix is lower triangular or not
ALGORITHM
Step 1: Import input package and declare ar[][],I,j,r,c with int data type.
Step 2: Create a parameterized constructor to initialize r and c ( rows and
columns) and array with r and c
Step 3: Create function accept() and accept the matrix using two for loops for
rows and columns
Step 4: Create the function check() with return type Boolean. Take an
int variable flag with initial value 0
Step 5: Run outer for loop from i=0 till i<r-1
Run inner for loop from j=i+1 till j<c
Step 6: Check if ar[i][j]!=0
If true then flag=1 and break
Step 7: Check flag==1
If true then return false else return true
Step 8: Create function print(). Call function check() in a variable of boolean
data type
Step 9: Check is variable holds true then print matrix is lower triangular else
print matrix is not lower triangular.
Step 10: Make the main method and make objects of functions accept() and
print()
PROGRAM
import java.util.*;
import java.util.Scanner;
class LTMatrix
int ar[][];
int r,c;
int i,j;
LTMatrix(int a,int b)
r=a;
c=b;
ar=new int[r][c];
void accept()
{
System.out.println("Enter a matrix");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
ar[i][j]=sc.nextInt();
boolean check()
int flag=0;
for(i=0;i<r-1;i++)
for(j=i+1;j<c;j++)
if(ar[i][j]!=0)
flag=1;
break;
}
if(flag==1)
return false;
else
return true;
void print()
boolean a=check();
if(a==true)
else
obj.accept();
obj.print();
}
OUTPUT OF PROGRAM
/*
Enter a matrix
1000
5100
7840
6052
*/
QUESTION 5:
Define class 'duplicate' with following specifiations :-
Data Members :-
Member Functions :-
ALGORITHM
Step 1: define class duplicate.
if(arr[i]!=arr[i+1])
arr[j++] = arr[i];
Step 5: call all above functions in main method() and end the main method.
PROGRAM
import java.io.*;
class duplicate
int arr[],size;
size=sc.nextInt();
arr[i] = sc.nextInt();
void packList()
int i,j=0;
for(i=0;i<size-1;i++)
if(arr[i]!=arr[i+1])
arr[j++] = arr[i];
arr[j++] = arr[i];
size = j;
void dispList()
System.out.print("Array is : ");
for(int i=0;i<size;i++)
System.out.print(arr[i]+" ");
System.out.println();
{
duplicate b=new duplicate();
b.readList();
b.dispList();
b.packList();
b.dispList();
OUTPUT OF PROGRAM
/*
Enter size : 4
Enter elements :
3
2
Array is : 4 3 2 2
Array is : 4 3 2
*/
OBJECT PASSING:
QUESTION 6:
Define a class 'Angle' with following classification :-
Data Member :-
Member Function :-
ALGORITHM
Step 1: define a class Angle and declare variables deg,min,sec.
Step 2: create a parameterized constructor to assign values to deg sec and min.
Step 5: create a function isValidAngle to check the validity of angle and returns
0 if(deg<0 || min>59 || min<0 || sec>59 || sec<0) else returns 1.
Step 7: create a function Angle add(Angle a) which returns an Angle with sum
of 'a' and calling object.
Step 9: Create a main method and create an object to call the above functions.
End of algorithm.
PROGRAM
import java.util.*;
class Angle
int deg,min,sec;
deg = d;
min = m;
sec = s;
deg = sc.nextInt();
min = sc.nextInt();
sec = sc.nextInt();
}
void showAngle()
System.out.print("\n\n\tAngle Is :"+deg+
(char)248+min+"'"+sec+"\"");
int isValidAngle()
return 0;
else
return 1;
int compare(Angle a)
long s1 = deg*3600+min*60+sec,s2=a.deg*3600+a.min*60+a.sec;
if(s1>s2)
return 1;
if(s1<s2)
return -1;
else
return 0;
}
Angle add(Angle a)
ret.sec = sec+a.sec;
ret.min = min+a.min+ret.sec/60;
ret.deg = deg+a.deg+ret.min/60;
ret.sec %= 60;
ret.min %= 60;
return ret;
Angle Diff(Angle a)
int s = (deg*3600+min*60+sec)-(a.deg*3600+a.min*60+a.sec);
if(s<0)
s = s*-1;
a.readAngle();
b.readAngle();
a.showAngle();
b.showAngle();
a.Diff(b).showAngle();
OUTPUT OF PROGRAM
/*
Angle Is :10ø20'30"
Angle Is :20ø30'40"
Angle Is :10ø10'10"
*/
QUESTION 7:
Define a class 'Fraction' with following classification :-
Data Member :-
Member Function :-
factor
object as a Fraction
ALGORITHM
Step 1: define a class Fraction and declare variables num den.
Step 4: define a function int hcf(int a,int b) to return HCF of a & b which uses
recursion.
Step 5: define a function int lcm(int a,int b) to return LCM of a & b int c=a and
int d=b in which a loop runs till a is not equal to b and if a<b then a=a+c else
b=b+d close the loop then return a.
Step 6: define a function void reduce() which calculates the hcf of numerator
and denominator and then divides them both with the hcf to get the reduced
fraction.
Step 7: define a function Fraction addFraction(Fractionf) which creates an
object of class and add in it the fractions of passed object and calling object by
multiplying the fractions with lcm of both denominators and returns the
created object.
Step 10: create the main method to create two object to call the above
functions. End of algorithm.
PROGRAM
import java.util.*;
class Fraction
int num,den;
void readFraction()
num = sc.nextInt();
while(den==0)
den = sc.nextInt();
}
if(den<0)
num = num*-1;
den = den*-1;
void showFraction()
if(den==0)
System.out.print("\n\tFraction Is Undefined");
else
System.out.print("\n\tFraction Is :"+num);
if(den!=1)
System.out.print("/"+den);
int c=a,d=b;
while(a!=b)
{ if(a<b)
a = a+c;
else
b = b+d; }
return a;
If(a%b==0)
return b;
else
return(hcf(b,a%b));
void reduce()
num = num/cut;
den = den/cut;
Fraction addFraction(Fraction f)
ret.num = num*(lcm(den,f.den)/den)
+ f.num*(lcm(den,f.den)/f.den);
ret.den = lcm(den,f.den);
ret.reduce();
return ret;
int compare(Fraction f)
if(num*f.den>f.num*den)
return 1;
if(num*f.den<f.num*den)
return -1;
return 0;
a.readFraction();b.readFraction();
a.reduce();a.showFraction();
b.reduce();b.showFraction();
a.addFraction(b).showFraction();
OUTPUT OF PROGRAM
/*
Enter Numerator :1000
Fraction Is :1/3
Fraction Is :2/3
Fraction Is :1
*/
QUESTION 8 :
Define a class 'complex' with following specification :-
Data Member :-
Member Function :-
format
*/
ALGORITHM
Step 1: create a class complex and declare variables real,imag.
Step 8: create a main method and call the above functions using suitable
objects. End of algorithm.
PROGRAM
class complex
double real,imag;
complex()
real = imag = 0;
}
complex(double x,double y)
real = x;
imag = y;
void display()
if(imag>0)
System.out.println(real+"+"+imag+"i\n");
else
System.out.println(real+""+imag+"i\n");
complex add(complex z)
complex sub(complex z)
complex multiply(complex z)
complex divide(complex z)
z = z.multiply(conjugate);
temp=temp.multiply(conjugate);
temp.real /= z.real;
temp.imag /= z.real;
return temp;
a.display();
b.display();
System.out.print("Sum is : ");
a.add(b).display();
System.out.print("Difference is : ");
a.sub(b).display();
System.out.print("Product is : ");
a.multiply(b).display();
System.out.print("Division is : ");
a.divide(b).display();
OUTPUT OF PROGRAM
/*
3.0+2.0i
2.0-3.0i
Sum is : 5.0-1.0i
Difference is : 1.0+5.0i
Product is : 12.0-5.0i
Division is : 0.0+1.0i
*/
QUESTION 9:
Define class 'Vector' with following specifications :-
Data Members :-
a : to store coefficient of i
b : to store coefficient of j
c : to store coefficient of k
Member Functions :-
*/
ALGORITHM
Step 1: Declare the variables a,b and c with int data type.
Step 2: Create a parameterized constructor to initialize variables
this.a=a;this.b=b;this.c=c
Step 3: Create function showVector() and print
(a+”i+”+”+b+”j+”+c+”k”)
Step 4: Create function int dotproduct(Vector z)
Return (a+z.a+b+z.b+c+z.c)
Step 5: Create function add(Vector z) with return type Vector
Return new Vector(a+z.a,b+z.b,c+z.c)
Step 6: Create function crossproduct(Vector z) with return type Vector
Return new Vector(b*z.c-z.b*c,c*z.a-a*z.c,a*z.b-b*z.a)
Step 7: Create main method and make objects of the methods and pass them
as follows-
Vector a=new Vector(1,2,3),b=new Vector(3,4,5);
a.showVector();b.showVector();
System.out.println("Dot Product : "+a.dotproduct(b));
System.out.print("ADD Vector : ");
Vector z=a.add(b);z.showVector();
System.out.print("Cross Product : ");
z=a.crossproduct(b);z.showVector();
PROGRAM
class Vector
int a,b,c;
this.a=a;this.b=b;this.c=c;
void showVector()
int dotproduct(Vector z)
return(a+z.a+b+z.b+c+z.c);
Vector add(Vector z)
{
return new Vector(a+z.a,b+z.b,c+z.c);
Vector crossproduct(Vector z)
class vect
a.showVector();b.showVector();
Vector z=a.add(b);z.showVector();
z=a.crossproduct(b);z.showVector();
Output Of Program
/*
1i + 2j + 3k
3i + 4j + 5k
Dot Product : 18
ADD Vector : 4i + 6j + 8k
*/
NUMBER SYSTEM:
QUESTION 10:
Write a program to input a number and check if its triangular
number or not. A triangular number is the number which is
the sum of the consecutive numbers starting from 1.
EXAMPLE:
INPUT :21 INPUT :19
OUTPUT: OUTPUT:
NUMBER IS TRIANGULAR NUMBER IS NOT
TRIANGULAR
ALGORITHM
Step 1: define a class and declare variable i
Step 4: check if(s==x) f=1 and print number ids triangular if f==1
PROGRAM
import java.util.*;
class Number
int i;
int check(int x)
for(i=1;i<=x;i++)
{
s=s+i;
if(s==x)
f=1;
break;
if(f==1)
return 1;
else
return 0;
void show()
System.out.println("Enter a number");
int n=sc.nextInt();
int a=check(n);
if(a==1)
System.out.println("NUMBER IS TRIANGULAR");
else
OUTPUT OF PROGRAM
/*
Enter a number
21
NUMBER IS TRIANGULAR
Enter a number
19
*/
QUESTION 11:
Write a program to input a number and check if it is unique
and print the number .A number is unique if none of the
digits in a number repeats itself.
ALGORITHM
Step 1: Define a class.
Step 6: create an array a[] of size c and put each digit as a separate element in
the array.
Step 7: run a nested loop in which each element tests equality with all other
elements and if they are equal put flag=1 and break.
Step 8: if flag is 1 then print not a unique number else print a unique number
PROGRAM
class unique
void check(int n)
int m=n;int p;
int i,j,c=0,flag=0;
while(n!=0)
{int k=n%10;
c++;
n=n/10;}
for(i=0,p=m;i<c;i++,p=p/10)
ar[i]=p%10;
for(i=0;i<c-1;i++)
{
for(j=i+1;j<c;j++)
if(ar[i]==ar[j])
flag=1;break;
if(flag==1)
else
OUTPUT OF PROGRAM
/*
Input:4896
Input:4556
*/
QUESTION 12:
Define class 'MyNumber' with following specifications :-
Data Members :-
n - to store no
Member Functions :-
readdata() : to input n
*/
ALGORITHM
Step 1: define a class MyNumber and declare variable n.
Step 4: take out reverse of n and check if it is equal to itself return true else
return false.
Step 6: calculate the sum of digits till it’s a single digit, if the digit is 1 return
true else return false.
Step 7: declare a function automorphic() which checks for automorphic
number.
Step 8: extact the digits of the number and calculate the remainder when
square is divided by j if number is single digit it will divide number by 10 if two
digit then by 100 and so on which would be stored in k, if k is equal to the
number return true else return false.
Step 10 : calculate the sum of squares of each digit an check equality with the
number if true then return true else return false.
Step 11: declare function showall() which calls all the above function and
displays a message accordingly.
Step 12: create a main method and create an object and call readdata() and
showall() in it through the object.
PROGRAM
import java.util.*;
class MyNumber
int n;
void readdata()
System.out.println(“enter a numer”);
N=sc.nextInt();
}
boolean palindrome()
int a=n,b=0;
for(;a>0;b=b*10+a%10,a/=10);
if(n==b)
return true;
else
return false;
boolean magical()
int a=n;
for(;a>9;a=a%10+a/10);
if(a==1)
return true;
else
return false;
boolean automorphic()
int a=n,b=n,j=10,k=0;
for(;a>0;k=(b*b)%j,j*=10,a/=10);
if(k==b)
return true;
else
return false;
boolean armstrong()
int a=n,c=0;
for(;a>0;c+=(int)Math.pow(n%10,3),a/=10);
if(n==c)
return true;
else
return false;
void showall()
if(magical())
if(palindrome())
if(automorphic())
if(armstrong())
System.out.println("no. is armstrong : ");
class number
a.readdata();
a.showall();
} }
OUTPUT OF PROGRAM
/*
Enter no. : 1
no. is magical :
no. is palindrome :
no. is automorphic :
no. is armstrong :
*/
QUESTION 13:
Define class 'GPS' with following specifications :-
Date Members :-
n : to store limit
Member Functions :-
*/
ALGORITHM
Step 1: define a class GPS and declare variables a, r, and n.
Step 3: declare a function which returns the nth term of the GP series by
formula a*r^n-1.
Step 4: declare a function sum() which returns the sum of the series by the
formula a*((r^n)-1)/(r-1)
Step 5: declare function showseries() which displays sum and nth term by
calling above functions.
Step 6: make the main methos and create an object and call the above
functions.
import java.util.*;
class GPS
int a,r,n;
void readdata()
a =sc.nextInt();
r =sc.nextInt();
n =sc.nextInt();
long nThterm(int b)
return a*(int)Math.pow(r,b-1);
long sum()
return a*(int)(Math.pow(r,n)-1)/(r-1);
}
void showseries()
for(int i=1;i<=n;i++)
System.out.print(nThterm(i)+" ");
System.out.println("\nSum : "+sum());
a.readdata();
a.showseries();
OUTPUT OF PROGRAM
/*
Enter limit : 5
2 4 8 16 32
Sum : 62
*/
QUESTION 14:
Define class REMOVEZEROS with following details:
Member methods:
int check(int n):to remove all zeros fom the number and return the number
without zeros
void main(): to input a number and call the above function to execute the
ALGORITHM
Step 1: define class REMOVEZEROES.
Step 3: covert it to String, extract each character and add to new string so
except ‘0’. return no.
Step 4: create main method and object and call the above function.
PROGRAM
import java.io.*;
import java.util.Scanner;
class REMOVEZEROS
{
int check(int n)
String s=Integer.toString(n);
int l=s.length();
String so="";int i;
for(i=0;i<l;i++)
char ch=s.charAt(i);
if(ch=='0')
continue;
else
so=so+ch;
int no=Integer.parseInt(so);
return no;
void main()
int n=sc.nextInt();
int no=check(n);
OUTPUT OF PROGRAM
/*
Enter a number
48090
*/
QUESTION 15:
Define a 'Binomial' class with following specification :-
Data Members :-
n : to store limit
Member Functions :-
ALGORITHM
Step 1: define a class Binomial and declare variables x, a, n.
Step 4: declare a function nCr(int l,int r) which returns nCr of given arguments
by formula l!/((r!)*(l-r)!).
Step 5: declare a function sumseries() which calculates the sume each term by
using formula of nth term in loop (nth tem= nCr(a^n-r)*(b^r))
Step 6: Crate main method and create an object to call the above functions.
End of algorithm.
PROGRAM
import java.util.*;
class Binomial
int x,a,n;
void readdata()
n =sc.nextInt();
x =sc.nextInt();
System.out.print("Enter value of a :");
a =sc.nextInt();
long factorial(int b)
if(b==0 || b==1)
return 1;
return factorial(l)/(factorial(r)*factorial(l-r));
void sumseries()
long sum=0;
for(int i=0;i<=n;i++)
sum += nCr(n,i)*(long)Math.pow(x,n-i)*(int)Math.pow(a,i);
a.readdata();
a.sumseries();
OUTPUT OF PROGRAM
/*
Enter limit : 6
Enter value of x :2
Enter value of a :1
*/
INHERITENCE:
QUESTION 16:
//Define a class 'Account' with two extended classes ‘Simple’ & 13
‘Compound’ for calculating interest :-
ALGORITHM
Step 1: define class account and a constructor to initialize principal and
account number by 0.
Step 4: create another class simple which extend class account using extends
keyword.
Step 7:create a function double interest which calculates interest and returns
it.
Step 14:create a class Account which extends compound and call necessary
methods accordingly.
PROGRAM
import java.io.*;
import java.util.*;
class Account
Account()
accountNumber = 0;
principal = 0;
accountNumber=acn;
principal=np;
}
void display()
System.out.println("Principal : "+principal);
Simple()
time = 0;
rate = 0;
super(acn,np);
time = nt;
rate = nr;
void display()
{
super.display();
System.out.println("Rate : "+rate);
System.out.println("Time : "+time);
System.out.println("Interest : "+interest());
double interest()
return principal*rate*time/100;
Compound()
time = 0;
rate = 0;
super(acn,np);
time = nt;
rate = nr;
}
void display()
super.display();
System.out.println("Rate : "+rate);
System.out.println("Time : "+time);
System.out.println("Interest : "+interest());
double interest()
return principal*(Math.pow(1+rate/100,time)-1);
class testaccount
double p = sc.nextDouble();
System.out.print("Enter rate : ");
int r = sc.nextInt();
int t = sc.nextInt();
if(sc.nextLine().charAt(0)=='S')
a = new Simple(acn,p,t,r);
else
a = new Compound(acn,p,t,r);
a.display();
OUTPUT OF PROGRAM
/*
Enter Acc# : 4
Enter principal : 10
Enter rate : 1
Enter time : 1
Account Number : 4
Principal : 10.0
Rate : 1.0
Time : 1.0
Interest : 0.1
*/
Data Members :-
Member functions :-
*/
ALGORITHM
Step 1: define class stack and a constructor to initialize top by -1 and size.
Step 3: create booleanisFull() to return true if array is full by checking with top
pointer.
Step 4: in void Push(int item) check whether array is full or not if not full
perform
Top++
ar[top]=num.
Top--.
Step 7:create a function display and un a loop from 0 to top and print the
elements.
PROGRAM
import java.io.*;
class Stack
int size,top,a[];
Stack(int s)
size = s;
a = new int[s];
top = -1;
boolean isEmpty()
if(top==-1)
return true;
return false;
boolean isFull()
if(top==size-1)
return true;
return false;
if(isFull())
else
a[++top] = item;
void Pop()
{
if(isEmpty())
else
top--;
void display()
for(int i=top;i>=0;i--)
System.out.print(a[i]+"\t");
System.out.println();
class teststack
do
{
n=Sc.nextInt();
switch(n)
case 1 :
a.Push(Integer.parseInt(in.readLine()));
break;
case 2 :
a.Pop();
break;
case 3 :
a.display();
break;
case 4 :
System.exit(0);
}while(n!=4);
} }
OUTPUT OF PROGRAM
/*
Enter size : 1
1. Insert
2. Delete
3. Display
4. Exit
Enter choice : 1
Enter item : 2
Enter choice : 1
Enter item : 3
Enter choice : 3
Stack is :----> 2
Enter choice : 2
Enter choice : 2
*/
QUESTION 17:
Data Members :-
a[] : array to store elements
Member functions :-
*/
ALGORITHM
Step 1: define class queue and a constructor to initialize rear by -1 and front
by 0.
Step 3: create booleanisFull() to return tue if array is full by checking with top
pointer.
Step 4: in void Push(int item) check whether array is full or not if not full
perform
Rear++;
ar[rear]=num.
Step 5: void pop()
Front++;
Step 7:create a function dispalay and run a loop from 0 to rear and print the
elements.
PROGRAM
import java.io.*;
class Queue
int size,front,rear,a[];
Queue(int s)
boolean isEmpty()
if(front==-1 || front==rear+1)
return true;
return false;
boolean isFull()
if(rear==size-1)
return true;
return false;
if(isFull())
else
if(rear==-1)
rear = front = 0;
else
++rear;
a[rear] = item;
void Pop()
{
if(isEmpty())
else
front++;
void display()
if(front>-1)
for(int i=front;i<=rear;i++)
System.out.print(a[i]+"\t");
System.out.println();
class testqueue
do
n = sc.nextInt();
switch(n)
a.Push(sc.nextInt());
break;
case 2 : a.Pop();
break;
case 3 : a.display();
break;
case 4 : System.exit(0);
}while(n!=4);
}
OUTPUT OF PROGRAM
/*
Enter size : 1
1. Insert
2. Delete
3. Display
4. Exit
Enter choice : 1
Enter item : 2
Enter choice : 1
Enter item : 3
Enter choice : 3
Queue is :----> 2
Enter choice : 2
Enter choice : 2
Enter choice : 1
Enter item : 3
Enter choice : 4
*/
RECURSION
QUESTION 18:
Write a program to print the following Fibonacci series in
string using recursion:
a b ba bab
ALGORITHM
Step 1:define a class fibo ,int n,String c=””;
PROGRAM
import java.io.*;//importing packages
import java.util.Scanner;
class fibo
int n;
String c="";
if(na>n)
{
return;
else
System.out.print(s0+" ");
c=s1+s0;
generate(s1,c,na+1);
void main()
n=sc.nextInt();
generate("a","b",1);
OUTPUT OF PROGRAM
/* Enter value of n
4
a b ba bab */
QUESTION 19:
An integer can be broken into two equal or unequal parts from the middle. For
example 2356 can be broken into 23(left part) or 56(right part). Similarly 56789
broken into 567(left part) and 89(right part). A class is declared Midhalf to
break the number in parts from middle its details are given:
Data Members
Member methods:
Void split() : breaks the number from middle using count and power
Write the main function to create an object and call the member methods to
implement the task
ALGORITHM
Step 1: define class Midhalf and define parameterized constructor
Step 4: define function power(int a,int b) check if(b==0) return 1 else return
a*power(a,b-1)
Step 5:then call the count function and power function to find the halves of the
number
Step 6: define function display() to display the halves of the number
Step 7: define main method to create objects and call the above function
PROGRAM
import java.util.*;
import java.util.Scanner;
int num;
int left;
int right;
num=m;
int count(int n)
if(n==0)
return 0;
else
return 1+count(n/10);
}
int power(int a,int b)
if(b==0)
return 1;
else
return a*(power(a,b-1));
void split()
int no=num;
int s=count(no);
int p=power(10,(s/2));
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
obj.split();
obj.display();
}//end of main
}//end of class
OUTPUT OF PROGRAM
/*
Enter a number
2356
Enter a number
56789
*/
QUESTION 20:
Define a class 'Dec_Bin' with following specifications :-
Data Members :-
Member Functions :-
*/
ALGORITHM
PROGRAM
import java.util.*;
class Dec_Bin
{
int n,s,i;
n =sc.nextInt();
recursive(n);
display();
void recursive(int n)
if(n>0)
s = s+(n%2)*(int)Math.pow(10,i++);
recursive(n/2);
void display()
}
class dec_bin
a.getdata();
OUTPUT OF PROGRAM
/*
Enter a no. : 14
Decimal no. : 14
*/
QUESTION 21:
/ / RECURSIVE METHOD to interchange even with odd and vice-versa
ALGORITHM
Step 1: define class eve_odd and input the size of the array and accept the
values of the array
Step 2: declare function void arrange (int ar[], int size, int eve, int odd)
Step 3: update eve++ while eve<size and ar[eve]%2!=0 or update odd++ while
odd<size and ar[odd]%2==0
PROGRAM
import java.util.*;
class eve_odd
//inputting size
for(i=0;i<size;i++)
arr[i] =sc.nextInt();
System.out.print(arr[i]+"\t");
//caliing function
arrange(arr,size,0,0);
for(i=0;i<size;i++)
System.out.print(arr[i]+"\t");
System.out.println();
eve++;
odd++;
swap(arr,eve,odd);
//swapping nos
int t = arr[i];
arr[i] = arr[j];arr[j] = t;
OUTPUT OF PROGRAM
/*
Enter size : 2
Initial Array :
1 2
Sorted Array :
2 1 */
STRINGS
QUESTION22:
Write a program to input 5 names and print whether they are
magic string or not. A magic string is the string in which the
numbers of vowel are equal to number of consonant.
ALGORITHM
Step 1: define a class magic , define function int ma(String w)
Step 3: run a loop till the length of the string and extract characters in char ch
Step 5: after terminating for loop check if(vo==co) and return 1 else return 0
Step 7: call the ma(String ) function int x=ma(ar[i]) and print the element of
array if(x==1)
PROGRAM
import java.io.*;
import java.util.Scanner;
class magic
int ma(String w)
{
int j;
int vo=0;
int co=0;
String v="AEIOUaeiou";
int l=w.length();
for(j=0;j<l;j++)
char ch=w.charAt(j);
if(v.indexOf(ch)!=-1)
vo++;
else
co++;
if(vo==co)
return 1;
else
return 0;
void main()
int i=0;
for(i=0;i<5;i++)
ar[i]=sc.nextLine();
for(i=0;i<5;i++)
int x=ma(ar[i]);
if(x==1)
System.out.println(ar[i]);
} }
OUTPUT OF PROGRAM
/* Enter 5 names
AHILY
JANE
BEN
OSHI
SABA
JANE
OSHI
SABA */
QUESTION 23:
//to display diamond pattern of string
ALGORITHM
Step 3: run a for loop from i=-s.length() and another loop with j=-s.length() till
i<s.length()and j<s.length() with k=0;
Step 5:print space ,terminate for loop of j then println() aand terminate loop of
i
PROGRAM
import java.util.*;
class diamondstr
for(i=-s.length();i<s.length();i++)
k = 0;
for(j=-s.length();j<s.length();j++)
if(Math.abs(i)+Math.abs(j)<s.length())
System.out.print(s.charAt(k));
k = j<0?++k:--k;
System.out.println();
}//end of main
}//end of class
OUTPUT OF PROGRAM
/*
Pattern is :
aba
abcba
aba
QUESTION 24:
Write a program to input a string and to return a string by recursion by
removing all vowels present in the string and all other character as it is
Example:
Output:”Dsply th rslt”
ALGORITHM
PROGRAM
import java.io.*;
import java.util.Scanner;
{String str;
String v="AEIOUaeiou";
String recurse(int I,String s)// function to return string without vowels
if(i==str.length()-1)
return s;
else
char ch=str.charAt(i);
if(v.indexOf(ch)!=-1)
return recurse(i+1,s);
else
s=s+ch;
return recurse(i+1,s);
void main()
String sen="";
str=sc.nextLine();
sen=recurse(0);
System.out.println(sen);
}//end of main
}//end of class
OUTPUT OF PROGRAM
/*
Enter a sentence
Dsply th rslt
*/
QUESTION 25:
Define a class 'Encrypt' which following specifications :-
Data Members :-
Member Functions :-
*/
ALGORITHM
Step 2: define function readdata () to input a string and print original string
alongwith encrypted and decrypted string
Step 4: start another loop with k=0; and run until k<str.length()
PROGRAM
import java.util.*;
class Encrypt
String str="";
void readdata()
display(str);
encrypt();
decrypt();
void encrypt()
s = str.substring(j,i);j=i+1;
for(k=0;k<s.length();k++)
{
r = s.charAt(k)+s.length();
if(r>90&&s.charAt(k)<='Z')
r = r-26;
if(r>122&&s.charAt(k)<='z')
r = r-26;
s1 = s1+(char)r;
s1 = s1+" ";
display(s1);
void decrypt()
s = str.substring(j,i);j=i+1;
for(k=0;k<s.length();k++)
r=s.charAt(k)-s.length();
if(r<65&&s.charAt(k)>='A')
r = r+26;
if(r<97&&s.charAt(k)>='a')
r = r+26;
s1 = s1+(char)r;
s1 = s1+" ";
display(s1);
System.out.println(st);
class code
a.readdata();
OUTPUT OF PROGRAM
/*
Enter a string : A a Z z
Original String : A a Z z
Encrypt String : B b A a
Decrypt String : Z z Y y
QUESTION 26:
A class Alpha is declared to arrange the letters of a word in ascending order of
alphabets
Data members
Member methods
Write the main method to print the original word and the arranged word
ALGORITHM
Step 1: define class Alpha and constructor Alpha()
Step 4: print the word and declare main method to form object Alpha ob=new
Alpha ();
Step 5: call all above functions and end the main
PROGRAM
import java.io.*;
import java.util.Scanner;
class Alpha
String str;
Alpha()
str="";
void accept()
System.out.println("Enter a string");
str=sc.nextLine();
void arrange()
int l=str.length();
char ar[]=new char[l];
int i,j;
for(i=0;i<l;i++)
ar[i]=str.charAt(i);
for(i=0;i<l;i++)
for(j=0;j<l-i-1;j++)
if(ar[j]>ar[j+1])
char ch=ar[j];
ar[j]=ar[j+1];
ar[j+1]=ch;
str="";
for(i=0;i<l;i++)
str=str+ar[i];
}
}
void display()
obj.accept();
obj.display();
obj.arrange();
obj.display();
OUTPUT OF PROGRAM
*/
Enter a string
"Computer"
Enter a string
Statistics
The word is Statistics
*/