Computer Project
Computer Project
SCIENCE
PROJECT
For ISC
Name: Amaan Rizwan
Pr ogr
Class: amming
12-C
in BLUEJ
Tirthanu Ghosh
12 A
Roll no. 11
Comput er
Science
pr oj ect
Tirthanu Ghosh
12 A
Roll No. 11
contents
No. Program Page No.
1 Pascal’s Triangle 1
2 Number in Words 3
3 AP Series 5
4 Calendar of Any Month 8
5 Factorial (Using Recursion) 11
6 Fibonacci Series (Using Recursion) 13
7 GCD (Using Recursion) 15
8 Spiral Matrix 17
9 Magic Square 20
10 Linear Search 23
11 Binary Search 26
12 Selection Sort 29
13 Bubble Sort 32
14 Decimal to Binary Number 35
15 Date Program 37
16 Star Pattern Using Input String 40
17 Palindrome Check 42
18 Frequency of Each String Character 44
19 Word Search in String 47
20 Decoding of String 49
21 String in Alphabetical Order 52
22 Number of Vowels and Consonants 55
23 Word Count 57
24 Replacing Vowels with * 59
25 Sum of All Matrix Elements 61
26 Sum of Matrix Column Elements 63
27 Sum of Matrix Diagonal Elements 65
28 Sales Commission 67
29 Decimal to Roman Numerical 69
30 Celsius to Fahrenheit (Using Inheritance) 71
ACKNOWLEDGEMENTS
First of all, I’d like to thank my parents for helping me out
with the project.
sol ut ion
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];
}}}
out pu
ut
sol ut ion
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","Nineteen"};
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]);
}}
out pu
ut
out pu
ut
out pu
ut
10 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 5
To Calculate 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
sol ut ion
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()); //accepting no.
Factorial obj = new Factorial();
long f = obj.fact(n);
System.out.println("Factorial ="+f); //displaying factorial
}
public long fact(int n) //recursive fact()
{if(n<2)
return 1;
else return (n*fact(n-1));
}}
11 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader object
2 n int main() input number
3 obj Factoriaal main() Factorial object
4 f long main() variable storing factorial
5 n int fact() parameter in recursive fun nction fact()
out pu
ut
12 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 6
To Display 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
sol ut ion
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;
else
return (fib(n-1) +fib(n-2));
}}
13 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader object
2 obj Fibonaccci main() Fibonacci object
3 n int main() input number
4 i int main() loop variable for Fibonacci element
5 f int main() Fibonacci element
6 n int fib() recursive function fib() parrameter
out pu
ut
14 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 7
To Calculate 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
sol ut ion
import java.io.*;
class GCD
{public static void main(String args[]) throws IOException //main function
{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the numbers =");
int p = Integer.parseInt(br.readLine()); //accepting nos.
int q = Integer.parseInt(br.readLine());
GCD obj = new GCD();
int g = obj.calc(p,q);
System.out.println("GCD ="+g);
}
public int calc(int p,int q) //recursive function calculating GCD
{if(q==0)
return p;
else return calc(q,p%q);
}}
15 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader objeect
2 p int main() ,calc() input number
3 q int main() ,calc() input number
4 obj GCD main() GCD object
5 g int main() variable storing the GCD
G
out pu
ut
16 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 8
To Display Spiral Matrix.
ALGORITHM
STEP 1 - START
STEP 2 - INPUT a[][]
STEP 3 - IF p!=(int)Math.pow(l,2) GOTO STEP 4
STEP 4 - IF co!=0 GOTO STEP 5
STEP 5 - re=1
STEP 6 - IF ri=1;ri<=k1-re;ri++ GOTO STEP 7
STEP 7 - p++,c++
STEP 8 - IF c==l GOTO STEP 9
STEP 9 - BREAK
STEP 10 - a[r][c]=p
STEP 11 - IF c==l GOTO STEP 12
STEP 12 - BREAK
STEP 13 - IF dw=1 GOTO STEP 14
STEP 14 - p++,r++,a[r][c]=p
STEP 15 - IF le=1 GOTO STEP 16
STEP 16 - p++,c--,a[r][c]=p
STEP 17 - IF up=1 GOTO STEP 18
STEP 18 - p++,r--,a[r][c]=p
STEP 19 - k1=k1+2, k2=k2+2 & co++
STEP 20 - up++ & IF up<=k2-1 GOTO STEP 18
STEP 21 - le++ & IF le<=k2-1 GOTO STEP 16
STEP 22 - dw++ & IF dw<=k1-1 GOTO STEP 14
STEP 23 - IF y=0 GOTO STEP 24
STEP 24 - IF yy=0 GOTO STEP 25
STEP 25 - PRINT "\t"+a[y][yy]) & ()
STEP 26 - yy++ & IF yy<l GOTO STEP 25
STEP 27 - y++ & IF y<l GOTO STEP 24
STEP 28 – END
17 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
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++) //Displaying matrix
{for(int yy=0;yy<l;yy++)
System.out.print("\t"+a[y][yy]);
System.out.println();
System.out.println();
}}}
18 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader object
2 a int[][] main() spiral matrix
3 r int main() l/2
4 c int main() r-1
5 k1 int main() stores 2
6 k2 int main() stores 3
7 p int main() loop gate
8 co int main() coloumn index
9 re int main() row index
10 l int main() dimensions of thr matrix
11 ri int main() right side matrix loop variaable
12 le int main() left side matrix loop variab
ble
13 dw int main() down side matrix loop variiable
14 up int main() up side matrix loop variablle
15 y int main() loop variable to print matrrix
16 yy int main() loop variable to print matrrix
out pu
ut
19 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 9
To Display 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 ()
STEP 18 - IFr=0 GOTO STEP 19
STEP 19 - IF c=0 GOT STEP 20
STEP 20 - PRINT arr[r][c]+" " & ()
STEP 21 - c++ & IF c<n GOTO STEP 20
STEP 21 - r++ & r<n GOTO STEP 19
STEP 22 – END
20 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
/*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 Exception //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();
for(r=0;r<n;r++) //loop displaying magic square
{for(c=0;c<n;c++)
System.out.print(arr[r][c]+" ");
System.out.println();
}}}
21 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader object
2 n int main() input dimensions
3 arr int[][] main() magic square matrix
4 num int main() loop variable for magic squ
uare
5 r int main() row
6 c int main() coloumn
out pu
ut
22 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 10
To Search an Array Using
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
23 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
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 the values to be searched
int v = Integer.parseInt(br.readLine());
obj.search(v);
}}
24 | I S C C o m p u t e r S c i e n c e P r o j e c t
abl e descrr ipt ion
var ia
No. Name Type Method Description
1 br BuffereedReader - BufferedReader object
o
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int LinearSearch() parameter in consstructor
6 v int search(), main() search element
7 flag int search() flag
8 obj LinearSSearch main() LinearSearch objeect
out pu
ut
25 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 11
To Search an Array Using
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
26 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
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 //function accepting array elements
{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 //main function
{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);
27 | I S C C o m p u t e r S c i e n c e P r o j e c t
}}
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader - BufferedReader object
o
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int BinarySearch() parameter in con
nstructor
6 v int search(), main() search element
7 flag int search() flag
8 l int search() lower limit
9 u int search() upper limit
10 m int search() middle index
11 obj BinarySSearch main() BinarySearch object
out pu
ut
28 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 12
To Sort an Srray Using
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
29 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
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 //function accepting array elements
{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() //function sorting array elements using selection sort technique
{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 //main function
{SelectionSort x = new SelectionSort(5);
x.input();
System.out.print("Before sorting - ");
x.display();
System.out.print("After sorting - ");
x.sort();
x.display();
}}
30 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader input() BufferedReader objeect
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int SelectionSort() parameter in constrructor
6 j int sort() sort index
7 temp int sort() temporary storage
8 min int sort() minimum value
9 x Selectio
onSort main() SelectioSort object
out pu
ut
31 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 13
To Sort an Array Using
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
32 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
import java.io.*;
class BubbleSort
{int n,i;
int a[] = new int[100];
public BubbleSort(int nn) //parameterized constructor
{n=nn;
}
public void input() throws IOException //function accepting array elements
{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() //function sorting array elements using Bubble Sort technique
{int j,temp;
for(i=0 ; i<n-1 ; i++)
{for(j=0 ; j<n-1-i ; j++)
{if(a[j] > a[j+1])
{temp = a[j];
a[j] =a[j+1];
a[j+1] = temp;
}}}}
public static void main(String args[]) throws IOException //main function
{BubbleSort x = new BubbleSort(5);
x.input();
System.out.print("Before sorting - ");
x.display();
System.out.print("After sorting - ");
x.sort();
x.display();}}
33 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader input BufferedReader object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int BubbleSort() parameter in construcctor
6 j int sort() sort index
7 temp int sort() temporary storage
8 x Selectio
onSort main() SelectionSort object
out pu
ut
34 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 14
To Convert a Decimal no. Into
its Binary Equivalent
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
sol ut ion
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 ] );
35 | I S C C o m p u t e r S c i e n c e P r o j e c t
}
public static void maiin(String args[]) throws IOException //m
main function
{Dec2Bin obj = new Dec2Bin(30);
D
System.out.println("eenter decimal no -");
int no = Integer.parseeInt(br.readLine());
obj.dectobin(no);
}}
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader BufferedReaderr object
2 n int - array length
3 i int - loop variable
4 a[] int[] - array storing bin
nary no.
5 nn int Dec2Bin() parameter in coonstructor
6 no int main(), dectobin() input number
7 temp int dectobin() temporary storaage
8 c int dectobin() counter
9 obj Dec2Bin main() Dec2Bin object
out pu
ut
36 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 15
To Display Date From
Entered Day Number
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
STEP 12 - PRINT d + "th" + m[s] + " , "+yr
STEP 13 – END
37 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
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);
}}
38 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader - BufferedReader objeect
2 n int calc(), main() Day number
3 yr int calc(), main() year
4 a int[] calc() array storing day
5 m int[] calc() array storing month
6 t int calc() array index
7 s int calc() array index
8 d int calc() n+a[--s]+t
9 obj Day2Daate main() Day2Date object
out pu
ut
39 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 16
To Create a Star Pattern
From Entered String
sol ut ion
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(" ");
}}}
40 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader object
2 s String main() input string
3 i int main() loop variable for printing the pattern
4 sp int main() Math.abs((l/2)-i)
5 j int main() loop variable for printing the pattern
6 k int main() loop variable for printing the pattern
7 l int main() length of string
out pu
ut
41 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 17
To Check if Entered String is
Palindrome or Not
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
sol ut ion
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 " );
}}
42 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader object
2 s String main() input string
3 sb StringB
Buffer main() StringBuffer object of s
4 rev String main() revese string
out pu
ut
43 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 18
To Display a Frequency of
Each Character in Entered
String
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 22
STEP 6 - char a=str.charAt(i)
STEP 7 - IF ii=0 THEN GOTO STEP 4 OTHERWISE GOTO STEP 22
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
44 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
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 frquency
{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);
}}
45 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader object
2 i int - loop variable
3 a1 int - instance variable
4 l int - length of string
5 p int - instance variable
6 freq int - frequency of characters
7 ii int count() loop variable
8 a char count() character at index i
9 b char count() character at index ii
10 str String main() input string
11 x Frequency main() Frequency object
out pu
ut
46 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 19
To Find a Word in Entered
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
sol ut ion
import java.util.StringTokenizer;
import java.io.*;
public class WordSearch
{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 string
StringTokenizer st = new StringTokenizer(s," "); //StringTokenizer initialization
System.out.println("enter the word to be searched =");
String look = br.readLine();
int flag = -1;
while(st.hasMoreElements()) //searching for word
{if(look.equals(st.nextElement()))
flag =1;
47 | I S C C o m p u t e r S c i e n c e P r o j e c t
}
if(flag ==-1)
{System.out.println(""the word not found"); //displayying the result
}
else {
System.out.println("tthe word found");
}}}
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader main() BufferedReader object
2 s String main() input string
3 st StringTokenizer main() StringTokenizer object
4 look String main() word to be searched
5 flag int main() flag
out pu
ut
48 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 20
To Decode the Entered 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
STEP 15 - DISPLAY (char) (c-n1)
STEP 16 - IF c>65 THEN GOTO STEP 17 OTHERWISE GOTO STEP 18
STEP 17 - c=c-65,
STEP 18 - c=n1 & PRINT (char)(90-(c-1))
STEP 19 - ELSE IF n==0
STEP 20 - DISPLAY "no change "+name
STEP 21 - END
49 | I S C C o m p u t e r S c i e n c e P r o j e c t
var ia
abl e descrr ipt ion
No. Name Type Method Description
1 br BuffereedReader compute() BufferedReadeer object
2 name String compute() input string
3 n int compute() decode numbeer
4 j int compute() loop variable
5 i int compute() loop variable
6 l int compute() length of stringg
7 c int compute() ASCII of c1
8 y int compute()
9 n1 int compute()
10 c1 char compute() character at ind
dex i
11 e NumbeerFormatException compute() NumberFOrmaatException object
out pu
ut
51 | I S C C o m p u t e r S c i e n c e P r o j e c t
sol ut ion
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 the 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();
}}
53 | I S C C o m p u t e r S c i e n c e P r o j e c t