0% found this document useful (0 votes)
0 views108 pages

Computer Project

This document outlines a computer project completed during the 2019-20 session, detailing various programming topics such as arrays, object passing, number systems, inheritance, data structures, recursion, and strings. It includes algorithms and Java programs for tasks like linear searching, generating magical matrices, checking unit matrices, and identifying lower triangular matrices. The project also features a class for handling angles and removing duplicates from arrays, showcasing practical applications of programming concepts.

Uploaded by

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

Computer Project

This document outlines a computer project completed during the 2019-20 session, detailing various programming topics such as arrays, object passing, number systems, inheritance, data structures, recursion, and strings. It includes algorithms and Java programs for tasks like linear searching, generating magical matrices, checking unit matrices, and identifying lower triangular matrices. The project also features a class for handling angles and removing duplicates from arrays, showcasing practical applications of programming concepts.

Uploaded by

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

COMPUTER

PROJECT

SESSION (2019-20)

name & class sec


ACKNOWLEDGEMENT
This project has been completed by
the combined effort of my teacher, family and friends.
Firstly my gratitude is towards the Divine power for
giving me the opportunity to work on this project.
Secondly I would present my gratitude towards my
computer teacher Mr. Sumit Kapoor Sir, who passed on
all the relevant information of programming required
to complete the project. He helped me in the editing of
the project. His motivation also played a significant role
in the completion of the project.
INDEX
SERIAL PAGE
NUMBER TOPIC NUMBER

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

public static void main()


{

Scanner sc=new Scanner(System.in);

//inputting size & elements

System.out.print("\n\n\tEnter size : ");

int i,a[] = new int[Integer.parseInt(in.readLine())];

System.out.print("\n\tStart Entering elements :\n");

for(i=0;i<a.length;i++)

a[i] = sc.nextInt();

//inputting key

System.out.print("\n\tEnter Key :");

int key = sc.nextInt();

//searching for elements

for(i=0;i<a.length;i++)

if(a[i]==key)

System.out.println("\n\tKey at position : "+(i+1));

System.exit(0);

System.out.print("\n\tKey doesn't occur");

OUTPUT OF PROGRAM
/*

Enter size : 3

Start Entering elements :

Enter Key :4

Key doesn't occur

*/

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

public static void main()

Scanner sc=new Scanner(System.in);

//inputting size

System.out.print("\n\tEnter size : ");

int n = sc.nextInt();

int a[][] = new int[n][n],i,r=0,c=n/2,r1,c1;

//genarating magical array

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;

//displaying magical matrix

System.out.println("\n\tMAGICAL MATRIX : ");

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 2: create a parameterized constructor to assign the size of array to m and


initialize array.

Step 3: take input from user in method input().


Step 4: define a function show to display the matrix which was taken as input.

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];

public void input()

Scanner sc=new Scanner(System.in);

System.out.println("enter");

for(i=0;i<m;i++)

for(j=0;j<m;j++)

{
ar[i][j]=sc.nextInt();

public void show()

for(i=0;i<m;i++)

for(j=0;j<m;j++)

System.out.print(ar[i][j]);

}System.out.println();

public void check()

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

System.out.println("not unit matrix");

public static void main(int n)

unit ob=new unit(n);

ob.input();

ob.show();

ob.check();

OUTPUT
enter

100

010

001

100

010

001

unit matrix
enter

123

456

789

123

456

789

not unit matrix

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

ar[][]:double dimensional array

r:integer to store rows

c:integer to store columns

Member methods

LTMatrix(int a,int b):parameterized constructor

void accept():enter the matrix

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

Write a main function to carry out the above program code

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()
{

Scanner sc=new Scanner(System.in);

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)

System.out.println("matrix is lower triangular");

else

System.out.println("matrix is not lower triangular");

public static void main()

LTMatrix obj=new LTMatrix(4,4);

obj.accept();

obj.print();

}
OUTPUT OF PROGRAM
/*

Enter a matrix

1000

5100

7840

6052

matrix is lower triangular

*/

QUESTION 5:
Define class 'duplicate' with following specifiations :-

Data Members :-

arr[] : to store sorted elements

size : to store size of array

