Sie sind auf Seite 1von 102

BIODATA

NAME-

Shashwat Singh

CLASS- XII

SECTION- A2

ROLL.NO.- 28
SUBJECT-

COMPUTER
PROGRAMING

PREFACE
The project has been made by
Shashwat Singh on the topic given
byMrs Monika Malik.
Project contains nearly all the
programs based on
Arrays,Strings,Patterns,Numbers,Con
versions and many more , All
the programs are of XII syllabus.
Proper care has been taken that
all the programs are written
correctly and nothing is missed.

Strings
Q.1-Write a program to accept a string and a word to
be deleted.
Display the string with that word deleted.
output
Enter a string
Eshant is great
enter a word to replace
Eshant
######## is great*/
import java.io.*;
class displace
{
public static void main(String args[])throws
Exception
{
BufferedReader d=new BufferedReader(new
InputStreamReader(System.in));
int l,i;
char ch;
String s=new String();
String s1=new String();String s2=new String();
System.out.println("Enter String");
s=d.readLine();

System.out.println("Enter String for word to


dispace");
s1=d.readLine();
l=s.length();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch!=' ')
{
s2=s2+ch;
}
if(s2==s1)
System.out.println("s.replace(s2,s1)");
}
}
}

Algorithm
Start
1 STEP -

import java.io.*;

2 STEP -

Create BufferedReader object;

3 STEP -

String s=new String();

4 STEP -

String s1=new String();

5 STEP -

String s2=new String();

6 STEP -

Display"Enter String";

7 STEP -

s=d.readLine();

8 STEP -

Display "Enter String for word to dispace".

9 STEP -

s1=d.readLine();

10 STEP -

l=s.length();

11 STEP repeat step 12 to 16 to l.


12 STEP -

ch=s.charAt(i);

13 STEP -

Check if(ch!=' ')

14 STEP -

s2=s2+ch;

15 STEP -

if(s2==s1)

16 STEP -

print "s.replace(s2,s1)".

End

Q.2-To fill * where space occurs


import java .io.*;
class fill_ast_all_occurs
{
public static void main(String args[])throws Exception
{
int t=0,i=0,j=0,k=0;

DataInputStream d=new DataInputStream(System.in);


int a=0;
String s=new String();
String s1;
System.out.println("pto");
s=d.readLine();
s1=d.readLine();
String s2;
s=s+' ';
s1=s1+" ";
try{
for(t=0;t<s.length();t++)
{
s2=s.substring(t,t+(s1.length()));
if(s2.equals(s1))
{
k=t;
for(j=k;j<k+(s1.length()-1);j++)
{
System.out.print('*');
t++;
}
System.out.print(" ");
}

else
System.out.print(s.charAt(t));
}
}
catch(IndexOutOfBoundsException e){}
}
}
/* variable description
s=to input string.
s1=to store string.
k=to store value of t.
j,t= to makefor loop.

Algorithm
Start
1 STEP -

import java .io.*;

2 STEP -

Create DataInputStream Object.

3 STEP -

String s=new String();

4 STEP -

s=d.readLine();

5 STEP -

s1=d.readLine();

6 STEP -

s=s+' ';

7 STEP -

s1=s1+" ";

8 STEP -

try{

9 STEP -

repeat step 10 to 18 till t<length

10 STEP -

s2=s.substring(t,t+(s1.length()));

11 STEP -

if(s2.equals(s1))

12 STEP -

then

13 STEP -

k=t

14 STEP -

print '*'

15 STEP -

t++

16 STEP -

print " "

17 STEP -

Else{

18 STEP -

Print s.charAt(t)

19 STEP -

catch(IndexOutOfBoundsException e){}

20 STEP -

End

Q.3- To check that each alphabet occurs how many


times in a string
import java.io.*;
class charfre
{
public static void main(String[] args) throws
IOException
{

for (i='a';i<='z';i++)
{
for(j=0;j<s.length();j++)
{
if(i==s.charAt(j))
{
c++;
}
}
if (c!=0)
{
System.out.println(i+ " :- occurs== "+c);
}
c=0;
}
}
}
/* variable description
i,j=to make for loop.
to count occurence. */

Algorithm
1 STEP -

import java.io.*

Start

2 STEP -

repeat step 3 from a to z.

3 STEP -

repeat step 4 &5 from till j=length of string

4 STEP -

Check for condition that if(i==s.charAt(j))

5 STEP -

Then c++;

6 STEP -

Check if (c!=0)

7 STEP -

Print c.

8 STEP -

Give c=0,

End

Q.4-To print the string in piglatin form


import java.io.*;
class piglatin
{
public static void main(String args[])throws Exception
{
DataInputStream d=new DataInputStream(System.in);
int i=0,e=0;
String s=new String();
String s1=new String();
System.out.println("Enmdra");
s=d.readLine();
if(s.charAt(0)!='a'||s.charAt(0)!='e'||s.charAt(0)!='i'||
s.charAt(0)!='o'||s.charAt(0)!='u')

{
for(i=0;i<s.length();i++)
{
if(s.charAt(i)=='a'||s.charAt(i)=='e'||s.charAt(i)=='i'||
s.charAt(i)=='o'||s.charAt(i)=='u')
{
e=i;
break;
}
}
for(int j=e;j<s.length();j++)
s1=s1+s.charAt(j);
for(int j=0;j<e;j++)
s1=s1+s.charAt(j);
System.out.println(s1+"Ay");
}
else
{
System.out.println(s);
}
}
}
/* variable description
s,s1=tostore strings.
i=to compare character.

j=to make for loop.

*/

Algorithm
Start

1 STEP -

import java.io.*;

2 STEP -

Create DataInputStream object.

3 STEP -

String s=new String();

4 STEP -

String s1=new String();

5 STEP -

Display "Enmdra"

6 STEP -

s=d.readLine();

7 STEP -

if(s.charAt(0)!='a'||s.charAt(0)!='e'||s.charAt(0)!

='i'||s.charthen At(0)!='o'||s.charAt(0)!='u')
8 STEP - repeat step 9 to 11 untill i<length
9 STEP -

if(s.charAt(i)=='a'||s.charAt(i)=='e'||

s.charAt(i)=='i'||s.charthen At(i)=='o'||s.charAt(i)=='u')
10 STEP - e=i
11 STEP -

break

12 STEP -

repeat step13 to 18 untill j<l

13 STEP -

s1=s1+s.charAt(j);

14 STEP -

repeat step 15 to 17 till j<e

15 STEP -

s1=s1+s.charAt(j);

16 STEP -

Print "Ay"

17 STEP -

