Computer Science Project 12th
Computer Science Project 12th
SCIENCE
PROJECT
Contents
NO.
PROGRAM
PAGE NO.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
MagicSquare
SpiralMatrix
4
7
10
12
14
16
19
22
24
26
29
32
35
38
41
43
45
47
49
PascalsTriangle
Linear Search
Binary Search
Selection Sort
Bubble Sort
Date Program
Decimal to Binary Number
Replacing Vowels with *
Word Count
Number of Vowels and Consonants
String in Alphabetical Order
20
21
22
23
24
25
26
27
28
29
30
Decoding of String
Word Search in String
Frequency of Each String Character
Palindrome Check
Star Pattern Using Input String
Sum of All Matrix Elements
Sum of Matrix Column Elements
Sum of Matrix Diagonal Elements
Sales commission
Decimal to Roman Numerical
Celsius to Fahrenheit (Using
Inheritance)
52
55
57
60
62
64
67
70
73
75
77
ACKNOWLEDGEMENTS
First of all, Id like to thank my parents for
helping me out with the project.
Secondly, Id like to thank our Computer
teachers Mr. Akhilesh Bajpai for helping us
with the programs.
Lastly, Im really grateful to Tabish Haider
Rizvi whose help was imperative for myself
making this project.
PROGRAM 1:
3
Magic Square
ALGORITHM
STEP 1 - START
STEP 2 - arr[][]=new int[n][n],c=n/2-1,r=1,num
STEP 3 - IF num=1;num<=n*n;num++ GOTO STEP 4
STEP 4 - r--,c++
STEP 5 - IF r==-1 GOTO STEP 6
STEP 6 - r=n-1
STEP 7 - IF c>n-1 GOTO STEP 8
STEP 8 - c=0
STEP 9 - IF arr[r][c]!=0 GOTO STEP 10
STEP 10 - r=r+2 & c
STEP 11 - num++ & IF num<=n*n GOTO STEP 4
STEP 12 - arr[r][c]=num
STEP 13 - IF r==0&&c==0 GOTO STEP 14
STEP 14 - r=n-1, c=1 & arr[r][c]=++num
STEP 15 - IF c==n-1&&r==0 GOTO STEP 16
STEP 16 - arr[++r][c]=++num
STEP 17 - PRINT ()
Solution:
/*A Magic Square is a square whose sum of diagonal elements, row elements and
coloumn elements is the same*/
import java.io.*;
class MagicSquare
{
public static void main(String args[])throws IOException //main function
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the dimension of magical square=");
int n = Integer.parseInt(br.readLine()); //accepting dimensions
int arr[][]=new int[n][n],c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++) //loop for finding magic square elements
{
r--;c++;
if(r==-1)r=n-1;
if(c>n-1)c=0;
if(arr[r][c]!=0)
{
r=r+2;c--;
}
arr[r][c]=num;
if(r==0&&c==0)
{
r=n-1;c=1;
arr[r][c]=++num;
}
if(c==n-1&&r==0)
arr[++r][c]=++num;
}
System.out.println();
Variable Description
NO.
1
2
3
4
5
6
NAME
br
n
arr
num
R
C
Output
TYPE
METHOD
DESCRIPTION
BufferedReader
int
Int[][]
int
int
int
main()
main()
main()
main()
main()
main()
BufferedReader object
Input dimensions
Magic square matrix
Loop variable for magic
Row
column
PROGRAM 2:
Spiral Matrix
Algorithm
STEP
STEP
STEP
STEP
STEP
1
2
3
4
5
START
- INPUT a[][]
- IF p!=(int)Math.pow(l,2) GOTO STEP 4
- IF co!=0 GOTO STEP 5
- re=1
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
solution
import java.io.*;
class SpiralMatrix
{
public static void main(String[] args) throws IOException //main function
{
int a[][],r,c,k1=2,k2=3,p=0,co=0,re=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the dimension of matrix A x A =");
int l = Integer.parseInt(br.readLine()); //accepting dimension of square spiral matrix
a=new int[l][l];
r=l/2;c=r-1;
if(l%2==0)
{System.out.println("wrong entry for spiral path");
System.exit(0);}/*Calculating and displaying spiral matrix*/
while(p!=(int)Math.pow(l,2))
{
if(co!=0)
re=1;
for(int ri=1;ri<=k1-re;ri++)
{p++;c++;
if(c==l)
break;
a[r][c]=p;}
if(c==l)
break;
for(int dw=1;dw<=k1-1;dw++)
{
p++;r++;a[r][c]=p;
}
for(int le=1;le<=k2-1;le++)
{
p++;c--;a[r][c]=p;
}
for(int up=1;up<=k2-1;up++)
{
p++;r--;a[r][c]=p;
}
k1=k1+2;k2=k2+2;co++;
}
for(int y=0;y<l;y++)
{for(int yy=0;yy<l;yy++)
System.out.print("\t"+a[y][yy]);
System.out.println();
System.out.println();
}}}
Variable Description
NO.
NAME TYPE
METHOD
DESCRIPTION
1
2
3
4
5
6
7
8
9
10
11
br
a
r
c
k1
k2
p
co
re
l
ri
BufferedReader
int[][]
int
int
int
int
int
int
int
int
int
main()
main()
main()
main()
main()
main()
main()
main()
main()
main()
main()
12
le
int
main()
13
dw
int
main()
14
up
int
main()
15
int
main()
16
yy
int
main()
BufferedReader object
spiral matrix
r-1
stores 2
stores 3
loop gate
column index
row index
dimensions of the matrix
right side matrix loop
variable
left side matrix loop
variable
down side matrix loop
variable
up side matrix loop
variable
loop variable to print
matrix
loop variable to print
matrix
Output
PROGRAM 3:
GCD (Using Recursion)
Algorithm
STEP 1 START
STEP 2 - INPUT p,q
STEP 3 - IF(q=0) THEN return p OTHERWISE return calc(q,p%q)
STEP 4 END
Solution
import java.io.*;
class GCD
{
public static void main(String args[]) throws IOException //mainfunction
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the numbers =");
int p = Integer.parseInt(br.readLine());
int q = Integer.parseInt(br.readLine());
GCD obj = new GCD();
int g = obj.calc(p,q);
System.out.println("GCD ="+g);//accepting nos.
}
public int calc(int p,int q)
{
if(q==0)
return p;
else return calc(q,p%q);
}}
Variable Description
NO. NAME
1
br
2
TYPE
BufferedRead
er
int
int
4
5
obj
g
GCD
int
Output
METHOD
main()
DESCRIPTION
BufferedReader object
main() ,
calc()
main() ,
calc()
main()
main()
input number
input number
GCD object
variable storing theGCD
PROGRAM 4:
Fibonacci Series (Using Recursion)
Algorithm
STEP 1 - START
STEP 2 - INPUT n
STEP 3 - IF(n<=1) THEN return 1 OTHERWISE return (fib(n-1) +fib(n-2))
STEP 4 END
Solution
import java.io.*;
class Fibonacci
{
public static void main(String args[]) throws IOException
//main function
{
Fibonacci obj = new Fibonacci();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no of term =");
//accepting no. of terms
int n = Integer.parseInt(br.readLine());
System.out.println();
for(int i=1;i<=n;i++)
//Fibonacci element display loop
{
int f = obj.fib(i);
System.out.print(f+" ");
}}
public int fib(int n)
//Recursive function fib() for calculation of Fibonacci element
{
if(n<=1)
return n;
elsereturn (fib(n-1) +fib(n-2));
}}
Variable Description
NO. NAME
1
br
2
3
4
obj
n
i
TYPE
BufferedRea
der
Fibonacci
int
int
5
6
f
n
int
int
Output
METHOD
main()
DESCRIPTION
BufferedReader object
main()
main()
main()
main()
main()
Fibonacci Object
input number
loop variable for fibonacci
element
Fibonacci element
recursive function fib()
parameter
PROGRAM 5:
Factorial (Using Recursion)
Algorithm
STEP 1 START
STEP 2 - INPUT n
STEP 3 - IF(n<2) THEN return 1 OTHERWISE return (n * fact(n-1))
STEP 4 END
Solution
import java.io.*;
class Factorial
{
public static void main(String args[]) throws IOException
//main function
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no =");
int n = Integer.parseInt(br.readLine());
Factorial obj = new Factorial();
long f = obj.fact(n);
System.out.println("Factorial ="+f);
}
public long fact(int n)
{
if(n<2)
//accepting no.
//displaying factorial
//recursive fact()
return 1;
else return (n*fact(n-1));
}}
Variable Description
NO.
1
2
3
4
5
Name
br
n
obj
f
n
Output
Type
BufferedReader
int
Factorial
long
int
Method
main()
main()
main()
main()
main()
Description
BufferedReader object
input number
Factorial object
variable storing factorial
parameter in recursive function
fact()
PROGRAM 6:
Calendar of Any Month Of any Year
Algorithm
STEP
STEP
STEP
STEP
STEP
STEP
1
2
3
4
5
6
START
- INPUT int month,int year
- int i,count=0,b,c,d=1 & String w="SMTWTFS"
- IF (year%100==0 && year%400==0) || (year%100!=0 && year%4==0)
- days[1]=29
- PRINT "================The Calendar of"+month1[month-1]+"
"+year+"is==================")
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
11
12
13
14
15
count+=2
count+=1
IF i=0 GOTO STEP 14
count+=days[i] , count+=1, count%=7 & b=7-count
IF b!=1 || b!=7 GOTO STEP 16
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
16
17
18
19
20
21
22
23
24
25
26
Solution
import java.io.*;
class Calendar
{
public void dee()throws IOException
//dee() function
{
int i,count=0,b,d=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(Enter month);
int month=Integer.parseInt(br.readLine());
System.out.println(Enter Year);
int year=Integer.parseInt(br.readLine());
/* Computing and displaying calendar*/
String w="SMTWTFS";
int days[]={31,28,31,30,31,30,31,31,30,31,30,31 //accepting month and year
};
String
month1[]={"January","February","March","April","May","June","July","August","September","October","November",
"December"};
for(i=0;i<w.length();i++)
System.out.print(w.charAt(i)+"\t");
System.out.println(" ");
for(i=1;i<year;i++)
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0)) count+=2;
else count+=1; for(i=0;i<month;i++) count+=days[i]; count+=1; count%=7; b=7count;
if(b!=1 || b!=7)
while(count>0)
{
System.out.print(' '+"\t"); count--;
}
for(i=1;i<7;i++)
{
while(b>0 && d<=days[month-1])
{
System.out.print(d+"\t");
d++;
b--;
}
b=7;
System.out.println(" ");
}}}
Variable Description
NO.
1
2
3
4
5
6
7
8
9
10
NAME
br
i
count
b
d
month
year
w
days
month1
TYPE
BufferedReader
int
int
int
int
int
int
String
String[]
String[]
Output
METHOD
dee()
dee()
dee()
dee()
dee()
dee()
dee()
dee()
dee()
dee()
DESCRIPTION
BufferedReader Object
loop variable
counter
week counter
day counter
input month
input year
week days
array storing days
array storing months
PROGRAM 7:
A.P. Series and its Sum
Algorithm
STEP 1 - START
STEP 2 - a = d = 0
STEP 3 - IMPORT a, d
STEP 4 - this.a = a & this.d = d
STEP 5 - IMPORT n
STEP 6 - RETURN (a+(n-1)*d)
STEP 7 - IMPORT n
STEP 8 - RETURN (n*(a+nTHTerm(n))/2)
STEP 9 - IMPORT n
STEP 10 - PRINT \n\tSeries\n\t"
STEP 11 - IF i=1;i<=n;i++ GOTO STEP 12
STEP 12 - PRINT nTHTerm(i)+" "
STEP 13 - i++ & IF i<=n GOTO STEP 12
STEP 14 - PRINT n\tSum : "+Sum(n)
Solution
STEP 15 END
class APSeries
{
private double a,d;
APSeries()
//default constructor
{
a = d = 0;
}
APSeries(double a,double d)
//parameterized constructer
{
this.a = a;
this.d = d;
}
double nTHTerm(int n)
{
return (a+(n-1)*d);
}
double Sum(int n)
//function calculating sum
{
return (n*(a+nTHTerm(n))/2);
}
void showSeries(int n)
//displaying AP Series
{System.out.print("\n\tSeries\n\t");
for(int i=1;i<=n;i++)
{
System.out.print(nTHTerm(i)+" ");
}
System.out.print("\n\tSum :"+Sum(n));
}
}
void main()throws IOException
//main function
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 1st term");
st
a=Integer.parseInt(br.readLine());
//accepting 1 term
System.out.println("Enter Common difference");
d=Integer.parseInt(br.readLine());
//accepting common difference
System.out.println("Enter no.of terms");
int n=Integer.parseInt(br.readLine());
//accepting no. of terms
nTHTerm(n);
Sum(n);
showSeries(n);
}
Variable Description
NO.
NAME
1
2
3
a
d
n
TYP
E
int
int
int
int
Output
METHOD
DESCRIPTION
sum(), showSeries(),
nTHTerm()
ShowSeries
1st term
common difference
total terms
loop variable
PROGRAM 8:
Number In Words
Algorithm
STEP 1 - START
STEP 2 - INPUT amt
STEP 3 - z=amt%10 , g=amt/10
STEP 4 - IF g!=1 THEN GOTO STEP 5 OTHERWISE GOTO STEP 6 STEP 5 - PRINT x2[g-1]+"
"+x1[z]
STEP 6 - PRINT x[amt-9]
STEP 7 END
Solution
import java.io.*;
class Num2Words
{
public static void main(String args[])throws IOException
//main
function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any Number(less than 99)");
int amt=Integer.parseInt(br.readLine());
//accepting number
int z,g;
String
x[]={,"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nin
eteen"};
String x1[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
String x2[]={"","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
z=amt%10;
//finding the number in words
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]);
else System.out.println(x[amt-9]);
Variable Description
}}
NO.
NAME TYPE
METHOD
DESCRIPTION
1
2
3
4
br
z
g
x
BufferedReader
int
int
String[]
main()
main()
main()
main()
x1
String[]
main()
x2
String[]
main()
amt
int
main()
BufferedReader object
amt%10
amt/10
String array storing no.in
words
String array storing no.in
words
String array storing no.in
words
input number
Output
PROGRAM 9:
Pascals Triangle
Algorithm
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
STEP
1 - START
2 - pas[0] = 1
3 - IF i=0 THEN GOTO STEP 4
4 - IF j=0 THEN GOTO STEP 5
5 - PRINT pas[j]+" "
6 - i++& IF i<n GOTO STEP 4
7 - j=0 & IF j<=i GOTO STEP 5
8 - IF j=i+1 THEN GOTO STEP 7
9 - pas[j]=pas[j]+pas[j-1]
10 - j--& IF j>0 GOTO STEP 9
11 END
Solution
import java.io.*;
class Pascal
{
public void pascalw()throws IOException
//pascalw() function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(Enter a no.);
int n=Integer.parseInt(br.readLine());
//accepting value
int [ ] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++)
//loop evaluating the elements
{for (int j=0; j<=i; ++j)
System.out.print(pas[j]+" ");
//printing the Pascal Triangle
elements
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}}}
Variable Description
NO.
NAME TYPE
METHOD
DESCRIPTION
1
2
3
br
n
pas
BufferedReader
int
int[]
pascalw()
pascalw()
pascalw()
4
5
i
j
int
int
pascalw()
pascalw()
BufferedReader object
input value
matrix storing pascal
numbers
Loop variable
Loop variable
Output
PROGRAM 10:
Linear Search
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1
STEP 6 - FROM i=0 to i<n REPEAT STEP 7
STEP 7 - IF (a[i] == v) THEN flag =i
STEP 8 - IF (flag=-1) THEN GOTO STEP 9 OTHERWISE GOTO STEP 10 STEP 9 - PRINT not
found
STEP 10 - PRINT v+" found at position - "+flag
STEP 11 END
Solution
import java.io.*;
class LinearSearch
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public LinearSearch(int nn)
{
n=nn;
}
public void input() throws IOException
//function for obtaining values from
user
{
System.out.println("enter elements");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}}
public void display()
//function displaying array values
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}}
public void search(int v)
//linear search function
{
int flag=-1;
for(int i=0; i<n ; i++)
{
if(a[i] == v)
flag =i;
}
if(flag== -1 )
System.out.println("not found");
else System.out.println(v+" found at position - "+flag);
}
public static void main(String args[]) throws IOException
//main function
{
LinearSearch obj = new LinearSearch(10);
obj.input();
obj.display();
System.out.println("enter no. to be searched -");
//accepting values to be
searched
int v = Integer.parseInt(br.readLine());
obj.search(v);
Variable Description
}}
NO.
1
2
3
4
5
NAME
br
n
i
a[]
nn
TYPE
BufferedReader
int
int
int[]
int
int
7
8
flag
obj
int
LinearSearch
Output
METHOD
LinearSearc
h()
Search(),ma
in()
Search()
main()
DESCRIPTION
BufferedReader Object
array length
loop variable
input array
parameter in constructor
search element
flag
linearSearch object
PROGRAM 11:
Binary Search
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1 , l=0, u=n-1
STEP 6 - IF(l<=u && flag=-1) REPEAT STEP 7 AND Step 8
STEP 7 - m = (l+u)/2
STEP 8 - IF (a[m] == v) THEN flag =m OTHERWISE GOTO STEP 9
STEP 9 - IF (a[m] < v) THEN l = m+1 OTHERWISE u =m-1
STEP 10 - IF (flag=-1) THEN GOTO STEP 11 OTHERWISE GOTO STEP 12
STEP 11 - PRINT not found
STEP 12 - PRINT v+" found at position - "+flag
STEP 13 - END
Solution
import java.io.*;
class BinarySearch
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public BinarySearch(int nn)
//default constructor
{
n=nn;
}
public void input() throws IOException
{
System.out.println("enter elements");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}}
public void display()
//displaying array elements
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}}
public void search(int v)
//function to search array elements using binary search technique
{
int l=0;
int u = n-1;
int m;
int flag=-1;
while( l<=u && flag == -1)
{
m = (l+u)/2;
if(a[m] == v)
flag = m;
else if(a[m] < v)
l = m+1;
else u = m-1;
}
if(flag== -1 )
System.out.println("not found");
else System.out.println(v+" found at position - "+flag);
}
public static void main(String args[]) throws IOException
{
BinarySearch obj = new BinarySearch(10);
obj.input();
obj.display();
System.out.println("enter no. to be searched -");
int v = Integer.parseInt(br.readLine());
//accepting integer to be searched by binary search
obj.search(v);
}}
Variable Description
NO.
1
2
3
4
5
NAME
br
n
i
a[]
nn
TYPE
BufferedReader
int
int
int[]
int
int
7
8
9
10
11
flag
l
u
m
obj
int
int
int
int
BinarySearch
Output
METHOD
BinarySearc
h()
Search(),ma
in()
Search()
Search()
Search()
Search()
main()
DESCRIPTION
BufferedReader Object
array length
loop variable
input array
parameter in constructor
search element
flag
lower limit
upper limit
middle index
BinarySearch object
PROGRAM 12:
Selection Sort
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1
STEP 6 - FROM i=0 to i<n-1 REPEAT STEP 7 to STEP 11
STEP 7 - min =i
STEP 8 - FROM j=i+1 to j<n REPEAT STEP 8
STEP 9 - IF(a[j]<a[min]) then min =j
STEP 10 - IF (min!=i) GOTO STEP 11
STEP 11 - temp = a[i], a[i] =a[min], a[min] = temp
Solution
import java.io.*;
class SelectionSort
{
int n,i;
int a[] = new int[100];
public SelectionSort(int nn)
//parameterized constructor
{
n=nn;
}
public void input() throws IOException
{BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter elements");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}}
public void display()
{System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}}
public void sort()
{
int j,temp,min;
for(i=0;i<n-1;i++)
{
min =i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
min =j;
}
if(min!=i)
{
temp = a[i];
a[i] =a[min];
a[min] = temp;
}}}
public static void main(String args[]) throws IOException
{
SelectionSort x = new SelectionSort(5);
x.input();
System.out.print("Before sorting - ");
x.display();
System.out.print("After sorting - ");
x.sort();
x.display();
}}
Variable Description
NO.
1
2
3
4
5
NAME
br
n
i
a[]
nn
TYPE
BufferedReader
int
int
int[]
int
6
7
8
9
j
temp
min
x
int
int
int
Selection sort
Output
METHOD
input[]
SelectionSo
rt()
Sort()
Sort()
Sort()
main()
DESCRIPTION
BufferedReader Object
array length
loop variable
input array
parameter in constructor
sort index
temporary storage
minimum value
SelectionSort object
PROGRAM 13:
BUBBLE Sort
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1
STEP 6 - FROM i=0 to i<n-1 REPEAT STEP 7 to STEP 9
STEP 7 - FROM j=i+1 to j<n REPEAT STEP 8
STEP 8 - IF(a[j] > a[j+1]) THEN GOTO STEP 9
STEP 9 - temp = a[i], a[i] =a[min], a[min] = temp
STEP 10 - END
Solution
import java.io.*;
class BubbleSort
{
int n,i;
int a[] = new int[100]; public BubbleSort(int nn)
{
n=nn;
//parameterized constructor
}
public void input() throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter elements");
for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}}
public void display()
//function displaying array elements
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}}
public void sort()
{
int j,temp;
for(i=0 ; i<n-1 ; i++)
//main function
Variable Description
}}
NO.
1
2
3
4
5
NAME
br
n
i
a[]
nn
TYPE
BufferedReader
int
int
int[]
int
6
7
8
j
temp
x
int
int
SelectionSort
Output
METHOD
input
BubbleSort(
)
Sort()
Sort()
main()
DESCRIPTION
BufferedReader Object
array length
loop variable
input array
parameter in constructor
sort index
temporary storage
SelectionSort object
PROGRAM 14:
Date Program
Algorithm
STEP 1 - START
STEP 2 - INITIALISE a[ ] , m[ ]
STEP 3 - INPUT n , yr
STEP 4 - IF ( yr%4=0) THEN a[1] = 29
STEP 5 - t =0 , s = 0
STEP 6 - IF ( t<n) REPEAT STEP 7
STEP 7 - t =t + a[s++]
STEP 8 - d = n + a[--s] - t
STEP 9 - IF ( d ==1|| d == 21 || d == 31 ) then PRINT d + "st" + m[s] + " , "+yr
STEP 10 - IF ( d ==2|| d == 22 ) then PRINT d + "nd" + m[s] + " , "+yr
STEP 11 - IF ( d ==3|| d == 23 ) then PRINT d + "rd" + m[s] + " , "+yr OTHERWISE GOTO STEP 12
Solution
import java.io.*;
class Day2Date
{
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public void calc(int n, int yr)
//function to calculate date
{
int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ;
String m[ ] = { "Jan", "Feb",
"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" } ;
if ( yr % 4 == 0)
a[1] =29;
int t=0,s=0;
while( t < n)
//loop calculating date
{
t =t + a[s++];
}
int d = n + a[--s] - t;
if( d == 1|| d == 21 || d == 31 )
{
System.out.println( d + "st" + m[s] + " , "+yr);
}
if( d == 2 || d == 22 )
{
System.out.println( d + "nd" + m[s] + " , "+yr);
}
if( d == 3|| d == 23 )
{
System.out.println( d + "rd" + m[s] + " , "+yr);
}
else {System.out.println( d + "th" + m[s] + " , "+yr);
}}
public static void main(String args[]) throws IOException
//main function
{
Day2Date obj = new Day2Date();
System.out.println( "Enter day no = ");
//accepting day no.
int n = Integer.parseInt(br.readLine());
System.out.println( "Enter year = ");
//accepting year
int yr = Integer.parseInt(br.readLine());
obj.calc(n,yr);
Variable Description
}}
NO.
1
2
NAME
br
n
TYPE
BufferedReader
int
yr
int
4
5
6
7
8
9
a
m
t
s
d
obj
int[]
int[]
int
int
int
Day2Date
Output
METHOD
calc(),
main()
calc(),
main()
calc()
calc()
calc()
calc()
calc()
main()
DESCRIPTION
BufferedReader Object
Day number
year
array storing day
array storing month
array index
array index
n+a[--s]+t
Day2Date object
PROGRAM 15:
Decimal to Binary
Algorithm
STEP 1 - START
STEP 2 - n = 30
STEP 3 - INPUT int no
STEP 4 - c =0 , temp = no
STEP 5 - IF (temp!=0) REPEAT STEP 6
STEP 6 - a[c++] = temp%2, temp = temp / 2
STEP 7 - FROM i=c-1 to i>0 REPEAT STEP 8
STEP 8 - PRINT a[i]
STEP 9 END
Solution
import java.io.*;
class Dec2Bin
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public Dec2Bin(int nn)
//parameterized contructor
{
n=nn;
}
public void dectobin(int no)
//function converting decimalto binary number
{
int c = 0;
int temp = no; while(temp != 0) {a[c++] = temp % 2; temp = temp / 2;
}
System.out.println("Binary eq. of "+no+" = ");
for( i = c-1 ; i>=0 ; i--)
//Displaying binary number
System.out.print( a[ i ] );
}
public static void main(String args[]) throws IOException
//main
function
{
Dec2Bin obj = new Dec2Bin(30);
System.out.println("enter decimal no -");
int no = Integer.parseInt(br.readLine());
obj.dectobin(no);
}}
Variable Description
NO.
1
NAME
br
2
3
4
5
6
n
i
a[]
nn
no
TYPE
BufferedRead
er
int
int
int[]
int[]
int
7
8
9
temp
c
obj
int
int
Dec2Bin
Output
METHOD
DESCRIPTION
BufferedReader Object
Dec2Bin()
main(),
dectobin()
dectobin()
dectobin()
main()
array length
loop variable
array storing binary no.
parameter in constructer
input number
temporary storage
counter
Dec2Bin object
PROGRAM 16:
Replacing Vowels with *
Algorithm
STEP 1 - START
STEP 2 - a = "Computer Applications"
STEP 3 - x= 0
STEP 4 - FROM z =0 to z<a.length() REPEAT STEP 5
STEP 5 -if(a.charAt(z)=='a'||a.charAt(z)=='e'||a.charAt(z)=='i'||a.charAt(z)=='o'||a.charAt(z)=='u) THEN a.setCharAt(z,'*')
STEP 6 - PRINT "New String -"+a
STEP 7 END
Solution
import java.io.*;
class VowelReplace
{
public static void main(String args[])throws IOException
//main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(Enter a String);
StringBuffer a=new StringBuffer(br.readLine());
//accepting a string
Variable Description
NO.
1
NAME
br
2
3
a
z
TYPE
BufferedRead
er
StringBuffer
int
Output
METHOD
main()
DESCRIPTION
BufferedReader Object
main()
main()
PROGRAM 17:
Word Count
Algorithm
STEP 1 - START
STEP 2 - a = "Computer Applications"
STEP 3 - z = a.length()
STEP 4 - x= 0
STEP 5 - FROM y =0 to y<z REPEAT STEP 6
STEP 6 - IF (a.charAt(y)==' ' ) then x =x+1
STEP 7 - PRINT "Number of words in string ="+(x+1)
STEP 8 END
Solution
import java.io.*;
class NoOfWords
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Sentence");
String a=br.readLine();
//accepting string
System.out.println("The string is -"+a);
int z=a.length(),y,x=0;
for(y=0;y<z;y++)
//loop for counting number of
spaces
{
if(a.charAt(y)==' ')
x=x+1;
}
System.out.println("Number of words in string ="+(x+1));
//displaying result
}}
Variable Description
NO.
1
NAME
br
2
3
4
5
z
a
x
y
TYPE
BufferedRead
er
int
String
int
int
Output
METHOD
main()
DESCRIPTION
BufferedReader Object
main()
main()
main()
main()
length of string
input string
space counter
loop variable
PROGRAM 18:
Number of Vowels and Consonants
Algorithm
STEP 1 - START
STEP 2 - a = "Computer Applications"
STEP 3 - z = a.length()
STEP 4 - x= 0 , b= 0
STEP 5 - FROM y =0 to y<z REPEAT STEP 6
STEP 6 - IF(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.charAt(y)=='o'||a.charAt(y)=='u') THEN x =x +1 OTHERWISE b= b+1
STEP 7 - PRINT x
STEP 8 - PRINT b
STEP 9 END
Solution
import java.io.*;
class Vowels
{
public static void main(String args[])throws IOException
//main
function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string");
String a= br.readLine();
//Accepting string
int z=a.length(),y,x=0,b=0;
for(y=0;y<z;y++)
//loop for counting
number of vowels
{
if(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.charAt(y)=='o'||
a.charAt(y)=='u')
x++;
else b++;
}
System.out.println("Number of vowels in string ="+x);
//displaying result
System.out.println("Number of consonants in string ="+b);
}}
Variable Description
NO.
1
NAME
br
2
3
4
5
6
a
z
y
b
x
TYPE
BufferedRead
er
String
int
int
int
int
Output
METHOD
main()
DESCRIPTION
BufferedReader Object
main()
main()
main()
main()
main()
input String
length of string
loop variable
no. of consonants
no. of vowels
PROGRAM 19:
String in Alphabetical order
Algorithm
STEP 1 - START
STEP 2 - str = "" , l = 0
STEP 3 - INPUT string str
STEP 4 - l =str.length()
STEP 5 - FROM i=0 to i<l REPEAT STEP 6
STEP 6 - c[i] = str.charAt(i)
STEP 7 - FROM i=0 to i<l-1 REPEAT STEP 8
STEP 8 - FROM j=0 to i<l-1 REPEAT STEP 9
STEP 9 - temp =c[j], c[j] = c[j+1] , c[j+1] = temp
STEP 10 - FROM i=0 to i<l REPEAT STEP 11
STEP 11 - PRINT c[i]
STEP 12 END
Solution
import java.io.*;
class Alpha
{
String str;
int l;
char c[] = new char[100];
public Alpha()
//Alpha() constructor
{
str = "";
l =0;
}
public void readword() throws IOException
//function to read input string
{
System.out.println("enter word - ");
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
l = str.length();
}
public void arrange()
//function to arrange string in ascending
order
{
int i,j;
char temp;
for(i=0;i<l;i++)
{
c[i]= str.charAt(i);
}
for(i=0;i<l-1;i++)
//loops for swapping of characters
{
for(j=0;j<l-1-i;j++)
{
if(c[j] > c[j+1])
{
temp = c[j];
c[j] = c[j+1];
c[j+1] = temp;
}}}}
public void display()
//function to display rearranged string
{
System.out.println();
for(int i=0;i<l;i++)
{System.out.print(c[i]);
}}
public static void main(String args[]) throws IOException
//main function
{
Alpha obj = new Alpha();
obj.readword();
obj.arrange();
obj.display();
}}
Variable Description
NO.
1
NAME
br
2
3
4
5
6
7
8
str
l
c
i
j
temp
obj
TYPE
BufferedRead
er
String
int
char[]
int
int
char
Alpha
Output
METHOD
readword()
DESCRIPTION
BufferedReader Object
readword()
readword()
readword()
main()
input String
length
character array
loop variable
loop variable
temporary storage
alpha object
PROGRAM 20:
Decoding of String
Algorithm
STEP 1 - START
STEP 2 - INPUT name, n
STEP 3 - l=name.length()
STEP 4 - PRINT original string is "+name
STEP 5 - IF i=0 THEN GOTO STEP 6
STEP 6 - char c1=name.charAt(i)
STEP 7 - c=(int)c1
STEP 8 - IF n>0 THEN GOTO STEP 9 THERWISE GOTO STEP 12
STEP 9 - IF (c+n)<=90 THEN GOTO STEP 10 OTHERWISE GOTO STEP 11
STEP 10 - PRINT (char)(c+n)
STEP 11 - c=c+n;c=c%10,c=65+(c-1) & PRINT (char)(c)
STEP 12 - ELSE IF n<0 THEN GOTO STEP 13 OTHERWISE GOTO STEP 19
STEP 13 - n1=Math.abs(n)
STEP 14 - IF (c-n1) >=65 THEN GOTO STEP 15 OTHERWISE GOTO STEP 16
Solution
import java.io.*;
class Decode
{
public void compute()throws IOException
//compute() function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(Enter name:);
String name=br.readLine();
System.out.println(Enter number:);
int n=Integer.parseInt(br.readLine());
int j,i,l,c=0,y,n1;
l=name.length();
System.out.println("original string is "+name);
for(i=0;i<l;i++)
{
char c1=name.charAt(i);
try
//trying for NumberFormatException
{
c=(int)c1 ;
}
catch(NumberFormatException e)
{}
if(n>0)
{
if((c+n)<=90)
/*Decoding String*/
System.out.print((char)(c+n));
else
{
c=c+n;
c=c%10;
c=65+(c-1);
System.out.print((char)(c));
}}
else if(n<0)
{
n1=Math.abs(n);
if((c-n1) >=65)
System.out.print((char) (c-n1));
else
{
if(c>65)
c=c-65;
else c=n1;
System.out.print((char)(90-(c-1)));
}}
else if (n==0)
{
System.out.println("no change "+name);
break;
}}}}
Variable Description
NO.
1
2
3
4
5
6
7
8
9
10
11
NAME
br
name
n
j
i
l
c
y
n1
c1
e
TYPE
BufferedReader
String
int
int
int
int
int
int
int
char
NumberFormatExce
ption
Output
METHOD
compute()
compute()
compute()
compute()
compute()
compute()
compute()
compute()
compute()
compute()
compute()
DESCRIPTION
BufferedReader Object
input String
decode number
loop variable
loop variable
length of string
ASCII of c1
character at index i
NumberFormatException object
PROGRAM 21:
Word Search In String
Algorithm
STEP 1 - START
STEP 2 - INPUT string s
STEP 3 - StringTokenizer st = s
STEP 4 - l =str.length()
STEP 5 - INPUT look
STEP 6 - flag = -1
STEP 7 - IF (st.hasMoreElements()) REPEAT STEP 8
STEP 8 - IF (look.equals(st.nextElement())) THEN flag =1
STEP 9 - IF flag = - 1 GOTO STEP 10 OTHERWISE STEP 11
STEP 10 - PRINT "word not found"
STEP 11 - PRINT "word found"
STEP 12 END
Solution
import java.util.StringTokenizer;
import java.io.*;
Variable Description
NO.
1
2
3
4
5
NAME
br
s
st
look
int
TYPE
BufferedReader
String
StringTokenizer
String
int
Output
METHOD
main()
main()
main()
main()
main()
DESCRIPTION
BufferedReader Object
input String
StringTokenizer object
word to be searched
flag
PROGRAM 22:
Frequency of Each String Character
Algorithm
STEP 1 - START
STEP 2 - INPUT str
STEP 3 - l=str.length()
STEP 4 - PRINT str
STEP 5 - IF i=0 THEN GOTO STEP 4 OTHERWISE GOTO STEP 6
STEP 6 - char a=str.charAt(i)
STEP 7 - IF ii=0 THEN GOTO STEP 4 OTHERWISE GOTO STEP 8
STEP 8 - char b = str.charAt(ii)
STEP 9 - IF a==b GOTO STEP 10
STEP 10 - freq=freq+1
STEP 11 - ii++ & IF ii<1 GOTO STEP 8
STEP 12 - i++ & IF i<1 GOTO STEP 6
STEP 13 - DISPLAY a+" occurs "+freq+" times"
STEP 14 END
Solution
import java.io.*;
class Frequency
{
private int i,a1,l,p,j,freq;
public Frequency()
//default constructor
{
p=0;
freq=0;
// initialise instance variables
}
public void count(String str)
//counting character frequency
{
int ii;
l=str.length();
System.out.print(str);
for(i=0;i<l;i++)
{
char a=str.charAt(i);
for(ii=0;ii<l;ii++)
{
char b = str.charAt(ii);
if (a==b)
freq=freq+1;
}
System.out.println(a+" occurs "+freq+" times");
//displaying frequency
freq=0;
}}
public static void main(String args[]) throws IOException
//main function
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter string");
String str = br.readLine();
Frequency x = new Frequency();
x.count(str);
}}
Variable Description
NO.
1
2
3
4
5
6
7
8
9
10
11
NAME
br
i
a1
l
p
freq
ii
a
b
str
x
TYPE
BufferedReader
int
int
int
int
int
int
char
char
String
Frequency
METHOD
main()
count()
count()
count()
main()
main()
Output
PROGRAM 23:
Palindrome Check
DESCRIPTION
BufferedReader Object
loop variable
instance varirable
length of string
instance variable
frequency of characters
loop variable
character at index i
character at index ii
input String
frequency object
Algorithm
STEP 1 - START
STEP 2 - INPUT string s
STEP 3 - StringBuffer sb = s
STEP 4 - sb.reverse
STEP 5 - String rev = sb
STEP 6 - IF rev = s GOTO STEP 7 OTHERWISE GOTO STEP 8
STEP 7 - PRINT " Palindrome"
STEP 8 - PRINT " Not Palindrome"
STEP 9 END
Solution
import java.io.*;
class Palindrome
{
public static void main(String args[]) throws IOException
//main
function
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string=");
String s = br.readLine();
//accepting the string
StringBuffer sb = new StringBuffer(s);
sb.reverse();
//reversing the string
String rev = new String(sb);
if(s.equalsIgnoreCase(rev))
//checking for palindrome
System.out.println("Palindrome " );
//displaying the result
else
System.out.println("Not Palindrome " );
}}
Variable Description
NO.
1
2
NAME
br
s
TYPE
BufferedReader
String
METHOD
main()
main()
DESCRIPTION
BufferedReader Object
input string
3
4
sb
rev
StringBuffer
String
main()
main()
StringBuffer object of s
reverse string
Output
PROGRAM 24:
Star Pattern Using Input String
Solution
import java.io.*;
class Pattern
{
public static void main (String args[]) throws IOException
{
int i,sp,j,k,l;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string =");
//accepting string
String s = br.readLine();
l=s.length();
/*printing the pattern*/
for(i=0;i<l;i++)
if(i==l/2)
System.out.println(s);
else
{
sp=Math.abs((l/2)-i);
for(j=sp;j<l/2;j++)
System.out.print(" ");
k=0;
while(k<3)
{
System.out.print(s.charAt(i));
for(j=0;j<sp-1;j++)
System.out.print(" ");
k++;
}
System.out.println(" ");
}}}
Variable Description
NO.
1
2
3
NAME
br
s
i
TYPE
BufferedReader
String
int
METHOD
main()
main()
main()
4
5
sp
j
int
int
main()
main()
int
main()
int
main()
DESCRIPTION
BufferedReader Object
input string
loop variable for printing the
pattern
Math.abs((1/2)-i)
loop variable for printing
pattern
loop variable for printing
pattern
length of string
Output
PROGRAM 25:
Star Pattern Using Input String
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM x =0 to x<5 REPEAT STEP 4
STEP 4 - FROM y =0 to y<5 REPEAT STEP 5
STEP 5 - PRINT (a[x][y]+" "
Solution
import java.io.*;
class MatrixSum
{
public static void main(String args[])throws IOException
//main function
{
int a[][]=new int[5][5];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<5;x++)
//loop for reading array
{
for(y=0;y<5;y++)
{
z=Integer.parseInt(aa.readLine());
//accepting array element
a[x][y]=z;
}}
System.out.println("Array -");
for(x=0;x<5;x++)
//loop for printing array
{
for(y=0;y<5;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
for(x=0;x<5;x++)
//loop for printing sum of array
elements
{
for(y=0;y<5;y++)
{
Sum=Sum+a[x][y];
}}
System.out.println("Sum of Array elements="+Sum);
//displaying sum
}}
Variable Description
NO.
1
2
3
4
5
6
NAME
aa
a
x
y
z
Sum
TYPE
BufferedReader
int[][]
int
int
int
main()
METHOD
main()
main()
main()
main()
main()
main()
DESCRIPTION
BufferedReader Object
input array
loop variable
loop variable
input element
Sum of all elements
Output
PROGRAM 26:
Sum Of Matrix Column Elements
Algorithm
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM x =0 to x<4 REPEAT STEP 4
STEP 4 - FROM y =0 to y<4 REPEAT STEP 5
STEP 5 - PRINT (a[x][y]+" "
STEP 6 - FROM x =0 to x<4 REPEAT STEP 7 , STEP 9 and STEP 10
STEP 7 - FROM y =0 to y<4 REPEAT STEP 8
STEP 8 - Sum=Sum+a[x][y] ,
STEP 9 - PRINT Sum
STEP 10 - Sum = 0
STEP11 END
Solution
import java.io.*;
class ColoumnSum
{
public static void main(String args[])throws IOException
//main
function
{
int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
//reading array
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}}
System.out.println("Array -");
//printing the array in matrix
form
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
for(y=0;y<4;y++)
{
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
}
System.out.println("Sum of column "+(y+1)+" is "+Sum);
coloumn
Sum=0;
}}}
//printing sum of
Variable Description
NO.
1
2
3
4
5
6
NAME
aa
a
x
y
z
Sum
TYPE
BufferedReader
int[][]
int
int
int
int
Output
METHOD
main()
main()
main()
main()
main()
main()
DESCRIPTION
BufferedReader Object
input array
loop variable
loop variable
input element
Sum of all elements
PROGRAM 27:
Sum Of Matrix Diagonal Elements
Algorithm
STEP 1- START
STEP 2- INPUT a[]
STEP 3- FROM x =0 to x<4 REPEAT STEP 4
STEP 4- FROM y =0 to y<4 REPEAT STEP 5
STEP 5- PRINT (a[x][y]+" "
STEP 6- FROM x =0 to x<4 REPEAT STEP 7
STEP 7 - Sum=Sum+a[x][y] , y=y+1
STEP 8- PRINT Sum
STEP 9 - Sum = 0
STEP10- END
Solution
import java.io.*;
class DiagonalSum
{
public static void main(String args[])throws IOException
//main function
{
int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++)
//Reading array
{
for(y=0;y<4;y++)
{
z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}}
System.out.println("Array -");
for(x=0;x<4;x++)
//displaying array
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
y=0;
for(x=0;x<4;x++)
//loop for finding sum of diagonal
{
Sum=Sum+a[x][y]; y=y+1;
}
System.out.println("Sum of diagonal is "+Sum);
//displaying the sum of diagonal
Sum=0;
}}
Variable Description
NO.
1
2
NAME
aa
a
TYPE
BufferedReader
int[][]
METHOD
main()
main()
DESCRIPTION
BufferedReader Object
input matrix
3
4
5
6
x
y
Sum
z
int
int
int
int
main()
main()
main()
main()
Output
PROGRAM 28:
Sales Commission
Sales Commission
>=100000 25% of sales
80000-99999 22.5% of sales
60000-79999 20% of sales
40000-59999 15% of sales
<40000 12.5% of sales
Algorithm
STEP 1 - START
STEP 2 - INPUT sales
loop variable
loop variable
Sum of Diagonals
input elements
Solution
import java.io.*;
class SalesComission
{
public static void main(String args[])throws IOException
//main
function
{
double sales,comm;
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
System.out.println(Enter sales);
sales=Double.parseDouble(aa.readLine());
//reading sales from the
keyboard
/*calculating commission*/
if(sales>=100000)
comm=0.25*sales;
else if(sales>=80000)
comm=0.225*sales;
else if(sales>=60000)
comm=0.2*sales;
else if(sales>=40000)
comm=0.15*sales;
else comm=0.125*sales;
System.out.println("Commission of the employee="+comm);
//displaying
commission
}}
Variable Description
NO.
1
2
3
NAME
aa
sales
comm.
TYPE
BufferedReader
double
double
Output
METHOD
main()
main()
main()
DESCRIPTION
BufferedReader Object
sales
commission
PROGRAM 29:
Decimal To Roman Numerical
Algorithm
STEP 1 START
STEP 2 Enter number num
STEP 3 -- hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}
STEP 4 -- ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
STEP 5 -- unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
STEP 6 Display hund[num/100] and ten[(num/10)%10] and unit[num%10]
STEP 7 END
Solution
import java.io.*;
public class Dec2Roman
{
public static void main() throws IOException
//main
function
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter Number : ");
int num=Integer.parseInt(in.readLine());
//accepting decimal
number
String hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
String ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
/*Displaying equivalent roman number*/
System.out.println("Roman Equivalent= "+hund[num/100]+ten[(num/10)%10]+unit[num
%10]);
}}
Variable Description
NO.
1
2
3
4
5
NAME
in
num
hund
ten
unit
TYPE
DataInputStream
int
String[]
String[]
String[]
Output
METHOD
main()
main()
main()
main()
main()
DESCRIPTION
DataInputStream Object
input number
array storing 100th position
array storing 10th position
array storing units position
PROGRAM 30:
Celsius To Fahrenheit (Using Inheritance)
Algorithm
STEP 1 START
STEP 2 -- Input temperature celcius in celcius
STEP 3 far=1.8*celcius + 32
STEP 4 Display far
STEP 5 -- END
Solution
import java.io.*;
class C2F
{
public static void main(String args[])throws IOException
//main
function
{
Temperature ob= new Temperature();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter temperature in Celsius");
//accepting
temperature
double temp=ob.convert(Double.parseDouble(br.readLine()));
System.out.println("The temperature in fahrenheit is = "+temp);
}}
class Temperature extends C2F
{
double convert(double celcius)
//function to convert Celsius
to fahrenheit
{
double far=1.8*celcius+32.0;
return far;
}}
Variable Description
NO.
1
2
3
NAME
br
ob
temp
TYPE
BufferedReader
C2F
double
METHOD
main()
main()
main()
4
5
celcius
far
double
double
convert()
convert()
Output
DESCRIPTION
BufferedReader Object
C2F object
calculated Fahrenheit
temperature
input temperature in celsius
Calculated Fahrenheit
temperature