Member Functions :-

void readList() : to enter elements

void packList() : to remove duplicates

void dispList() : to display array the array

ALGORITHM
Step 1: define class duplicate.

Step 2: input elements in function void readlist().

Step 3: in function void packlist() perform,to remove duplicates.

if(arr[i]!=arr[i+1])

arr[j++] = arr[i];

Step 4: print the elements of the array in void displaylist().

Step 5: call all above functions in main method() and end the main method.

Step 6:end the class

PROGRAM
import java.io.*;

class duplicate

int arr[],size;

void readList()throws Exception

Scanner sc=new Scanner(System.in);

System.out.print("Enter size : ");

size=sc.nextInt();

System.out.println("Enter elements : ");

arr = new int[size];


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

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();

public static void main()

{
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 :-

deg : an integer to degrees of angle

min : an integer to minutes of angle

sec : an integer to seconds of angle

Member Function :-

Angle() : Default constructor

Angle(int d,int m,int s) : Parameterised constructor

void readAngle() : to input angle

void showAngle() : to display angle

int isValidAngle : to return 1 if angle is valid else 0

Angle add(Angle a) : to return an Angle with sum of 'a' and


calling object

Angle Diff(Angle a) : to return an Angle with difference of 'a'

and calling object

int compare(Angle a) : to return 1 if calling angle is greater or

else -1 if 'a' greater otherwise 0


*/

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 3: create a fuction readdata() to take input.

Step 4: create a function showAngle() to display the angle.

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 6: create a function int compare(Angle e) Which return 1 if calling angle is


greater or -1 if 'a' greater otherwise 0 by converting to degrees in long s1 =
deg*3600+min*60+sec,s2=a.deg*3600+a.min*60+a.sec;

Step 7: create a function Angle add(Angle a) which returns an Angle with sum
of 'a' and calling object.

Step 8: create a function Angle diff(Angle a) which return an Angle with


difference 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;

Angle(int d,int m,int s)

deg = d;

min = m;

sec = s;

void readAngle() throws Exception

Scanner sc=new Scanner(System.in);

System.out.print("\n\n\tEnter Data About An Angle");

System.out.print("\n\tEnter Degrees :");

deg = sc.nextInt();

System.out.print("\tEnter Minutes :");

min = sc.nextInt();

System.out.print("\tEnter Seconds :");

sec = sc.nextInt();
}

void showAngle()

System.out.print("\n\n\tAngle Is :"+deg+

(char)248+min+"'"+sec+"\"");

int isValidAngle()

if(deg<0 || min>59 || min<0 || sec>59 || sec<0)

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)

Angle ret = new Angle();

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;

return new Angle(s/3600,(s%3600)/60,(s%3600)%60);

public static void main()

Angle a = new Angle(),b = new Angle();

a.readAngle();

b.readAngle();
a.showAngle();

b.showAngle();

a.Diff(b).showAngle();

OUTPUT OF PROGRAM
/*

Enter Data About An Angle

Enter Degrees :10

Enter Minutes :20

Enter Seconds :30

Enter Data About An Angle

Enter Degrees :20

Enter Minutes :30

Enter Seconds :40

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 :-

num : an integer to store numerator


den : an integer to store denominator

Member Function :-

void readFraction() : to input Fraction

void showFraction() : to display Fraction

int hcf(int a,int b) : to return HCF of a & b

int lcm(int a,int b) : to return LCM of a & b

void reduce() : to reduce the fraction to co-prime

factor

Fraction addFraction(Fraction f) : to return sum of 'f' and calling

object as a Fraction

int compare(Fraction f) : to return 1 if calling fraction is

greater or else -1 if 'f' greater otherwise 0

ALGORITHM
Step 1: define a class Fraction and declare variables num den.

Step 2: define a function readFraction() to take input.

Step 3: define a function void showFraction() to display Fraction.

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 8: define a function int compare(Fraction f) to return 1 if calling fraction is


greater or else -1 if 'f' greater otherwise 0.

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()

Scanner sc=new Scanner(System.in);

System.out.print("\n\n\tEnter Numerator :");

num = sc.nextInt();

while(den==0)

System.out.print("\n\tEnter Denominator(<>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 lcm(int a,int b)

int c=a,d=b;

while(a!=b)

{ if(a<b)
a = a+c;

else

b = b+d; }

return a;

int hcf(int a,int b)

If(a%b==0)

return b;

else

return(hcf(b,a%b));

void reduce()

int cut = hcf((int)Math.abs(num),den);

num = num/cut;

den = den/cut;

Fraction addFraction(Fraction f)

Fraction ret = new Fraction();

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;

public static void main()

Fraction a=new Fraction(),b=new Fraction();

a.readFraction();b.readFraction();

a.reduce();a.showFraction();

b.reduce();b.showFraction();

a.addFraction(b).showFraction();

OUTPUT OF PROGRAM
/*
Enter Numerator :1000

Enter Denominator(<>0) :3000

Enter Numerator :2000

Enter Denominator(<>0) :3000

Fraction Is :1/3

Fraction Is :2/3

Fraction Is :1

*/