else{

18 STEP -

Print(s)

19 STEP -

End

Q.5-To convert string in this form:input->This is my india


output->sihT si ym aidni
import java .io.*;
class harsh
{
public static void main(String args[])throws Exception
{
DataInputStream d=new DataInputStream(System.in);
int i,j,l;
char ch;
String s=new String();
String s1="";
String s2="";
System.out.println("Enter any string");
s=d.readLine();
l=s.length();
for(i=0;i<l;i++)
{

ch=s.charAt(i);
if(ch!=' ')
{
s1=ch+s1;
}
else
{
s2=s2+s1;
s2=s2+" ";
s1="";
}
}
System.out.println("String="+s2);
}
}
/* variable description
s=to input string.
s1,s2=to store string.
l=to take length of string.
i=to make for loop.
*/
input->This is my india
output->sihT si ym aidni

Algorithm
Start

1 STEP -

import java .io.*;

2 STEP -

Create DataInputStream object.

3 STEP -

String s=new String();

4 STEP -

String s1="";

5 STEP -

String s2="";

6 STEP -

Display "Enter any string"

7 STEP -

s=d.readLine();

8 STEP -

l=s.length();

9 STEP -

repeat step 10 to 19 till j<l

10 STEP -

ch=s.charAt(i);

11 STEP -

if(ch!=' ')

12 STEP -

then

13 STEP -

s1=ch+s1;

14 STEP -

else{

15 STEP -

s2=s2+s1;

16 STEP -

s2=s2+" ";

17 STEP -

s1="";

18 STEP -

print ("String="+s2);

End

Number based
Q.6- To find that no. is twin prime or not
import java.io.*;
class aar2
{
public static void main(String args[])throws Exception
{
DataInputStream d=new DataInputStream(System.in);
int i,c=0,j,x=0;
for(j=1;j<=100;j++)
{
for(i=1;i<=j;i++)
{
if(j%i==0)
{
c++;
}
}
if(c==2)
{

for(i=1;i<=(j+2);i++)
{
if((j+2)%i==0)
{
x++;
}
}
if(x==2)
System.out.println(j+","+(j+2));
}
c=0;
x=0;
}
}
}
/* variable description
i,j=to make for loop.
c=to count no.of times no. is divisible.
x=to count no.of times other no. is divisible.

Algorithm
Start

1 STEP -

import java.io.*;

2 STEP -

Create DataInputStream object.

3 STEP -

int i,c=0,j,x=0;

4 STEP -

repeat step 5 untill j= 100and other till j=i;

5 STEP - if(j%i==0)
6 STEP - then c++,
7 STEP - if(c==2)
8 STEP repeat step9,10 till i=(j+2)
9 STEP -

if((j+2)%i==0)

10 STEP - then x++;


11 STEP - if(x==2)
12 STEP -

print j ,c=0,x=0.

End
Q.7 -Class name: Special
Data Members:int n
Member Methods : Special(int m)
int fact(int x) :: returns factorial of a

number
int isspecial() :: returns 1 if number is
special else o.
void display() :: prints number if special
*/
import java.io.*;
class cspecial
{
int n;
cspecial(int m)
{
n=m;
}
int fact(int x)
{
int i,f=1;
for(i=x;i>0;i--)
f=f*i;
return f;
}
int isspecial()
{
int a=n,s=0;
while(a!=0)
{

s=s+fact(a%10);
a=a/10;
}
if(s==n)
return 1;
return 0;
}
void display()
{
if(isspecial()==1)
System.out.println("Special");
else
System.out.println("Not Special");
}
}

Algorithm
Start

1 STEP -

Take the integer from the user to check whether it

is Special or not.
2 STEP - Store that value in another integer(n=m)
3 STEP -

Find the factorial of the number(f=f*i)

4 STEP -

a=n.

5 STEP -

while(a!=0)

s=s+fact(a%10)
a=a/10
6 STEP - Check if(s==n)
7 STEP -

return 1,

8 STEP -

return 0.

9 STEP -

If(isspecial==1) print Special

10 STEP -

Else print not Special.

End

Q.8-To find the cosine of the angle


import java.io.*;
import java.lang.*;
class CosNumber
{
public static void main() throws Exception
{
double x,y;
DataInputStream d=new
DataInputStream(System.in);
System.out.print("enter Value of x =");
x = Double.parseDouble(d.readLine());
y = Math.cos(x*22/1260);
System.out.println();

System.out.print("Cosine Value of Angle =" +y);


}
}
variable description
x=to store no.
y=to convert angle.*/

Algorithm
Start

1 STEP -

Use import java.io.*;

2 STEP -

Use import java.lang.*;

3 STEP -

Create DataInputStream Object,

4 STEP -

Give a message to user "enter Value of x =";

5 STEP -

Take the values of x from user ;

6 STEP -

Give the mathematical Expression that

Math.cos(x*22/1260);
7 STEP - Print "Cosine Value of Angle " .

End
Q.9-To check a number is unique or not.
import java.io.*;
class prog1

{
public static void main()throws Exception
{
DataInputStream d=new DataInputStream(System.in);
int n,c,r,p,s,i;
s=0;
c=0;
System.out.println("Enter a Number");
n=Integer.parseInt(d.readLine());
p=n;
while(p!=0)
{
r=p%10;
c=c+1;
p=p/10;
}
int a[]=new int[c];
for(i=0;i<c;i++)
{
a[i]=n%10;
n=n/10;
}
for(i=0;i<c-1;i++)
{
if(a[i]==a[i+1])

s=1;
}
if(s==1)
System.out.println("No. not Unique");
else
System.out.println("No. Unique");
}
}
Enter a Number
456
No. Unique
Enter a Number
334567
No. not Unique

Algorithm
Start

1 STEP -

import java.io.*;

2 STEP -

Create DataInputStream Object.

3 STEP -

s=0

4 STEP -

c=0

5 STEP -

Display "Enter a Number"

6 STEP -

n=Integer.parseInt(d.readLine());

7 STEP -

p=n;

8 STEP -

while(p!=0)

9 STEP -

r=p%10;

10 STEP - c=c+1;
11 STEP - p=p/10;
12 STEP - int a[]=new int[c];
13 STEP repeat step 14 and 15 till i<c
14 STEP - a[i]=n%10;
15 STEP -

n=n/10;

16 STEP - i++)
17 STEP -

if(a[i]==a[i+1])

18 STEP -

then s=1;

19 STEP -

check if(s==1)

20 STEP -

print "No. not Unique"

21 STEP -

else

22 STEP -

print "No. Unique"

End

Q.10-To check a number is magic or not.*/


import java.io.*;
class magic
{
public static void main()throws Exception
{
DataInputStream d=new
DataInputStream(System.in);

int n,c;
c=0;
System.out.println("Enter a Number");
n=Integer.parseInt(d.readLine());
while(n!=0)
{
c=c+n%10;
n=n/10;
if(c>9 && n==0)
{
n=c;
c=0;
}
}
if(c==1)
System.out.println("No. is magic");
else
System.out.println("No. is Not-magic");
}
}
/*Enter a Number
9981
No. is Not-magic

Enter a Number
900191
No. is Not-magic
Enter a Number
433
No. is magic
Enter a Number
1009
No. is magic
*/
variable description
n=to input no.
x,j,t=to make calculations.
m=to chack magical no.
*/

Algorithm
Start

1 STEP -

Use import java.io.*;

2 STEP -

Create DataInputStream object.

3 STEP -

Display "Enter a Number"

4 STEP -

n=Integer.parseInt(d.readLine());

5 STEP -

while(n!=0)

6 STEP -

c=c+n%10;

7 STEP -

n=n/10;

8 STEP -

if(c>9 && n==0)

9 STEP -

then

10 STEP -

n=c

11 STEP -

c=0

12 STEP -

if(c==1)

13 STEP -

print"No. is magic"

14 STEP -

else

15 STEP -

print "No. is Not-magic"

End
Q.11-To find whether the no is perfect or not
import java.io.DataInputStream;
class perferct
{
public static void main(String args[])throws Exception
{
DataInputStream d=new DataInputStream(System.in);
int n,m,i,r,lcm=0,p;
System.out.println("ENTER ANY NO.");
n=Integer.parseInt(d.readLine());
for(i=1;i<n;i++)
{
if(n%i==0)
lcm=lcm+i;

}
if(lcm==n)
System.out.println("Perfect");
else
System.out.println("Not Perfect");
}
}
/* variable description
n=to enter a no.
i=to make for loop.
lcm,n=to store result.

Algorithm
Start

1 STEP -

Use import java.io.DataInputStream;

2 STEP -

Create DataInputStream d=new

DataInputStream(System.in);
3 STEP - Display "ENTER ANY NO."
4 STEP -

n=Integer.parseInt(d.readLine());

5 STEP -

for(i=1;i<n;i++)

6 STEP -

if(n%i==0)

7 STEP -

then lcm=lcm+i

8 STEP -

if(lcm==n)

9 STEP -

then print"Perfect"

10 STEP -

else

11 STEP -

print "Not Perfect"

End

Q.12-To find whether the no is perfect or not


import java.io.DataInputStream;
class perferct
{
public static void main(String args[])throws Exception
{
DataInputStream d=new DataInputStream(System.in);
int n,m,i,r,lcm=0,p;
System.out.println("ENTER ANY NO.");
n=Integer.parseInt(d.readLine());
for(i=1;i<n;i++)
{
if(n%i==0)
lcm=lcm+i;
}
if(lcm==n)
System.out.println("Perfect");

else
System.out.println("Not Perfect");
}
}
/* variable description
n=to enter a no.
i=to make for loop.
lcm,n=to store result.

Algorithm
Start

1 STEP -

Use import java.io.DataInputStream;

2 STEP -

Create DataInputStream d=new

DataInputStream(System.in);
3 STEP - Display "ENTER ANY NO."
4 STEP -

n=Integer.parseInt(d.readLine());

5 STEP -

for(i=1;i<n;i++)

6 STEP -

if(n%i==0)

7 STEP -

then lcm=lcm+i

8 STEP -

if(lcm==n)

9 STEP -

then print"Perfect"

10 STEP -

else

11 STEP -

print "Not Perfect"

End

Q.13- To check a number is automorphic or not.


import java.io.*;
class prog6
{
public static void main()throws Exception
{
DataInputStream d=new
DataInputStream(System.in);
int a,n,c,r,j,i,sq;
c=0;
j=1;
System.out.println("Enter a Number");
a=Integer.parseInt(d.readLine());
n=a;
while(n!=0)
{
r=n%10;
c++;
n=n/10;
}

for(i=1;i<=c;i++)
j=j*10;
sq=a*a;
System.out.println("Square\t"+sq);
if((sq%j)==a)
System.out.println("No. is Automorphic");
else
System.out.println("No. is Not-Automorphic");
}
}
/*
Enter a Number
25
Square 625
No. is Automorphic
Enter a Number
35
Square 1225
No. is Not-Automorphic

Algorithm
1 STEP -

import java.io.*;

Start

2 STEP -

Create DataInputStream object

3 STEP -

c=0;

4 STEP -

j=1;

5 STEP -

print "Enter a Number"

6 STEP -

a=Integer.parseInt(d.readLine());

7 STEP -

n=a;

8 STEP -

while(n!=0)

9 STEP -

r=n%10;

10 STEP -

c++;

11 STEP -

n=n/10;

12 STEP - repeat step 13 to 16 till i<c.


13 STEP -

j=j*10;

14 STEP -

sq=a*a;

15 STEP -

"Square "

16 STEP -

if((sq%j)==a)

17 STEP -

print "No. is Automorphic"

18 STEP -

else

19 STEP -

print "No. is Not-Automorphic"

End

Searching
Q.14-To search a string in a array using binary
search.assume that the strings are entered in
descending order.
import java.io.*;
class prog13
{
public static void main()throws Exception
{
DataInputStream d=new
DataInputStream(System.in);
String s[]=new String[10];
int i,l,u,m,pos;
String n=new String();
for(i=0;i<10;i++)
{
System.out.println("ENTER NAME "+(i+1)+" : ");
s[i]=d.readLine();
}
System.out.println("Enter the String to be
Searched");
n=d.readLine();
l=0;u=9;pos=-1;
m=0;

while(l<=u && pos==-1)


{
m=(l+u)/2;
if(s[m].equals(n))
pos=m;
else
{
if((s[m].compareTo(n))>0)
u=m-1;
else
l=m+1;
}
}
if(pos!=-1)
System.out.println("Name found at "+(m+1)+"
Position");
else
System.out.println("Name not found ");
}
}
/*
ENTER NAME 1 :
abhishek
ENTER NAME 2 :

babloo
ENTER NAME 3 :
chanchal
ENTER NAME 4 :
dharmendra
ENTER NAME 5 :
farook
ENTER NAME 6 :
govinda
ENTER NAME 7 :
himanshu
ENTER NAME 8 :
jitendra
ENTER NAME 9 :
manmohan
ENTER NAME 10 :
zeeshan
Enter the String to be Searched
govinda
Name found at 6 Position

Algorithm
Start

1 STEP -

import java.io.*;

2 STEP -

Create DataInputStream object.

3 STEP -

String s[]=new String[10];

4 STEP -

String n=new String();

5 STEP -

repeat step 6 ,7 till i<10.

6 STEP -

Display ("ENTER NAME "+(i+1)+" : ");

7 STEP -

s[i]=d.readLine();

8 STEP -

Display "Enter the String to be Searched"

9 STEP -

n=d.readLine();

10 STEP -

l=0;u=9;pos=-1;

11 STEP -

m=0;

12 STEP -

while(l<=u && pos==-1)

13 STEP -

m=(l+u)/2;

14 STEP -

if(s[m].equals(n))

15 STEP -

then pos=m;

16 STEP -

else{

17 STEP -

if((s[m].compareTo(n))>0)

18 STEP -

then u=m-1;

19 STEP -

else

20 STEP -

l=m+1;

21 STEP -

if(pos!=-1)

22 STEP -

then print("Name found at "+(m+1)+" Position")

23 STEP -

else

24 STEP -

print"Name not found "

End

Sorting
Q.15- Sorting of array using Insertion sort.
import java.io.*;
class insertion
{
public static void main(String args[])throws exception
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in);
int a[],I,n.k=0;
System.out.println(Enter size);
n=Integer.parseInt(in.readLine());
for(i=0;i<n;i++)
{
System.out.println(Enter array elements);
a[i]= Integer.parseInt(in.readLine());
}
for(i=0;i<n;i++)
{
k=a[i];
j=i;
While((j>0)&&(a[j]>k))
{

a[j]=a[j-1];
j--;
}
a[j]=k;
}

for(int y=0;y<=n;y++)
{
System.out.println(a[y]);
}
}
}

Algorithm
Start
1 STEP import java.io.*;
2 STEP class insertion
3STEP create BufferedReader object.
4 STEP int a[],I,n.k=0;
5 STEP System.out.println(Enter size);
6 STEP n=Integer.parseInt(in.readLine());
7 STEP repeat step to till i<n.
8 STEP System.out.println(Enter array elements);
9 STEP a[i]= Integer.parseInt(in.readLine());

10 STEP repeat step to till i<n.

11 STEP k=a[i];
12 STEP j=i;
13 STEP While((j>0)&&(a[j]>k))
14 STEP a[j]=a[j-1];
15 STEP j--;
16 STEP a[j]=k;
17 STEP repeat step to till y<n.

18 STEP System.out.println(a[y]);

End

Q.16- Bubble Sorting


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

DataInputStream d=new DataInputStream(System.in);


int t=0;
int a[]=new int[5];
for(int i=0;i<5;i++)
{
a[i]=Integer.parseInt(d.readLine());
}
for(int j=4;j>=0;j--)
{
for(int k=0;k<=j-1;k++)
{
if(a[k]>a[k+1])
{
t=a[k];
a[k]=a[k+1];
a[k+1]=t;
}
}
}
for(int y=0;y<=4;y++)
{
System.out.println(a[y]);
}
}
}

/* variable description
a=to input array.
i,j,k=to make for loop.
t=to sort.
*/

Algorithm
1 STEP - import java.io.*;
2 STEP -

Start

Create DataInputStream Object;

3 STEP repeat step5 till i<5


4 STEP - a[i]=Integer.parseInt(d.readLine());
5 STEP -

repeat step 6 till 0>i<=4.

6 STEP -

repeat step 7,8 till k<j-1;

7 STEP -

if(a[k]>a[k+1])

8 STEP - Then t=a[k];


9 STEP -

a[k]=a[k+1];

10 STEP - a[k+1]=t;
11 STEP - print the array.

End

Pattern
Q.17- write a program to print the following pattern.
class happy
{
static void fu(String str)
{
str=str+" "+str;
int l=str.length();
int mid=l/2;
int m,n;
m=n=mid;
for(int i=0;i<str.length();i++)
{
for(int j=0;j<str.length();j++)
{
if(j>m&&j<n)
System.out.print(" ");
else
System.out.print(str.charAt(j));
}
if(i<mid)
{
m--;
n++;

}
else
{
m++;
n--;
}
System.out.println();
}
}
}
/*
input:happy
output:
happy happy
happy happy
happ appy
hap ppy
ha
py
h
y
ha
py
hap ppy
happ appy
happy happy
happy happy

Algorithm
Start
1 STEP- str=str+" "+str;
2 STEP-

int l=str.length();

3 STEP- int mid=l/2;


4 STEP-

int m,n;

5 STEP- m=n=mid;
6 STEP-

repeat step 6 till i<s.length.

7 STEP-

repeat step 7 and 9 till j<l

8 STEP-

if(j>m&&j<n)

9 STEP10 STEP-

System.out.print(" ");
else

11 STEP-

System.out.print(str.charAt(j));

12 STEP-

if(i<mid)

13 STEP-

m--;

14 STEP- n++;
15 STEP-

else

16 STEP-

m++;

17 STEP-

n--;

18 STEP-

System.out.println();

End

Q.18- To print the following pattern


abcdcba
abc cba
ab ba
a
a
ab ba
abc cba
abcdcba */
class ab3
{
public static void main(String args[])
{
int x,k,f,c;
char i,j,y,m,g;
for(i='d';i>='a';i--)
{
for(j='a';j<=i;j++)

{
System.out.print(j);
}
x=(int)i;
k=x-1;
f=x-2;
y=(char)k;
m=(char)f;
c=x+1;
g=(char)c;
for(j=y;j<='a';j++)
System.out.print(" ");
for(j=g;j<='d';j++)
System.out.print(" ");
if(i=='d')
{
for(j='c';j>='a';j--)
System.out.print(j);
}
else
{
for(j=i;j>='a';j--)
System.out.print(j);
}
System.out.println();

}
for(i='b';i<='d';i++)
{
for(j='a';j<=i;j++)
{
System.out.print(j);
}x=(int)i;
k=x-1;
f=x-2;
c=x+1;
g=(char)c;
int t=x+2;
char r=(char)t;
y=(char)k;
m=(char)f;
for(j=g;j<='d';j++)
System.out.print(" ");
for(j=r;j<='d';j++)
System.out.print(" ");
if(i!='d')
{
for(j=i;j>='a';j--)
System.out.print(j);
}
else

{
for(j='c';j>='a';j--)
System.out.print(j);
}
System.out.println();
}
}
}
/* variable description
i,j,y,m,g=to read characters and to make for loop.
x,k,f,c=for counting and reinitializing values.

Algorithm
Start

1 STEP -

repeat step2 to 44 till i >a and i = d.

2 STEP -

repeat step3 till j<i.

3 STEP -

print (j)

4 STEP -

x=(int)i;

5 STEP -

k=x-1;

6 STEP -

f=x-2;

7 STEP -

y=(char)k;

8 STEP -

m=(char)f;

9 STEP -

c=x+1;

10 STEP -

g=(char)c;

11 STEP -

repeat step 12 to 22 till j<=a

12 STEP -

print(" ")

13 STEP -

repeat step 14 till j<d.

14 STEP -

print(" ")

15 STEP -

if(i=='d')

16 STEP -

then

17 STEP -

repeat step 18 till j<e.

18 STEP -

print(j)}

19 STEP -

else{

20 STEP -

repeat step 21 till j<e.

21 STEP -

print (j)

22 STEP -

23 STEP -

repeat step 24 till j<e.

24 STEP -

repeat step 25 to 34 till j<i

25 STEP -

print (j)

26 STEP -

x=(int)i;

27 STEP -

k=x-1;

28 STEP -

f=x-2;

29 STEP -

c=x+1;

30 STEP -

g=(char)c;

31 STEP -

int t=x+2;

32 STEP -

char r=(char)t;

33 STEP -

y=(char)k;

34 STEP -

m=(char)f;

35 STEP -

repeat step 36 till j<d.

36 STEP -

print (" ")

37 STEP -

repeat step 38 till j<d.

38 STEP -

print (" ")

39 STEP -

if(i!='d')

40 STEP -

then

41 STEP -

repeat step 42 & 43 till j>a

42 STEP -

print (j)

43 STEP -

else{

44 STEP -

repeat step 45 till j>a

45 STEP -

print (j)

46 STEP -

End

1-D and 2D Array


Q.19-To find the n highest number in an array.
import java.io.*;
class prog14
{

public static void main()throws Exception


{
DataInputStream d=new
DataInputStream(System.in);
int m,n,p,s=0,i,c=0;
System.out.println("Enter the limit of array");
m=Integer.parseInt(d.readLine());
int a[]=new int[m];
for(i=0;i<m;i++)
{
System.out.println("Enter element "+(i+1)+" : ");
a[i]=Integer.parseInt(d.readLine());
}
System.out.println("Enter which highest to find ");
n=Integer.parseInt(d.readLine());
for(i=0;i<m;i++)
{
if(a[i]>s)
s=a[i];
}
while(c!=(n-1))
{
p=0;
for(i=0;i<m;i++)
{

if(a[i]>p && a[i]<s)


p=a[i];
}
s=p;
c++;
}
System.out.println(s);
}
}
/*
Enter the limit of array
6
Enter element 1 :
12
Enter element 2 :
23
Enter element 3 :
341
Enter element 4 :
12
Enter element 5 :
4545
Enter element 6 :
32
Enter which highest to find

3
32

Algorithm
Start

1 STEP -

import java.io.*;

2 STEP -

Create DataInputStream object.

3 STEP -

"Enter the limit of array"

4 STEP -

m=Integer.parseInt(d.readLine());

5 STEP -

int a[]=new int[m];

6 STEP -

repeat step 7 and 8 till i<m

7 STEP -

Display ("Enter element "+(i+1)+" : ")

8 STEP -

a[i]=Integer.parseInt(d.readLine());

9 STEP -

Display "Enter which highest to find "

10 STEP -

n=Integer.parseInt(d.readLine());

11 STEP -

repeat step 12 and 13 till i<m

12 STEP -

if(a[i]>s)

13 STEP -

then s=a[i];

14 STEP -

while(c!=(n-1))

15 STEP -

p=0;

16 STEP -

repeat step 17 and 18 till i<m

17 STEP -

if(a[i]>p && a[i]<s)

18 STEP -

then p=a[i];

19 STEP -

s=p;

20 STEP -

c++;

21 STEP -

Print (s)

End

Q-20- 2D Array Sorting


import java.io.*;
class sorting
{
public static void main(String args[])throws Exception
{
DataInputStream d=new DataInputStream(System.in);
int a[][]=new int[4][4];
int r,j,i,c;
for(r=0;r<4;r++)
{
for(c=0;c<4;c++)
{
a[r][c]=Integer.parseInt(d.readLine());
}
}
for(i=0;i<4;i++)

{
for(j=0;j<4;j++)
{
for(r=0;r<4;r++)
{
for(c=0;c<4;c++)
{
if(a[r][c]>a[i][j])
{
int t=a[r][c];
a[r][c]=a[i][j];
a[i][j]=t;
}}
}}
}
for(r=0;r<4;r++)
{
for(c=0;c<4;c++)
{
System.out.print(a[r][c]+"\t");
}
System.out.println();
}
}
}

/* variable description
a=to input array.
r=rows.
c=column.
i,j=to make for loop.

Algorithm
START
1 STEP -

import java.io.*;

2 STEP -

Create DataInputStream object

3 STEP -

int a[][]=new int[4][4];

4 STEP -

repeat step 5 till r<4 and c<4.

5 STEP -

a[r][c]=Integer.parseInt(d.readLine());

6 STEP -

repeat steps 7 to 11 till i<4.

7 STEP -

if(a[r][c]>a[i][j])

8 STEP -

Then-

9 STEP -

int t=a[r][c];

10 STEP -

a[r][c]=a[i][j];

11 STEP -

a[i][j]=t;

12 STEP -

print the array.

END

REcursion
Q.21- Write a program to calculate sum of n Natural
numbers.
class natural
{
public int Sumnatural(int n)
{
if(n==1) return 1;
else
return(n+SumNatural(n-1));
}

Algorithm
START

1 STEP -

int Sumnatural(int n)

2 STEP -

if(n==1) return 1;

3 STEP -

else

4 STEP -

return(n+SumNatural(n-1));

END
Q.22- Write a recurcive function to calculate hcf of
two numbers.
class highestfact
{
public int hcf(int x, int y)
if(x==y) return x;
else if(x>y)
return hcf(x-y , y);
else
return hcf(x , y-x);
}

Algorithm
START

1 STEP -

int hcf(int x, int y)

2 STEP -

if(x==y) return x;

3 STEP -

else if(x>y)

4 STEP -

return hcf(x-y , y);

5 STEP -

else

6 STEP -

return hcf(x-y , y);

END

Q.23- Write a recursive function to find sum of vowels


in a string.
class alpha
{
public int vowels(String s, int i)
{
if(i<s.length())
{
char ch = s.charAt(i);
if(AEIOUaeiou.indexOf(ch)!=-1)

return 1+vowels(s,i+1);
else
return vowels(s,i+1);
}
}
}

Algorithm
START

1 STEP -

int vowels(String s, int i)

2 STEP -

if(i<s.length())

3 STEP -

char ch = s.charAt(i);

4 STEP -

if(AEIOUaeiou.indexOf(ch)!=-1)

5 STEP -

return 1+vowels(s,i+1);

6 STEP -

else

7 STEP -

return vowels(s,i+1);

END
Q.24- write a recursive function to convert a number
into hexadecimal number.
Class hexo

{
public String hexa(int n , int b)
{
String h=123456789ABCDEF;
if(n<=9)
return Character.toString(h.charAt(n));
else{
int r=n%b;
char c=h.charAt(r);
return hexa(h/b,b)+ Character.toString(c);
}
}

Algorithm
START

1 STEP -

String hexa(int n , int b)

2 STEP -

String h=123456789ABCDEF;

3 STEP -

if(n<=9) return

5 STEP -

int r=n%b;

Character.toString(h.charAt(n));
4 STEP - else{
6 STEP -

char c=h.charAt(r);

7 STEP -

return hexa(h/b,b)+ Character.toString(c);

END

User s choice
Q.25-To calculate the fine of the worker for the no.
of days he is absent
less than 10 days , (days*1)
> than 11 but < than 20 , (days*1 + (days10)*5)
> than 20 , (days*1 + days*5 + (days20)*8)*/
import java. io.*;
class fine1
{
public static void main(String[] args)throws
IOException
{
DataInputStream d=new
DataInputStream(System.in);
System.out.println(" Enter Days");
int days = Integer.parseInt(d.readLine());

int fine=0;
if ( days <=10)
{
fine = days*1;
}
else if (days >=11 && days<=20)
{
fine = ( 10*1) + ( days-10) *5;
}
else if (days > 20)
{
fine = (10*1) + (10*5) + (days-20)*8;
}
System.out.print("Fine is : "+fine);
}
}
variable description
days=to input days.
fine=to calculate fine.

Algorithm
Start
1 STEP -

import java. io.*;

2 STEP -

Create DataInputStream Object.

3 STEP -

Display "Enter Days"

4 STEP -

int days = Integer.parseInt(d.readLine());

5 STEP -

int fine=0

6 STEP -

if ( days <=10)

7 STEP -

then fine = days*1;

8 STEP -

else if (days >=11 && days<=20)

9 STEP -

fine = ( 10*1) + ( days-10) *5

10 STEP -

else if (days > 20)

11 STEP -

fine = (10*1) + (10*5) + (days-20)*8;

12 STEP -

print Fine

End

Q.26-write a program to make a calculator.


import java.io.*;
class menu
{
public static void main(String args[])throws
Exception
{
BufferedReader obj=new BufferedReader(new

InputStreamReader(System.in));
System.out.print("\t1)\tAdd\n\t2)\tSub\n\t3)\tMulti
\n\t4)\tDivision\n\t Enter your choice: ");
int ch=Integer.parseInt(obj.readLine());
System.out.print("\t Enter 1st: ");
int a=Integer.parseInt(obj.readLine());
System.out.print("\t Enter 2nd: ");
int b=Integer.parseInt(obj.readLine());
if(ch==1)
System.out.print("\tAdd: "+(a+b));
if(ch==2)
System.out.print("\tSub: "+(a-b));
if(ch==3)
System.out.print("\tMUL: "+(a*b));
if(ch==4)
System.out.print("\tdiv: "+(a/b));
}
}
/* variable description
ch=to input choice.
a=to enter first no.
b=to enter second no. */

Algorithm

Start

1 STEP -

Use import java.io.*;

2 STEP -

Create BufferedReader object .

3 STEP 4 STEP

Display

("\t1)\tAdd\n\t2)\tSub\n\t3)\tMulti\n\t4)\tDivision\n\t
Enter your choice: ");
5 STEP - int ch=Integer.parseInt(obj.readLine());
6 STEP -

Display " Enter 1st

7 STEP -

int a=Integer.parseInt(obj.readLine());

8 STEP -

Display" Enter 2nd"

9 STEP -

int b=Integer.parseInt(obj.readLine());

10 STEP -

if(ch==1)

11 STEP -

then print Addition

12 STEP -

if(ch==2)

13 STEP -

then print Subtraction

14 STEP -

if(ch==3)

15 STEP -

then print MULtiply

16 STEP -

if(ch==4)

17 STEP -

then print division

End

Q 27-wap To enter a time in words and print it in


words.

import java.io.*;
class prog11
{
public static void main()throws Exception
{
DataInputStream d=new
DataInputStream(System.in);
int hr,mi,s,p;
String s1[]={"
","one","two","three","four","five","six","seven","eight",
"nine","ten","eleven","twelve","thirteen","fourteen","fif
teen","sixteen","seventeen","eighteen","nineteen"};
String s2[]={"
","twenty","Thirty","forty","fifty","Sixty"};
System.out.println("Enter the Time");
System.out.print("Hours : \t");
hr=Integer.parseInt(d.readLine());
System.out.print("Minutes :\t");
mi=Integer.parseInt(d.readLine());
System.out.print("Seconds :\t");
s=Integer.parseInt(d.readLine());
if(hr<=24 && hr>=0 && s>=0 && s<60 && mi>=0 &&
mi<60)
{
System.out.println("Time:");

if(hr>12 && hr<24)


System.out.print(s1[hr-12]+" Hours ");
else if(hr<13)
System.out.print(s1[hr]+" Hours ");
else
System.out.print("0 Hours ");
if(mi!=0)
{
if(mi>19)
{
p=mi/10;
System.out.print(s2[p-1]+" ");
p=mi%10;
if(p>0)
System.out.print(s1[p]+" Minutes ");
else
System.out.print(" Minutes ");
}
else
System.out.print(s1[mi]+" Minutes ");
}
if(s!=0)
{
if(s>19)
{

p=s/10;
System.out.print(s2[p-1]+" ");
p=s%10;
if(p>0)
System.out.print(s1[p]+" Seconds ");
else
System.out.print(" Seconds ");
}
else
System.out.print(s1[s]+" seconds ");
}
if(hr<12)
System.out.print("am");
else
{
if(mi==0 && s==0)
System.out.println();
else
System.out.print("pm");
}
}
else
System.out.println("Invalid Time");
}

Algorithm
Start

1 STEP -

Use import java.io.*;

2 STEP -

Create DataInputStream object.

3 STEP -

String s1[]={"

","one","two","three","four","five","six","seven","eight","nine",
"ten","eleven","twelve","thirteen","fourteen","fifteen","sixtee
n","seventeen","eighteen","nineteen"};
4 STEP - String s2[]={"
","twenty","Thirty","forty","fifty","Sixty"};
5 STEP - Display "Enter the Time"
6 STEP -

Print "Hours "

7 STEP -

hr=Integer.parseInt(d.readLine());

8 STEP -

Print"Minutes "

9 STEP -

mi=Integer.parseInt(d.readLine());

10 STEP -

Print"Seconds "

11 STEP -

s=Integer.parseInt(d.readLine());

12 STEP -

if(hr<=24 && hr>=0 && s>=0 && s<60 && mi>=0 &&

mi<60)
13 STEP -

then print "Time"

14 STEP -

if(hr>12 && hr<24)

15 STEP -

then print (s1[hr-12]+" Hours ")

16 STEP -

else if(hr<13)

17 STEP -

then print(s1[hr]+" Hours ")

18 STEP -

else

19 STEP -

print"0 Hours "

20 STEP -

if(mi!=0)

21 STEP -

then{

22 STEP -

if(mi>19)

23 STEP -

then p=mi/10;

24 STEP -

print(s2[p-1]+" ")

25 STEP -

p=mi%10

26 STEP -

if(p>0)

27 STEP -

print(s1[p]+" Minutes ");

28 STEP -

else

29 STEP -

print" Minutes "

30 STEP -

else

31 STEP -

print(s1[mi]+" Minutes ")

32 STEP -

if(s!=0)

33 STEP -

34 STEP -

if(s>19)

35 STEP -

36 STEP -

p=s/10

37 STEP -

print(s2[p-1]+" ")

38 STEP -

p=s%10;

39 STEP -

if(p>0)

40 STEP -

then print(s1[p]+" Seconds ")

41 STEP -

else

42 STEP -

print " Seconds "

43 STEP -

else

44 STEP -

print(s1[s]+" seconds ")

45 STEP -

if(hr<12)

46 STEP -

then print"am"

47 STEP -

else{

48 STEP -

if(mi==0 && s==0)

49 STEP -

then println()

50 STEP -

else

51 STEP -

print "pm"

52 STEP -

53 STEP -

else

54 STEP -

print "Invalid Time"

55 STEP -

End

File handling
Q.28- Write a program to write records in name.txt
file.

import java.io.*;
class filehandling
{
public static void main(String args[])throws
Exception
{
BufferedReader in=new
BufferedReader (newInputStreamReader(System.in));
String name;
FileWriter fw=new FileWriter(name.txt);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
for(int i=1;i<=10;i++)
{
name=in.readLine();
pw.println(name);
}
pw.close();
bw.close();
fw.close();
}
}

Algorithm

Start

1 STEP -

import java.io.*;

2 STEP -

Create BufferedReader object .

3 STEP -

String name;

4 STEP -

FileWriter fw=new FileWriter(name.txt);

5 STEP -

BufferedWriter bw=new

BufferedWriter(fw);
6 STEP - PrintWriter pw=new BufferedWriter(bw);
7 STEP -

repeat step 8&9 till i<=10.

8 STEP -

name=in.readLine();

9 STEP -

pw.println(name);

10 STEP -

pw.close();

11 STEP -

bw.close();

12 STEP -

fw.close();

End

Q.29-write a program using text file concept for the


following:
(a):- Create a text text file data.txt to input and
store N sentences.

(b):- Read the above text file to read sentences and


print the sentences and number of words in each
sentence.Print output in two columns.
Solution aimport java.io.*;
class filehandling
{
public static void main(String args[])throws
Exception
{
BufferedReader in=new
BufferedReader (newInputStreamReader(System.in));
String name;
FileWriter fw=new FileWriter(data.txt);
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
System.out.println( enter total no of sentences);
int N=Integer.parseInt(in.readLine());
for(int i=1;i<=N;i++)
{
name=in.readLine();
pw.println(name);
pw.close();
}
}

Solution bimport java.io.*;


class filehandling
{
public static void main(String args[])throws
Exception
{
BufferedReader in=new
BufferedReader (newInputStreamReader(System.in));
String name;
int w=0;
FileReader fw=new FileReader(data.txt);
BufferedReader bw=new BufferedReader(fw);
System.out.println(sentence \t\t\t Number of
words);
while((name.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(name);
w=st.countTokens();
System.out.println(name+ \t\t+w);
}
br.close();
}
}

Algorithm
Start
(a)
1 STEP -

import java.io.*;

2 STEP -

Create BufferedReader object .

3 STEP -

String name;

4 STEP -

FileWriter fw=new FileWriter(name.txt);

5 STEP -

BufferedWriter bw=new BufferedWriter(fw);

6 STEP -

PrintWriter pw=new BufferedWriter(bw);

7 STEP -

int N=Integer.parseInt(in.readLine());

8 STEP 9 STEP -

repeat step 8&9 till i<=N.


name=in.readLine();

10 STEP pw.println(name);
11 STEP - pw.close();

End
Start
(b)
1 STEP -

import java.io.*;

2 STEP -

Create BufferedReader object .

3 STEP -

String name;

4 STEP -

FileReader fw=new FileReader(data.txt);

5 STEP -

BufferedReader bw=new

BufferedReader(fw);
6 STEP - display sentence \t\t\t Number of words
7 STEP 8 STEP -

while((name.readLine())!=null)
StringTokenizer st=new

StringTokenizer(name);
9 STEP - w=st.countTokens();
10 STEP br.close();

End

Q.30- write a program using binary file concept to


create a binary file invoice.dat to input product
name,Quantity of product and unit price of product for
N products.
import java.io.*;

class filehandling
{
public static void main(String args[])throws
Exception
{
BufferedReader in=new
BufferedReader (newInputStreamReader(System.in));
String pname; int qty; double price;
FileOutputStream fos= new
FileOutputStram(invoice.dat);
DataOutputStream dos= new
DataOutputStram(invoice.dat);
System.out.println( input total number of products );
int N=Integer.parseInt(in.readLine());
for(int i=1;i<=N;i++)
{
System.out.println( input product name);
pname=in.readLine();
System.out.println( input quantity);
qty=Integer.parseInt(in.readLine());
System.out.println( input unit price);
price= Double.parseDouble(in.readLine());
dos.writeUTF(pname);
dos.writeInt(qty);
dos.writeDouble(price);

}
fos.close();
dos.close();
}
}

Algorithm
Start

1 STEP -

import java.io.*;

2 STEP -

Create BufferedReader object .

3 STEP -

String pname

4 STEP -

int qty

5 STEP -

double price

6 STEP -

FileOutputStream fos= new

FileOutputStram(invoice.dat);
7 STEP - DataOutputStream dos= new
DataOutputStram(fos);
8 STEP display input total number of products );
9 STEP - int N=Integer.parseInt(in.readLine());
10 STEP repeat step 11 to 16 till i<=N.
11 STEP display input product name
12 STEP pname=in.readLine();
13 STEP display input quantity

14 STEP qty=Integer.parseInt(in.readLine());
15 STEP display input unit price
16 STEP price= Double.parseDouble(in.readLine());
17 STEP dos.writeUTF(pname);
18 STEP dos.writeInt(qty);
19 STEP dos.writeDouble(price);
20 STEP fos.close();
21 STEP dos.close();

End

Q.31-Read binary file invoice.dat to calculate the


total cost of each product,discount as 20% on total
cost if it is more than 12000.00 otherwise no
discount.Calculate yhe net price to be paid .print cash
memo.
import java.io.*;
class filehandling
{
public static void main(String args[])throws
Exception
{

BufferedReader in=new
BufferedReader (newInputStreamReader(System.in));
FileInputStream fos= new
FileInputStram(invoice.dat);
DataInputStream dos= new DataInputStram(fos);
boolean eof=false;
while(!=eof)
{
try
{
String pname; int qty; double price,total,dis;
pname=fos.readUTF();
qty=fos.readInt();
price=fos.readDouble();
total=price*qty;
if(total>12000.0)
dis=(total*20)/100;
else
dis=0.0;
double net=total-dis;
System.out.println( CASH MEMO);
System.out.println( product name+pname);
System.out.println( quantity=+qty);
System.out.println( unit price +price);
System.out.println( total cost=+total);

System.out.println( Discount=+dis);
System.out.println( net amount=+net);
}
catch(EOFException e)
{
System.out.println(this is end of file);
eof=true;
}
}
}
}

Algorithm
Start

1 STEP -

import java.io.*;

2 STEP -

Create BufferedReader object .

3 STEP -

String pname

4 STEP -

int qty

5 STEP -

double price,total,dis

6 STEP -

FileInputStream fos= new

FileInputStram(invoice.dat);
7 STEP - DataOutputStream dos= new DataInputStram(fos);
8 STEP boolean eof=false;

9 STEP - while(!=eof)
10 STEP try
11 STEP Pname=fos.readUTF();
12 STEP Qty=fos.readInt();
13 STEP Price=fos.readDouble();
14 STEP Total=price*qty;
15 STEP If(total>12000.0)
16 STEP Dis=(total*20)/100;
17 STEP Double net=total-dis;
18 STEP Display CASH MEMO,productname, quantity,
unit price , total cost Discount, net amount
19 STEP catch(EOFException e)
20 STEP displaythis is end of file
21 STEP Eof=true;

End

Inheritance
Q.32- A class bank defines account holder related
informations such as name and account number while
another class deposit defines amount to make a fixed
deposit. The details of both the classes are given

below.
CLASS NAME : Bank
DATA MEMBERS:
Nam: String variables to store name of account holder.
Accno : long integer variable to store account number.
MEMBER METHODS:
Bank() : Constructor to store 0 to AccNo and blank to
nam.
Bank(long x,String na) : Constructor to assign x to
AccNo
And na to nam.
SUB CLASS NAME : Deposite
DATA MEMBERS:
amt : Double type variable to store amount.
MEMBER METHODS :
Deposit(double q) : Constructor to assign q to amt to
make fixed Deposit also make the data available to
class Bank.
void showDetails() : to display name of account holder,
account number, amount to be deposited to make fixed
deposit .
(a) : specify the class Bank giving details of
constructors.
(b) : Using concept of Inheritance , specify the
class Depositgiving details of constructor and

function void
showDetails(). Class Deposit is derived from
class Bank. Write the main function to input and
print all the data.
SolutionClass Bank
{
String nam;
long AccNo;
Bank()
{
nam= ;
AccNo=0;
}
Bank(long x, String na)
{
nam=na;
AccNo=x;
}
}
Class Deposit extends Bank
{
double amt;
Deposit(long x, String na,double q)
{

super(x , na);
amt = q;
}
Void showDetails()
{
System.out.println(name of account holder=+nam);
System.out.println(account number=+AccNo);
System.out.println(Fixed deposid amount=+amt);
}
}
Class result
{
public static void main(String args[])
{
Deposit bnk=new Deposit(11234,shubham,75000.0);
System.out.println(account details :);
bnk.showDetails();
}
}

Algorithm
Start
1 STEP -Class Bank

2 STEP -String nam;

3 STEP -long AccNo;


4 STEP -Bank()
5 STEP -nam= ;
6 STEP -AccNo=0;

7 STEP -Bank(long x, String na)


8 STEP -nam=na;
9 STEP -=x;

10 STEP -Deposit extends Bank

11 STEP -Deposit(long x, String na,double q)


12 STEP -super(x , na);
13 STEP -amt = q;
14 STEP -void showDetails()

15 STEP -Displayname of account holder


16 STEP -Displayaccount number
17 STEP -Display Fixed deposid amount
18 STEP -Class

result

19 STEP -Deposit bnk=new

Deposit(11234,shubham,75000.0);
20 STEP -Display account details
21 STEP -bnk.showDetails();

End

*** the following program is based on function


overloading in base and drived class**
Q.33- The details of classes findmax and find great
are given below :
CLASS NAME : findmax
DATA MEMBERS:
m , n: integer variables to store numbers.
MEMBER METHODS:
findmax() : Constructor to store 0 to m and n.
int Getmax : to assign x to m and y to n and return
greatest from m and n.
SUB CLASS NAME : findgreat
DATA MEMBERS:
x, y , z : integer variables to store numbers.
MEMBER METHODS :
FindGreat : Constructor to assign 0 to x , y , z.
int Getmax : to assign q to x, r to y, s to z. Find and
return greatest from x , y , z.
(a) : specify the class findmax giving details of
constructors and function in int Getmax( int x,
int y).
(b) : Using concept of Inheritance , specify the
class findgreat giving details of constructor and
function int Getmax( int q, int r, int s). Class
findgreat is derived from class findmax. Write

the main function to input and print all the data.


import java.io.*;
class findmax
{
int m,n;
findmax()
{
m=0,n=0;
}
int Getmax(int x, int y)
{
m=x ; n=y;
int g=Math.max(m,n);
return g ;
}
}
Class findgreat extends findmax
{
int x, y, z;
findgreat()
{
x=0 ; y=0; z=0;
}
int Getmax(int q, int r, int s)
{

x=q ; y=r ; z=s ;


if((x>y)&&(x>z))
return x;
else if((y>x)&&(y>z))
return y;
else
return z;
}
}
Class result
{
public static void main(String args[])
{
int n1=10, n2=70, n3=25;
findgreat st=new findgreat();
int p= st.Getmax(n2,n1);
int d= st.Getmax(n1,n2,n3);
System.out.println(output);
System.out.println(greatest from+n1+and+n2+=+p);
System.out.println(greatest
from+n1+and+n2+and+n3+=+d);
}
}
OutputGreatest between 10 and 70 = 70

Greatest among 10 , 70 and 25 = 70

Algorithm
Start

1 STEP -Use import java.io.*;


2 STEP -class findmax
3 STEP -int m,n;
4 STEP -findmax()
5 STEP -m=0,n=0;

6 STEP -int Getmax(int x, int y)


7 STEP -m=x ; n=y;
8 STEP -int g=Math.max(m,n);
9 STEP -return g ;

10 STEP -Class findgreat extends findmax


11 STEP -int x, y, z;

12 STEP -findgreat()
13 STEP -x=0 ; y=0; z=0;
14 STEP -int Getmax(int q, int r, int s)
15 STEP -x=q ; y=r ; z=s ;
16 STEP -if((x>y)&&(x>z))
17 STEP -return x;

18 STEP -else if((y>x)&&(y>z))


19 STEP -return y;
20 STEP -else
21 STEP -return z;
22 STEP -Class result

23 STEP -int n1=10, n2=70, n3=25;


24 STEP -findgreat st=new findgreat();
25 STEP -int p= st.Getmax(n2,n1);

26 STEP -int d= st.Getmax(n1,n2,n3);

27 STEP -Display Greatest between 10 and 70 = 70


28 STEP -Display Greatest among 10 , 70 and 25 = 70

End

***The following program is written using


abstract class concept.***
Q.34- The details of class number and data are given
below :
CLASS NAME : Number
DATA MEMBERS:
m , n: integer variables to store numbers.

MEMBER METHODS:
Number() : Constructor to store 25 to m and 400 to
n.
void readpoint : to print value of m using abstract
function concept.
void Show() :- to print value of n.
SUB CLASS NAME : Data
DATA MEMBERS:
x : double variable to store numbers.
MEMBER METHODS :
void assign() : to assign 45.22 to x .
void display() : to print value of x.
(a) : specify the class Numberspecify the class
Number using abstract class concept giving
details of constructors and functions void print()
and void Show().
(b) : Using concept of Inheritance , specify the
class Data giving details function void assign()
and void display(). Class Data is derived from
class Number. Write the main function to input
and print all the data.
import java.io.*;
abstract class Number
{

int m,n;
Number()
{
m=25;n=400;
}
abstract void print();
void show()
{
System.out.println(value of n of abstract base
class=+n);
}
}
class data extends Number
{
Double x;
void assign()
{
x=45.22;
}
void display()
{
System.out.println(value of x of derived class =+x);
}
void print()
{

System.out.println(value of m of abstract base


class=+m);
}
}
class result
{
public static void main(string args)
{
Data obj =new Data();
System.out.printin(Output);
Obj.Show();
Obj.assign();
Obj.print();
Obj.display();
}
}
1 STEP -Use import java.io.*;
2 STEP - abstract class Number
3 STEP -int m,n;
4 STEP - Number()
5 STEP - m=25;n=400;
6 STEP - abstract void print();
7 STEP - void show()
8 STEP display value of n

9 STEP - class data extends Number


10 STEP - void assign()
11 STEP - x=45.22;
12 STEP - void display()
13 STEP Display value of x
14 STEP - void print()

15 STEP Display value of m


16 STEP - class result

17 STEP - Data obj =new Data();


18 STEP - Obj.Show();

19 STEP - Obj.assign();
20 STEP - Obj.print();
21 STEP - Obj.display();
Q 35 write a program to print diamond matrix
class diamond
{
public static void main(int n)
{
int a[][]=new int[n][n];
int i,j,k=1,c=n/2,r=0;
for(i=n/2;i>0;i--)
{
for(j=0;j<i;j++)
a[r++][c--]=k++;

for(j=0;j<i;j++)
a[r++][c++]=k++;
for(j=0;j<i;j++)
a[r--][c++]=k++;
for(j=0;j<i;j++)
a[r--][c--]=k++;
r=r+1;
}
a[n/2][n/2]=k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=0)
System.out.print(a[i][j]+" ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
INPUT 5
OUTPUT
1
2 9 8
3 10 13 12 7
4 11 6
5

Algorithm
1 STEP

start and initialize the variables

2 STEP for loop with i for rows

3 STEP for loop with J for columns


4 STEP for loop for space on upper side
5 STEP for loop for space on lower side
6 STEP print the pattern
7 STEP end

End

THEEND
Further suggestions
are welcome.