QUESTION 8 :
Define a class 'complex' with following specification :-

Data Member :-

real : to store real part

imag : to store imaginary part

Member Function :-

complex(double x,double y) : Parameterised constructor

complex add(complex z) : to return 'this' plus z as complex

complex sub(complex z) : to return 'this' minus z as complex

complex multiply(complex z) : to return 'this' product z as complex

complex divide(complex z) : to return 'this' division z as complex

void display() : to display 'this' in complex number

format
*/

ALGORITHM
Step 1: create a class complex and declare variables real,imag.

Step 2: define a parameterized constructor to assign values to data members.

Step 3: define a function complex add(complex z) to return 'this' plus z as


complex by creating an object and adding real part and imaginary parts of both
current and passed object and return the created object.

Step 4: define a function complex sub(complex z) to return 'this' minus z as


complex in a similar manner as add(complex z).

Step 5: define a function complex multiply(complex z) to return 'this' product z


as complex by using return new complex(real*z.real-
imag*z.imag,real*z.imag+imag*z.real);

Step 7: define a function complex divide(complex z) to return 'this' division z as


complex by multiplying with conjugate and dividing by real part.

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)

return new complex(real+z.real,imag+z.imag);

complex sub(complex z)

return new complex(real-z.real,imag-z.imag);

complex multiply(complex z)

return new complex(real*z.real-imag*z.imag,


real*z.imag+imag*z.real);

complex divide(complex z)

complex temp = new complex(real,imag);

complex conjugate = new complex(z.real,-(z.imag));

z = z.multiply(conjugate);

temp=temp.multiply(conjugate);

temp.real /= z.real;

temp.imag /= z.real;

return temp;

public static void main()

complex a = new complex(3,2),b = new complex(2,-3);

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 :-

Vector(int a,int b,int c) : Parameterised Constructor

void showVector() : to display vectors

Vector CrossProduct(Vector z) : to calculate and return cross product

int dotproduct(Vector z) : to calculate and return dot product

Vector add(Vector z) : to calculate and return sum of vectors

*/

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(&quot;Dot Product : &quot;+a.dotproduct(b));
System.out.print(&quot;ADD Vector : &quot;);
Vector z=a.add(b);z.showVector();
System.out.print(&quot;Cross Product : &quot;);
z=a.crossproduct(b);z.showVector();

PROGRAM

class Vector

int a,b,c;

Vector(int a,int b,int c)

this.a=a;this.b=b;this.c=c;

void showVector()

System.out.println(a+"i + "+b+"j + "+c+"k ");

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)

return new Vector(b*z.c-z.b*c,c*z.a-a*z.c,a*z.b-b*z.a);

class vect

public static void main(String args[])

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();

Output Of Program

/*
1i + 2j + 3k

3i + 4j + 5k

Dot Product : 18

ADD Vector : 4i + 6j + 8k

Cross Product : -2i + 4j + -2k

*/

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 2: declare function check(int x) declare s=0;f=0

Step 3: run a loop from i=1 till i<=x and do s=s+I;

Step 4: check if(s==x) f=1 and print number ids triangular if f==1

Step 5: end loop

Step 6: define another function to input the value of n and print if it is


triangular by calling the check(n) function

Step 7: end of function

Step 8: end of class

PROGRAM
import java.util.*;

class Number

int i;

int check(int x)

int s=0;int f=0;

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()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

int a=check(n);

if(a==1)

System.out.println("NUMBER IS TRIANGULAR");

else

System.out.println("NUMBER IS NOT TRIANGULAR")


}
}

OUTPUT OF PROGRAM

/*

Enter a number

21

NUMBER IS TRIANGULAR

Enter a number

19

NUMBER IS NOT TRIANGULAR

*/

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.

Step2: Declare a function void check(int) which accepts an integer to be


checked.

Step 3: create a copy m of integer n, and declare necessary variables p, I, j, flag.


Step 5: calculate and store the count of digits in c.

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

Step 9: end of algorithm.

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;}

int ar[]=new int[c];

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)

System.out.println("the number is not unique");

else

System.out.println("the number is unique");

OUTPUT OF PROGRAM
/*

Input:4896

The number is unique

Input:4556

The number is not unique

*/
QUESTION 12:
Define class 'MyNumber' with following specifications :-

Data Members :-

n - to store no

Member Functions :-

readdata() : to input n

boolean palindrome() : to return true if no. is palindrome otherwise false

boolean automorphic(): to return true if no. is automorphic otherwise false

boolean armstrong() : to return true if no. is armstrong otherwise false

boolean magical() : to return true if no. is magical otherwise false

voidshowall() : to display whether the no. one of the above

*/

ALGORITHM
Step 1: define a class MyNumber and declare variable n.

Step 2: declare a function void readdata() to take input.

Step 3: declare a function Boolean palindrome() which checks is a number is


palindrome or not.

Step 4: take out reverse of n and check if it is equal to itself return true else
return false.

Step 5: declare a function magical() to check for magic number.

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 9 : declare a function Armstrong() which checks if a number is Armstrong


or not.

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.

Step 13: end of algorithm.

PROGRAM
import java.util.*;

class MyNumber

int n;

void readdata()

Scanner sc= new Scanner(System.in);

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())

System.out.println("no. is magical : ");

if(palindrome())

System.out.println("no. is palindrome : ");

if(automorphic())

System.out.println("no. is automorphic : ");

if(armstrong())
System.out.println("no. is armstrong : ");

class number

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

MyNumber a=new MyNumber();

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 :-

a : to store first term

r : to store common ratio

n : to store limit

Member Functions :-

void readdata() : to input a,d,n

long nThterm(int) : to return nTh term of the series

long sum() : to return sum of the series

void showseries() : to display the series and sum

*/

ALGORITHM
Step 1: define a class GPS and declare variables a, r, and n.

Step 2: declare a function readata() to take a r n as input of gp series.

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.

Step 7:end of algorithm.


PROGRAM

import java.util.*;

class GPS

int a,r,n;

void readdata()

Scanner sc=new Scanner(System.in);

System.out.print("Enter first term : ");

a =sc.nextInt();

System.out.print("Enter common ratio : ");

r =sc.nextInt();

System.out.print("Enter limit : ");

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());

public static void main()

GPseries a=new GPseries();

a.readdata();

a.showseries();

OUTPUT OF PROGRAM

/*

Enter first term : 2

Enter common ratio : 2

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

program code and print the number without zeros

ALGORITHM
Step 1: define class REMOVEZEROES.

Step 2: declare function check() to take input.

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.

Step 5: end of algorithm.

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()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number containg zeros");

int n=sc.nextInt();
int no=check(n);

System.out.println("The number after removing zeros is"+no);

OUTPUT OF PROGRAM
/*

Enter a number

48090

The number after removing zeros is489

*/

QUESTION 15:
Define a 'Binomial' class with following specification :-

Data Members :-

x : to store value of x in (x + a)^n

a : to store value of a in (x + a)^n

n : to store limit

Member Functions :-

void readdata() : To input x,a,n

long factorial(int) : To return factorial of n

long nCr(int,int) : To return nCr


void sumseries() : To return value of (x+a)^n

ALGORITHM
Step 1: define a class Binomial and declare variables x, a, n.

Step 2: declare a function readdata to take input.

Step 3: declare a function factorial() which returns the factorial of a number


using recursion.

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()

Scanner sc=new Scanner(System.in);

System.out.print("Enter limit : ");

n =sc.nextInt();

System.out.print("Enter value of x :");

x =sc.nextInt();
System.out.print("Enter value of a :");

a =sc.nextInt();

long factorial(int b)

if(b==0 || b==1)

return 1;

else return b*factorial(b-1);

long nCr(int l,int r)

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);

System.out.println("Sum of "+"("+x+"+"+a+")"+"^"+n+" is : "+sum);

public static void main()


{

Binomial a=new Binomial();

a.readdata();

a.sumseries();

OUTPUT OF PROGRAM

/*

Enter limit : 6

Enter value of x :2

Enter value of a :1

Sum of (2+1)^6 is : 729

*/
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 2: create another parametrized constructor o initialize data members.

Step 3: in function void display() print the data members.

Step 4: create another class simple which extend class account using extends
keyword.

Step 5: create a parameterize constructor of this class .

Step 6:display in void display().

Step 7:create a function double interest which calculates interest and returns
it.

Step 8:end of class simple.

Step 9: create another class compound which inherits class simple.

Step 10: create constructor of the class compound.

Step 11:display account number time rate etc. in void display().

Step 12:calculate compound interest in double compound and return it.


Step 13:end the class compound.

Step 14:create a class Account which extends compound and call necessary
methods accordingly.

Step 15:end the class

PROGRAM

import java.io.*;

import java.util.*;

class Account

protected int accountNumber;

protected double principal;

Account()

accountNumber = 0;

principal = 0;

Account(int acn,double np)

accountNumber=acn;

principal=np;
}

void display()

System.out.println("Account Number : "+accountNumber);

System.out.println("Principal : "+principal);

class Simple extends Account

protected double time,rate;

Simple()

time = 0;

rate = 0;

Simple(int acn,double np,double nt,double nr)

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;

class Compound extends Account

protected double time,rate;

Compound()

time = 0;

rate = 0;

Compound(int acn,double np,double nt,double nr)

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

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

Scanner sc=new Scanner(SSytem.in);

Account a=new Account();

System.out.print("Enter Acc# : ");

int acn = sc.nextInt();

System.out.print("Enter principal : ");

double p = sc.nextDouble();
System.out.print("Enter rate : ");

int r = sc.nextInt();

System.out.print("Enter time : ");

int t = sc.nextInt();

System.out.print("Enter type of account (S/C) : ");

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

Enter type of account (S/C) : S

Account Number : 4

Principal : 10.0

Rate : 1.0

Time : 1.0
Interest : 0.1

*/

DATA STRUCTURE PROGRAMMING:


QUESTION 16:
Define a class 'Stack' with following specification :-

Data Members :-

a[] : array to store elements

size : to store size of array

top : to store and delete element at top position

Member functions :-

Stack(int s) : to initialize array with size & top=-1

boolean isEmpty() : to return true if array is empty

boolean isFull() : to return true if array is full

void Push(int item) : to push item at top position

void Pop() : to pop element at top position

void display() : to display elements from top to 0

*/

ALGORITHM
Step 1: define class stack and a constructor to initialize top by -1 and size.

Step 2: create booleanisEmpty() which returns true if array is empty.

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.

Step 5: void pop()

Step 6 check if the array is not empty and perform

Int temp =ar[top]

Top--.

Step 7:create a function display and un a loop from 0 to top and print the
elements.

Step 8:end of class.

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;

void Push(int item)

if(isFull())

System.out.println("Filled Upto The Brim");

else

a[++top] = item;

void Pop()
{

if(isEmpty())

System.out.println("Asking From Beggar");

else

top--;

void display()

System.out.print("Stack is :----> ");

for(int i=top;i>=0;i--)

System.out.print(a[i]+"\t");

System.out.println();

class teststack

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

Scanner sc=new Scanner(System.in);

System.out.print("Enter size : ");

Stack a = new Stack(sc.nextInt());int n;

System.out.println("1. Insert\n2. Delete\n3. Display\n4. Exit");

do
{

System.out.print("Enter choice : ");

n=Sc.nextInt();

switch(n)

case 1 :

System.out.print("Enter item : ");

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

Filled Upto The Brim

Enter choice : 3

Stack is :----> 2

Enter choice : 2

Enter choice : 2

Asking From Beggar

*/

QUESTION 17:

/*Define a class 'Queue' with following specification :-

Data Members :-
a[] : array to store elements

size : to store size of array

rear : store position of push

front : store position of pop

Member functions :-

Stack(int s) : Parameterised Constructor

boolean isEmpty() : to return true if array is empty

boolean isFull() : to return true if array is full

void Push(int item) : to push item at rear position

void Pop() : to pop element at front position

void display() : to display elements from front to rear

*/

ALGORITHM
Step 1: define class queue and a constructor to initialize rear by -1 and front
by 0.

Step 2: create booleanisEmpty() which returns true if array is empty.

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()

Step 6 check if the array is not empty and if it is not perform

Int temp =ar[front]

Front++;

Step 7:create a function dispalay and run a loop from 0 to rear and print the
elements.

Step 8:end of class.

PROGRAM
import java.io.*;

class Queue

int size,front,rear,a[];

Queue(int s)

size = s;a = new int[s];front = rear = -1;

boolean isEmpty()

if(front==-1 || front==rear+1)
return true;

return false;

boolean isFull()

if(rear==size-1)

return true;

return false;

void Push(int item)

if(isFull())

System.out.println("Filled Upto The Brim");

else

if(rear==-1)

rear = front = 0;

else

++rear;

a[rear] = item;

void Pop()
{

if(isEmpty())

front = rear= -1;

System.out.println("Asking From Beggar");

else

front++;

void display()

System.out.print("Queue is :----> ");

if(front>-1)

for(int i=front;i<=rear;i++)

System.out.print(a[i]+"\t");

System.out.println();

class testqueue

public static void main()


{

Scanner sc=new Scanner(System.in);

System.out.print("Enter size : ");

Queue a=new Queue(sc.nextInt());int n;

System.out.println("1. Insert\n2. Delete\n3. Display\n4. Exit");

do

System.out.print("Enter choice : ");

n = sc.nextInt();

switch(n)

case 1 : System.out.print("Enter item : ");

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

Filled Upto The Brim

Enter choice : 3

Queue is :----> 2

Enter choice : 2

Enter choice : 2

Asking From Beggar

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=””;

Step 2: define function void generate(String s0,String s1,int na)

Step 3:if(na>n)return; else print s0 and concat s1 with s0 in c

Step 4: call function generate(s1,c,na+1)

Step 5:end function end class

PROGRAM
import java.io.*;//importing packages

import java.util.Scanner;

class fibo

int n;

String c="";

void generate(String s0,String s1,int na)

if(na>n)

{
return;

else

System.out.print(s0+" ");

c=s1+s0;

generate(s1,c,na+1);

void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter value of n");

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

Num :to store a number

Left :to store the left part of the number

Right :to store the right part of the number

Member methods:

Midhalf(int m) : constructor to initialize num=m;

Int count(int n) : returns the number of digits using recursion

Int power(int a,int b) : return the value of ab by recursion

Void split() : breaks the number from middle using count and power

Void display() : to display the data with proper messages.

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 2: define function count(int n) check if(n==0) return 0

Step 3:else return 1+count(n/10)

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

Step 8:end of main, end of class

PROGRAM
import java.util.*;

import java.util.Scanner;

class Midhalf//define class

int num;

int left;

int right;

Midhalf(int m)//parameterized constructor

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));

left=num/p;//stores left part of number

right=num%p;//stores right part of number

void display()//to display

System.out.println("The left part of number is"+" "+left);

System.out.println("The right part of a number is"+" "+right);

public static void main()throws IOException

{
Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int n=sc.nextInt();

Midhalf obj=new Midhalf(n);

obj.split();

obj.display();

}//end of main

}//end of class

OUTPUT OF PROGRAM
/*

Enter a number

2356

The left part of number is 23

The right part of a number is 56

Enter a number

56789

The left part of number is 567

The right part of a number is 89

*/
QUESTION 20:
Define a class 'Dec_Bin' with following specifications :-

Data Members :-

n : to store decimal no.

s : to store equivalent binary no

i : incremental value of power

Member Functions :-

void getdata() : to input decimal no

void recursive() : recursive method to compute binary of no

void display() : to display decimal no and its binary equivalent

*/

ALGORITHM

Step 1: define class, declare variable n, s, i

Step 2: accept the value of decimal number

Step 3: check if(n>0) then s=s+(n%2)*(int)Math.pow(10,i++);and call the


function updating n by n/2

Step 4:display the number

Step 5 : end of method end of class

PROGRAM
import java.util.*;

class Dec_Bin

{
int n,s,i;

void getdata()throws Exception

Scanner sc=new Scanner(System.in);

System.out.print("Enter a no. : ");

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()

System.out.print("Decimal no. : "+n+" "+"

\nBinary equaivalent is : "+s);

}
class dec_bin

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

Dec_Bin a=new Dec_Bin();

a.getdata();

OUTPUT OF PROGRAM

/*

Enter a no. : 14

Decimal no. : 14

Binary equaivalent is : 1110

*/
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

Step 4: call the function if(eve<size&&odd<size)

Step 5: swap the even with odd in function swap()

Step 6: end of function

Step 7: end of class

PROGRAM
import java.util.*;

class eve_odd

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

Scanner sc=new Scanner(System.in);

//inputting size

System.out.print("Enter size : ");

int i,size = sc.nextInt();


int arr[] = new int[size];//inputting nos

System.out.println("Start Entering elements : ");

for(i=0;i<size;i++)

arr[i] =sc.nextInt();

System.out.println("Initial Array : ");

for(i=0;i<size;i++) //displaying initial array

System.out.print(arr[i]+"\t");

//caliing function

arrange(arr,size,0,0);

//displaying sorted array

System.out.println("\nSorted Array : ");

for(i=0;i<size;i++)

System.out.print(arr[i]+"\t");

System.out.println();

//changing position of even with odd and vice-versa

public static void arrange(int arr[],int size,int eve,int odd)

while(eve<size && arr[eve]%2!=0)

eve++;

while(odd<size && arr[odd]%2==0)

odd++;

if(eve<size && odd<size){


arrange(arr,size,eve+1,odd+1);

swap(arr,eve,odd);

//swapping nos

public static void swap(int arr[],int i,int j)

int t = arr[i];

arr[i] = arr[j];arr[j] = t;

OUTPUT OF PROGRAM
/*

Enter size : 2

Start Entering elements :

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 2: declare int j=0,vo=0,co=0;String v=”aeiouAEIOU”;to count vowels and


consonants

Step 3: run a loop till the length of the string and extract characters in char ch

Step 4: check if(v.indexOf(ch)!=-1) then vo++ else co++;

Step 5: after terminating for loop check if(vo==co) and return 1 else return 0

Step 6: define a main method input a String array of size 5

Step 7: call the ma(String ) function int x=ma(ar[i]) and print the element of
array if(x==1)

Step 8: close loop,end main(),end of class

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;

Scanner sc=new Scanner(System.in);

String ar[]=new String[5];


System.out.println("Enter 5 names");

for(i=0;i<5;i++)

ar[i]=sc.nextLine();

System.out.println("the magic names are");

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

The magic names are:

JANE

OSHI

SABA */
QUESTION 23:
//to display diamond pattern of string

ALGORITHM

Step 1: define a class diamondstr and main() function

Step 2: input a string in String s, declare i,j,k

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 4:check if(Math.abs(i)+Math.abs(j)<s.length()) then printlns.charAt(k) and


do k=j<0?++k:--k

Step 5:print space ,terminate for loop of j then println() aand terminate loop of
i

Step 6: end of main and end of class

PROGRAM

import java.util.*;

class diamondstr

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

Scanner sc=new Scanner(System.in);//inputting string

System.out.print("Enter a string : ");


String s = sc.nextLine();int i,j,k;

System.out.println("Pattern is : "); //displaying diamond pattern

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;

else System.out.print(" ");

System.out.println();

}//end of main

}//end of class

OUTPUT OF PROGRAM
/*

Enter a string : abc

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:

Input:”Display the result”

Output:”Dsply th rslt”

ALGORITHM

Step 1:define class novowels and declare variable String str,String


v=”AEIOUaeiou”

Step 2: define function recurse(inti,String s)

Step 3: check if (i==str.length()) return s

Step 4: in the else block check if(v.indexOf(str.charAt(i))!=-1)then call function


recurse(i+1,s) else return s+recurse(i+1,s)

Step 5:define void main

Step 6: input a sentence and call recurse(0,””)

Step 7: end of function end of class

PROGRAM

import java.io.*;

import java.util.Scanner;

class novowel// define class

{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="";

Scanner sc=new Scanner(System.in);

System.out.println("enter a sentence");//input a string

str=sc.nextLine();

sen=recurse(0);
System.out.println(sen);

}//end of main

}//end of class

OUTPUT OF PROGRAM
/*

Enter a sentence

“Display the result”

Dsply th rslt

*/

QUESTION 25:
Define a class 'Encrypt' which following specifications :-
Data Members :-

str : to store string

Member Functions :-

void readdata() : to input string

void encrypt() : to encrypt string by word length

void decrypt() : to decrypt string by word length

void display() : to display string

*/

ALGORITHM

Step 1: define class Encrypt and declare String str

Step 2: define function readdata () to input a string and print original string
alongwith encrypted and decrypted string

Step 3: in void encrypt run loop for(i=str.indexOf(" ",0);i!=-1;i=str.indexOf("


",i+1)) and update s= str.substring( )and j=i+1

Step 4: start another loop with k=0; and run until k<str.length()

Step 5: put r = s.charAt(k)+s.length();

Step 6: check if(r>90&&s.charAt(k)<='Z')then do r = r-26;


if(r>122&&s.charAt(k)<='z') r = r-26; s1 = s1+(char)r;terminate all loops

Step 7: for decrypt() perform step 3,4,5 but for checking


if(r<65&&s.charAt(k)>='A') r = r+26;if(r<97&&s.charAt(k)>='a') r = r+26; s1 =
s1+(char)r;

Step 8: end of all loops, end of function ,end of class.

PROGRAM
import java.util.*;
class Encrypt

String str="";

void readdata()

Scanner sc=new Scanner(system.in);

System.out.print("Enter a string : ");

str = sc.nextLine()+" ";

System.out.print("Original String : ");

display(str);

System.out.print("Encrypt String : ");

encrypt();

System.out.print("Decrypt String : ");

decrypt();

void encrypt()

String s="",s1="";int i=0,j=0,r,k=0;

for(i=str.indexOf(" ",0);i!=-1;i=str.indexOf(" ",i+1))

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()

String s="",s1="";int i=0,j=0,r,k=0;

for(i=str.indexOf(" ",0);i!=-1;i=str.indexOf(" ",i+1))

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);

void display(String st)

System.out.println(st);

class code

public static void main()

Encrypt a=new Encrypt();

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

Str : to store a word

Member methods

Alpha() : default constructor

void accept() : to input a word

void arrange() : to arrange the alphabets in ascending order using bubble


sort

void display() : to display the word

Write the main method to print the original word and the arranged word

ALGORITHM
Step 1: define class Alpha and constructor Alpha()

Step 2: input a word in function input()

Step 3: arrange characters of word by storing characters in an array and


perform sorting for(i=0;i<str.length()-1;i++) and for(j=0;j<str.length()-1-
I;j++)and swap accordingly

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

Step 6:end the class

PROGRAM
import java.io.*;

import java.util.Scanner;

class Alpha

String str;

Alpha()

str="";

void accept()

Scanner sc=new Scanner(System.in);

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()

System.out.println("The word is"+" "+str);

public static void main()

Alpha obj=new Alpha();

obj.accept();

obj.display();

obj.arrange();

obj.display();

OUTPUT OF PROGRAM
*/

Enter a string

"Computer"

The word is "Computer"

The word is ""Cemoprtu

Enter a string

Statistics
The word is Statistics

The word is Saciissttt

*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy