Sie sind auf Seite 1von 54

Qwertyuiopasdfghjklzxcvbnmqwe rtyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnm

qwertyuiopa sdfghjklzxcvbnmqwertyuiopasdfg hjklzxcvbnmqwertyuiopasdfghjklz xcvbn


mqwertyuiopasdfghjklzxcv COMPUTER bnmqwertyuiopasdfghjklzxcvbnm SCEIENCE qwertyu
iopasdfghjklzxcvbnmqwe rtyuiopasdfghjklzxcvbnmqwertyui PROJECT opasdfghjklzxcvbn
mqwertyuiopa sdfghjklzxcvbnmqwertyuiopasdfg hjklzxcvbnmqwertyuiopasdfghjklz xcvb
nmqwertyuiop asdfghjklzxcvbnmqwertyuiopasdf ghjklzxcv bnmqwertyuiopasdfghjklzxcv
bnm qwertyuiopasdfghjklzxcvbnmrtyui opasdfghjklzxcvbnmqwertyuiopa sdfghjklzxcvbn
mqwertyuiopasdfg
COMPUTER SCEIENCE 1 NAME : TABISH HAIDER RIZVI XII B CLASS : ROLL NO SCHOOL : 25
: LA MARTINIERE COLLEGE

ACKNOWLEDGEMENT
I would like to thank my Computer Science Teacher , Mr. J.V. Nagendra Rao, who g
uided me in making this project by giving some valuable points. I would also lik
e to thank my parents who helped me in making this project more presentable.

TABLE OF CONTENTS
S.No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 30 PROGRAM TO create Pascal s triangle To display entered number in words To dis
play A.P. series and its sum To display calendar of any month of any year To cal
culate factorial using recursion To display Fibonacci series using recursion To
calculate GCD using recursion To display spiral matrix To display magical square
To search an array using Linear Search To search an array using Binary Search T
o sort an array using Selection sort To sort an array using Bubble sort To conve
rt a decimal no into it binary equivalent To display date from entered day no. T
o create a pattern from entered string To check if entered string is palindrome
or not To display a frequency of each character in entered string To find a word
in entered string To decode the entered string To display the entered string in
alphabetical order. To create a string and count number of vowels and consonant
s. To create a string and count number of words To create a string and replace a
ll vowels with * To create a double-dimensional array of 4*4 subscripts. To gene
rate sum of all elements of a double dimensional array of 5*5 subscripts To gene
rate product of two arrays of 5 subscripts as a third array To find sum of each
column of a double dimensional array To find sum of diagonal of a double dimensi
onal array of 4*4 subscripts To calculate the commission of a salesman PAGE No.
4 5 6 8 10 11 12 13 15 17 20 22 24 27 28 30 31 32 34 36 38 40 41 42 43 44 46 47
49 51

PROGRAM 1 - TO create Pascal s 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 ST
EP 7 9 - pas[j]=pas[j]+pas[j-1] 10 - j--& IF j>0 GOTO STEP 9 11 - END
Solution:
class pascal { public void pascalw(int n) { int [ ] pas = new int [n+1]; pas[0]
= 1; for (int i=0; i<n; i++) { for (int j=0; j<=i; ++j) System.out.print(pas[j]+
" "); System.out.println( ); for (int j=i+1; j>0; j--) pas[j]=pas[j]+pas[j-1]; }
}

}
Output:
n=5 1 1 1 1 1 1 21 331 4641
PROGRAM 2 - To display entered number in words
Algorithm:
STEP STEP STEP STEP STEP STEP STEP 1234567START INPUT amt z=amt%10 , g=amt/10 IF
g!=1 THEN GOTO STEP 5 OTHERWISE GOTO STEP 6 PRINT x2[g-1]+" "+x1[z] PRINT x[amt
-9 END
SOLUTION:
import java.io.*; class eng { public static void main(String args[])throws IOExc
eption { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String x3; System.out.println("Enter any Number(less than 99)");

int amt=Integer.parseInt(br.readLine()); int a,b,c,y,z,g; String x[]={" ,"Ten","Ele


ven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen", "Seventeen","Eighteen",
"Nineteen"}; String x1[]={" ","One","Two","Three","Four","Five","Six","Seven","E
ight","Nine"}; String x2[]={" ","Twenty","Thirty","Fourty","Fifty","Sixty","Seve
nty","Eighty","Ninety"}; z=amt%10; g=amt/10; if(g!=1) System.out.println(x2[g-1]
+" "+x1[z]); else System.out.println(x[amt-9]); } }
Output:
Enter any Number(less than 99) 45 Fourty Five
PROGRAM 3 - To display A.P. series and its sum
Algorithm :
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10 11 S
TART a=d=0 IMPORT a, d this.a = a & this.d = d IMPORT n RETURN (a+(n-1)*d) IMPOR
T n - RETURN (n*(a+nTHTerm(n))/2) - IMPORT n - PRINT \n\tSeries\n\t" - IF i=1;i<
=n;i++ GOTO STEP 12

STEP STEP STEP STEP


12 13 14 15
PRINT nTHTerm(i)+" " i++ & IF i<=n GOTO STEP 12 PRINT n\tSum : "+Sum(n) END
SOLUTION:
class APSeries { private double a,d; APSeries() { a = d = 0;
,double d) { this.a = a; this.d = d; } double nTHTerm(int n)
); } double Sum(int n) { return (n*(a+nTHTerm(n))/2); } void
System.out.print("\n\tSeries\n\t"); for(int i=1;i<=n;i++) {
HTerm(i)+" "); } System.out.print("\n\tSum : "+Sum(n)); } }
Output:

} APSeries(double a
{ return (a+(n-1)*d
showSeries(int n) {
System.out.print(nT

a=5 d=2 n=10 Series 5.0 7.0 9.0 11.0 13.0 15.0 17.0 19.0 21.0 23.0 Sum : 140.0
PROGRAM 4 - To display calendar of any month of any year
Algorithm :
STEP 1 - START STEP 2 - INPUT int month,int year STEP 3 - int i,count=0,b,c,d=1
& String w="SMTWTFS" STEP 4 - IF (year%100==0 && year%400==0) || (year%100!=0 &&
year %4==0) STEP 5 - days[1]=29 STEP 6 - PRINT "================The Calendar of
"+month1[month-1]+" "+year+" is==================") STEP 7 - IF i=0 THEN GOTO S
TEP 8 STEP 8 - PRINT (i)+"\t" & " " STEP 9 - IF i=1 GOTO STEP 10 STEP 10 - IF (y
ear%100==0 && year%400==0) || (year%100!=0 && year %4==0)THEN GOTO STEP 11OTHERW
ISE GOTO STEP 12 STEP 11 - count+=2 STEP 12 - count+=1 STEP 13 - IF i=0 GOTO STE
P 14 STEP 14 - count+=days[i] , count+=1, count%=7 & b=7-count STEP 15 - IF b!=1
|| b!=7 GOTO STEP 16 STEP 16 - IF count>0 GOTO STEP 17,18 STEP 17 - PRINT ' '+"
\t") STEP 18 - count-STEP 19 - IF i=1 GOTO STEP 20 STEP 20 - IF b>0 && IF d<=day
s[month-1] GOTO STEP 21,22 STEP 21 - PRINT d+"\t" STEP 22 - d++ & b-STEP 23 - b=
7 STEP 24 - i++ & IF i<MONTH GOTO STEP14 STEP 25 - PRINT " " STEP 26 - END

SOLUTION:
class calendar { public void dee(int month,int year) { int i,count=0,b,c,d=1; St
ring w="SMTWTFS"; int days[]={31,28,31,30,31,30,31,31,30,31,30,31}; String month
1[]={"January","February","March","April","May","June","July", "August","Septemb
er","October","November","December"} ; If((year%100==0 && year%400==0) || (year%
100!=0 && year%4==0)) days[1]=29; System.out.println("================The Calend
ar of "+month1[month-1]+" "+year+" is=================="); 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=7-coun
t; 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(" "); } } }


Output:
month = 10 year = 2009 =====The Calendar of October 2009 is==== S M T W T F S 1
2 3 4 5 6 7 8 9 1 0 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
31
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)) STEP11 - END
SOLUTION:
import java.io.*; class factorial { public static void main(String args[]) throw
s IOException { 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("factotial ="+f); } public long fact(int n) { if(n<2) return
1; else return (n*fact(n-1)); } }
Output:
enter no = 5 factotial =120
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)) STEP11 - END
SOLUTION:
import java.io.*; class fibonacci

{ public static void main(String args[]) throws IOException { fibonacci obj = ne


w fibonacci(); BufferedReader br = new BufferedReader(new InputStreamReader(Syst
em.in)); System.out.println("enter no of term ="); int n = Integer.parseInt(br.r
eadLine()); System.out.println(); for(int i=1;i<=n;i++) { int f = obj.fib(i); Sy
stem.out.print(f+" "); } } public int fib(int n) { if(n<=1) return n; else retur
n (fib(n-1) +fib(n-2)); } }
Output:
enter no of term = 5 1 1 2 3 5
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) STEP11 - END


SOLUTION:
import java.io.*; class gcd { public static void main(String args[]) throws IOEx
ception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in
)); System.out.println("enter the numbers ="); int p = Integer.parseInt(br.readL
ine()); int q = Integer.parseInt(br.readLine()); gcd obj = new gcd(); int g = ob
j.calc(p,q); System.out.println("GCD ="+g); } public int calc(int p,int q) { if(
q==0) return p; else return calc(q,p%q); } }
Output :
enter the numbers = 12 8 GCD =4

PROGRAM 8 - To display spiral matrix.


Algorithm:
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 START INPUT a[][] IF p!=(
int)Math.pow(l,2) GOTO STEP 4 IF co!=0 GOTO STEP 5 re=1 IF ri=1;ri<=k1-re;ri++ G
OTO STEP 7 p++,c++ IF c==l GOTO STEP 9 BREAK a[r][c]=p IF c==l GOTO STEP 12 BREA
K IF dw=1 GOTO STEP 14 p++,r++,a[r][c]=p IF le=1 GOTO STEP 16 p++,c--,a[r][c]=p
IF up=1 GOTO STEP 18 p++,r--,a[r][c]=p k1=k1+2, k2=k2+2 & co++ up++ & IF up<=k21 GOTO STEP 18 le++ & IF le<=k2-1 GOTO STEP 16 dw++ & IF dw<=k1-1 GOTO STEP 14 I
F y=0 GOTO STEP 24 IF yy=0 GOTO STEP 25 PRINT "\t"+a[y][yy]) & () yy++ & IF yy<l
GOTO STEP 25 y++ & IF y<l GOTO STEP 24 END
SOLUTION:
import java.io.*; class spiralsm { public static void main(String[] args) throws
IOException { 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 ="); int l = Integer.parseInt(br.rea
dLine()); a=new int[l][l]; r=l/2;c=r-1; if(l%2==0) { System.out.println("wrong e
ntry for spiral path"); System.exit(0); } 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;l
e++) {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.p
rint("\t"+a[y][yy]); System.out.println(); System.out.println(); } } }
Output:
enter the dimension of matrix =

5 21 20 19 18 17 22 7 6 5 16 23 8 1 4 15 24 9 2 3 14 25 10 11 12 13
PROGRAM 9 - To display magical square
Algorithm:
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP
STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 20 21 21 22 START arr[][]=new int[n][n],c=n/2-1,r=1,num IF num=1;num<=n*n;num
++ GOTO STEP 4 r--,c++ IF r==-1 GOTO STEP 6 r=n-1 IF c>n-1 GOTO STEP 8 c=0 IF ar
r[r][c]!=0 GOTO STEP 10 r=r+2 & c-num++ & IF num<=n*n GOTO STEP 4 arr[r][c]=num
IF r==0&&c==0 GOTO STEP 14 r=n-1, c=1 & arr[r][c]=++num IF c==n-1&&r==0 GOTO STE
P 16 arr[++r][c]=++num PRINT () IFr=0 GOTO STEP 19 IF c=0 GOT STEP 20 PRINT arr[
r][c]+" " & () c++ & IF c<n GOTO STEP 20 r++ & r<n GOTO STEP 19 END
SOLUTION:

import java.io.*; class magicalsquare { public static void main(String args[])th


rows Exception { BufferedReader br = new BufferedReader(new InputStreamReader(Sy
stem.in)); System.out.println("enter the dimension of magical square="); int n =
Integer.parseInt(br.readLine()); int arr[][]=new int[n][n],c=n/2-1,r=1,num; for
(num=1;num<=n*n;num++) { 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++)
{ for(c=0;c<n;c++) System.out.print(arr[r][c]+" "); System.out.println(); } } }

Output :
enter the dimension of magical square= 5 17 23 4 10 11 24 1 8 15 5 7 14 16 6 13
20 22 12 19 21 3 18 25 2 9
PROGRAM 10 - To search an array using Linear Search
Algorithm:
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10 11 S
TART INPUT a[] FROM i=0 to i<n REPEAT STEP 4 PRINT a[i]+" " flag=-1 FROM i=0 to
i<n REPEAT STEP 7 IF (a[i] == v) THEN flag =i IF (flag=-1) THEN GOTO STEP 9 OTHE
RWISE GOTO STEP 10 PRINT
not found
PRINT v+" found at position - "+flag END
SOLUTION:
import java.io.*; class linear_search { int n,i; int a[] = new int[100]; static
BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); public
linear_search(int nn) {

n=nn; } public void input() throws IOException { System.out.println("enter eleme


nts"); for(i=0;i<n;i++) { a[i] = Integer.parseInt(br.readLine()); } } public voi
d display() { System.out.println(); for(i=0;i<n;i++) { System.out.print(a[i]+" "
); } } public void search(int v) { 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.o
ut.println(v+" found at position - "+flag); } public static void main(String arg
s[]) throws IOException { linear_search obj = new linear_search(10); obj.input()
; obj.display(); System.out.println("enter no. to be searched -"); int v = Integ
er.parseInt(br.readLine()); obj.search(v);

} }
Output :
enter elements 5 3 8 4 1 6 4 7 9 5 5384164795 enter no. to be searched 1 1 found
at position - 4
PROGRAM 11 - To search an array using Binary Search
Algorithm:
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8
9 10 11 12 13 START INPUT a[] FROM i=0 to i<n REPEAT STEP 4 PRINT a[i]+" " flag
=-1 , l=0, u=n-1 IF(l<=u && flag=-1) REPEAT STEP 7 AND Step 8 m = (l+u)/2 IF (a[
m] == v) THEN flag =m OTHERWISE GOTO STEP 9 IF (a[m] < v) THEN l = m+1 OTHERWISE
u =m-1 - IF (flag=-1) THEN GOTO STEP 11 OTHERWISE GOTO STEP 12 - PRINT
not found
RINT v+" found at position - "+flag - END

- P

STEP 11 - END SOLUTION: import java.io.*; class binary_search { int n,i; int a[]
= new int[100]; static BufferedReader br =new BufferedReader(new InputStreamRea
der(System.in)); public binary_search(int nn) { n=nn; } public void input() thro
ws IOException { 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 search(int v)
{ 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("no


t found"); else System.out.println(v+" found at position - "+flag); } public sta
tic void main(String args[]) throws IOException { binary_search obj = new binary
_search(10); obj.input(); obj.display(); System.out.println("enter no. to be sea
rched -"); int v = Integer.parseInt(br.readLine()); obj.search(v); } }
Output :
enter elements 5 3 8 4 1 6 4 7 9 5 5384164795 enter no. to be searched -

1 1 found at position - 4
PROGRAM 12 - To sort an array using Selection sort
Algorithm:
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10 11 START - INPUT a[] - FROM i=0 to i<n REPEAT STEP 4 - PRINT a[i]+" " - flag=-1 FROM i=0 to i<n-1 REPEAT STEP 7 to STEP 11 - min =i - FROM j=i+1 to j<n REPEAT S
TEP 8 - IF(a[j]<a[min]) then min =j - IF (min!=i) GOTO STEP 11 - temp = a[i], a[
i] =a[min], a[min] = temp
SOLUTION:
import java.io.*; class selection_sort { int n,i; int a[] = new int[100]; public
selection_sort(int nn) { n=nn; } public void input() throws IOException { Buffe
redReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.p
rintln("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.pr


int(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 I
OException { selection_sort x = new selection_sort(5); x.input(); System.out.pri
nt("Before sorting - "); x.display(); System.out.print("After sorting - "); x.so
rt(); x.display(); }

}
Output:
enter elements 4 6 1 2 9 Before sorting 46129 After sorting 12469
PROGRAM 13 - To sort an array using Bubble Sort
Algorithm:
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10 - START INPUT a[] - FROM i=0 to i<n REPEAT STEP 4 - PRINT a[i]+" " - flag=-1 - FROM i=0
to i<n-1 REPEAT STEP 7 to STEP 9 - FROM j=i+1 to j<n REPEAT STEP 8 - IF(a[j] >
a[j+1]) THEN GOTO STEP 9 - temp = a[i], a[i] =a[min], a[min] = temp - END
SOLUTION:
import java.io.*; class bubble_sort { int n,i; int a[] = new int[100]; public bu
bble_sort(int nn) { 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; for(i=0 ; i<n-1 ; i++) { for(j=0 ; j<n-1-i ; j++) { i
f(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 {

bubble_sort x = new bubble_sort(5); x.input(); System.out.print("Before sorting


- "); x.display(); System.out.print("After sorting - "); x.sort(); x.display();
} }
Output:
enter elements 4 6 1 2 9 Before sorting 46129 After sorting 12469
PROGRAM 14 - To convert a decimal no into it binary equivalent
SOLUTION:
STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 - START - n = 30
- INPUT int no - c =0 , temp = no - IF (temp!=0) REPEAT STEP 6 - a[c++] = temp%2
, temp = temp / 2 - FROM i=c-1 to i>0 REPEAT STEP 8 - PRINT a[i] - END
Program :

import java.io.*; class dec_bin { int n,i; int a[] = new int[100]; static Buffer
edReader br =new BufferedReader(new InputStreamReader(System.in)); public dec_bi
n(int nn) { n=nn; } public void dectobin(int no) { int c = 0; int temp = no; whi
le(temp != 0) { a[c++] = temp % 2; temp = temp / 2; } System.out.println("Binary
eq. of "+no+" = "); for( i = c-1 ; i>=0 ; i--) System.out.print( a[ i ] ); } pu
blic static void main(String args[]) throws IOException { dec_bin obj = new dec_
bin(30); System.out.println("enter decimal no -"); int no = Integer.parseInt(br.
readLine()); obj.dectobin(no); } }
Output :
enter decimal no 56

Binary eq. of 56 = 111000


PROGRAM 15 - To display date from entered day no.
SOLUTION:
STEP 1 STEP 2 STEP 3 STEP 4 STEP 5 STEP 6 STEP 7 STEP 8 STEP 9 , "+yr STEP 10 ST
EP 11 - START - INITIALISE a[ ] , m[ ] - INPUT n , yr - IF ( yr%4=0) THEN a[1] =
29 - t =0 , s = 0 - IF ( t<n) REPEAT STEP 7 - t =t + a[s++] - d = n + a[--s] t - IF ( d ==1|| d == 21 || d == 31 ) then PRINT d + "st" + m[s] + "
- IF ( d ==2|| d == 22 ) then PRINT d + "nd" + m[s] + " , "+yr - IF ( d ==3|| d
== 23 ) then PRINT d + "rd" + m[s] + " , "+yr OTHERWISE GOTO STEP 12 STEP 12 - P
RINT d + "th" + m[s] + " , "+yr STEP 13 - END
import java.io.*; class daytodate { static BufferedReader br =new BufferedReader
(new InputStreamReader(System.in)); public void calc(int n, int yr) { 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) { 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 { Sys
tem.out.println( d + "th" + m[s] + " , "+yr); } } public static void main(String
args[]) throws IOException { daytodate obj = new daytodate(); System.out.printl
n( "Enter day no = "); int n = Integer.parseInt(br.readLine()); System.out.print
ln( "Enter year = "); int yr = Integer.parseInt(br.readLine()); obj.calc(n,yr);
} }
Output:
Enter day no =

192 Enter year = 2009 11th Jul , 2009


PROGRAM 16
To create a pattern from entered string
Program : 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 ="); Str
ing s = br.readLine(); l=s.length(); for(i=0;i<l;i++) if(i==l/2) System.out.prin
tln(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(" "); } } }

Output:
enter the string = india iii nnn india iii aaa
PROGRAM 17 - To check if entered string is palindrome or not
Algorithm :
STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 START INPUT strin
g s StringBuffer sb = s sb.reverse String rev = sb IF rev = s GOTO STEP 7 OTHERW
ISE GOTO STEP 8 PRINT " Palindrome" PRINT " Not Palindrome" END
Solution:
import java.io.*; class palindrome {
ws IOException { BufferedReader br =
stem.in)); System.out.println("enter
ringBuffer sb = new StringBuffer(s);

public static void main(String args[]) thro


new BufferedReader(new InputStreamReader(Sy
the string="); String s = br.readLine(); St
sb.reverse(); String rev = new String(sb);

if(s.equals(rev)) System.out.println("Palindrome " ); else System.out.println("N


ot Palindrome " ); } }
Output :
enter the string= arora Palindrome
PROGRAM 18 - To display a frequency of each character in entered string
Algorithm :
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5
6 7 8 9 10 11 12 13 14 START INPUT str l=str.length() PRINT str IF i=0 THEN GOTO
STEP 4 OTHERWISE GOTO STEP 22 char a=str.charAt(i) IF ii=0 THEN GOTO STEP 4 OTH
ERWISE GOTO STEP 22 char b = str.charAt(ii) IF a==b GOTO STEP 10 freq=freq+1 ii+
+ & IF ii<1 GOTO STEP 8 i++ & IF i<1 GOTO STEP 6 DISPLAY a+" occurs "+freq+" tim
es" END

Solution :
import java.io.*; class frequency { private int i,a1,l,p,j,freq; public frequenc
y() { p=0; freq=0;// initialise instance variables } public void count(String st
r) { int ii; l=str.length(); System.out.print(str); for(i=0;i<l;i++) { char a=st
r.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"); freq=0; } } public static v
oid main(String args[]) throws IOException { BufferedReader br =new BufferedRead
er(new InputStreamReader(System.in)); System.out.println("enter string"); String
str = br.readLine(); frequency x = new frequency(); x.count(str); } }

Output:
enter string school s occurs 1 times c occurs 1 times h occurs 1 times o occurs
2 times o occurs 2 times l occurs 1 times
PROGRAM 19 - To find a word in entered string
Algorithm :
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10
11 12 START INPUT string s StringTokenizer st = s l =str.length() INPUT look fl
ag = -1 IF (st.hasMoreElements()) REPEAT STEP 8 IF (look.equals(st.nextElement()
)) THEN flag =1 IF flag = - 1 GOTO STEP 10 OTHERWISE STEP 11 PRINT "word not fou
nd" PRINT "word found" END
Solution:
import java.util.StringTokenizer; import java.io.*; public class word_search {

public static void main(String[] args) throws IOException { BufferedReader br =


new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter
the string="); String s = br.readLine(); StringTokenizer st = new StringTokenize
r(s," "); System.out.println("enter the word to be searched ="); String look = b
r.readLine(); int flag = -1; while(st.hasMoreElements()) { if(look.equals(st.nex
tElement())) flag =1; } if(flag ==-1) { System.out.println("the word not found")
; } else { System.out.println("the word found"); } } }
Output:
enter the string= there are 7 days in a week enter the word to be searched = are
the word found
PROGRAM 20 - To decode the entered string

Algorithm :
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP
STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 S
TART INPUT name, n l=name.length() PRINT original string is "+name IF i=0 THEN G
OTO STEP 6 char c1=name.charAt(i) c=(int)c1 IF n>0 THEN GOTO STEP 9 THERWISE GOT
O STEP 12 IF (c+n)<=90 THEN GOTO STEP 10 OTHERWISE GOTO STEP 11 PRINT (char)(c+n
) c=c+n;c=c%10,c=65+(c-1) & PRINT (char)(c) ELSE IF n<0 THEN GOTO STEP 13 OTHERW
ISE GOTO STEP 19 n1=Math.abs(n) IF (c-n1) >=65 THEN GOTO STEP 15 OTHERWISE GOTO
STEP 16 DISPLAY (char) (c-n1) IF c>65 THEN GOTO STEP 17 OTHERWISE GOTO STEP 18 c
=c-65, c=n1 & PRINT (char)(90-(c-1)) ELSE IF n==0 DISPLAY "no change "+name END
Solution :
class decode { public void compute(String name,int n) { int j,i,l,c=0,y,n1; l=na
me.length(); System.out.println("original string is "+name); for(i=0;i<l;i++) {
char c1=name.charAt(i); try { c=(int)c1 ; }

catch(NumberFormatException e) {} if(n>0) { if((c+n)<=90) 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; } } } }
Output:
original string is ABCDE n=4 decoded string is EFGHI

PROGRAM 21 - To display the entered string in alphabetical order.


Algorithm :
STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 9 10
11 12 START str = "" , l = 0 INPUT string str l =str.length() FROM i=0 to i<l R
EPEAT STEP 6 c[i] = str.charAt(i) FROM i=0 to i<l-1 REPEAT STEP 8 FROM j=0 to i<
l-1 REPEAT STEP 9 temp =c[j], c[j] = c[j+1] , c[j+1] = temp FROM i=0 to i<l REPE
AT STEP 11 PRINT c[i] END
Solution:
import java.io.*; class Alpha { String str; int l; char c[] = new char[100]; pub
lic Alpha() { str = ""; l =0; } public void readword() throws IOException { Syst
em.out.println("enter word - "); BufferedReader br =new BufferedReader(new Input
StreamReader(System.in)); str = br.readLine();

l = str.length(); } public void arrange() { int i,j; char temp; for(i=0;i<l;i++)


{ c[i]= str.charAt(i); } for(i=0;i<l-1;i++) { 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
() { System.out.println(); for(int i=0;i<l;i++) { System.out.print(c[i]); } } pu
blic static void main(String args[]) throws IOException { Alpha obj = new Alpha(
); obj.readword(); obj.arrange(); obj.display(); } }

Output:
enter word window dinoww
PROGRAM 22 - To create a string and count 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:
class p42 { public static void main(String args[]) { String a="Computer Applicat
ions";//initialising string int z=a.length(),y,x=0,b=0; for(y=0;y<z;y++)//loop f
or 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); System.out.println("Number


of consonants in string ="+b); } }
Output:
Number of vowels in string =7 Number of consonants in string =12
PROGRAM 23 - To create a string and count number of words.
Algorithm :
STEP STEP STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 7 8 START a = "Computer Appl
ications" z = a.length() x= 0 FROM y =0 to y<z REPEAT STEP 6 IF (a.charAt(y)=='
' ) then x =x+1 PRINT "Number of words in string ="+(x+1) END
Solution:
class p45 { public static void main(String args[]) { String a="Computer Applicat
ions"; //initialising string System.out.println("The string is -"+a); int z=a.le
ngth(),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)); } }

Output:
The string is -Computer Applications Number of words in string =2
PROGRAM 24 - To create a string and replace all 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 p48 { public static void main(String args[]) { StringBuf
fer a=new StringBuffer("Computer Applications"); System.out.println("Original St
ring -"+a); int z=0; for(z=0;z<a.length();z++)//loop for replacing vowels with "
*" { if(a.charAt(z)=='a'||a.charAt(z)=='e'||a.charAt(z)=='i'||a.charAt(z)=='o'||
a.charAt(z)=='u') a.setCharAt(z,'*'); } System.out.println("New String -"+a); }
}
Output:
Original String -Computer Applications

New String -C*mp*t*r Appl*c*t**ns


PROGRAM 25 - To create a double-dimensional array of 4*4 subscripts.
Algorithm :
STEP STEP STEP STEP STEP STEP 123456START INPUT a[] FROM x =0 to x<3 REPEAT STEP
4 FROM y =0 to y<3 REPEAT STEP 5 PRINT (a[x][y]+" " END
Solution:
class p31 { public static void main(String args[])throws IOException { int a[][]
=new int[3][3], x,y,z; BufferedReader aa=new BufferedReader(new InputStreamReade
r(System.in)); System.out.println("Enter the array"); for(x=0;x<3;x++)//loop for
reading the array { for(y=0;y<3;y++) { z=Integer.parseInt(aa.readLine()); a[x][
y]=z; } } System.out.println("Array -"); for(x=0;x<3;x++)//loop for printing the
array { for(y=0;y<3;y++) { System.out.print(a[x][y]+" "); } System.out.print("\
n"); } } }

Output:
Enter the array 1 2 3 4 5 6 7 8 9 Array 123 456 789
PROGRAM 26 - To generate sum of all elements of a double dimensional array of 5*
5 subscripts
Algorithm :
STEP 1 STEP 2 STEP 3 STEP 4 STEP 5 STEP 6 STEP 7 STEP 8 STEP 9 STEP10 START INPU
T a[] FROM x =0 to x<5 REPEAT FROM y =0 to y<5 REPEAT PRINT (a[x][y]+" " FROM x
=0 to x<5 REPEAT FROM y =0 to y<5 REPEAT Sum=Sum+a[x][y] PRINT Sum - END
STEP 4 STEP 5 STEP 7 STEP 8
Solution:
class p33 { public static void main(String args[])throws IOException { int a[][]
=new int[5][5]; BufferedReader aa=new BufferedReader(new InputStreamReader(Syste
m.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()); 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]+" "); } S
ystem.out.print("\n"); } for(x=0;x<5;x++)//loop for printing sum of array elemen
ts { for(y=0;y<5;y++) { Sum=Sum+a[x][y]; } } System.out.println("Sum of Array el
ements="+Sum); } }
Output:
Enter the array 1 2 3 4 5 6 7 8 8 9 0 9 8 7 6 5

4 3 2 1 2 3 4 5 6 Array 12345 67889 09876 54321 23456 Sum of Array elements=118


PROGRAM 27 - To generate product of two arrays of 5 subscripts as a third array.
Algorithm :
STEP STEP STEP STEP STEP STEP 1 2 3 4 5 6 START INPUT a[] , b[] FROM i =0 to i<5
REPEAT STEP 4 and STEP 5 c[i] = a[i] * b[i] PRINT c[i] END
Solution:
class p35 { public static void y(int a[],int b[]) { int c[]=new int[5]; int i; S
ystem.out.println("Product of two arrays is-"); for(i=0;i<5;i++)//loop for findi
ng product of the two arrays { c[i]=a[i]*b[i]; System.out.print(+c[i]+" "); } }

}
Output:
Product of two arrays is4 6 21 32 25
PROGRAM 28 - To find sum of each column of a double dimensional array.
Algorithm :
STEP 1 - START STEP 2 - INPUT a[] STEP 3 - FROM x =0 to x<4 REPEAT STEP 4 - FROM
y =0 to y<4 REPEAT STEP 5 - PRINT (a[x][y]+" " STEP 6 - FROM x =0 to x<4 REPEAT
STEP 7 - FROM y =0 to y<4 REPEAT STEP 8 - Sum=Sum+a[x][y] , STEP 9 - PRINT Sum
STEP 10 - Sum = 0 STEP11 - END
STEP 4 STEP 5 STEP 7 , STEP 9 and STEP 10 STEP 8
Solution:
class p39 { public static void main(String args[])throws IOException { int a[][]
=new int[4][4]; BufferedReader aa=new BufferedReader(new InputStreamReader(Syste
m.in)); int x,y,z,Sum=0; System.out.println("Enter the array");//reading array f
or(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.ou


t.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);//printing sum of Sum=0; //colum
n } } }
Output:
Enter the array 1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 Array 1234 5678 9098 7654 Sum of
column 1 is 22 Sum of column 2 is 14

Sum of column 3 is 24 Sum of column 4 is 24


PROGRAM 29 - To find sum of diagonal of a double dimensional array of 4*4 subscr
ipts.
Algorithm :
STEP 1- START STEP 2- INPUT a[] STEP 3- FROM x =0 to x<4 REPEAT STEP 4 STEP 4- F
ROM 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 9- PRINT Sum STEP 10 - Sum
= 0 STEP11- END
Solution:
class p40 { public static void main(String args[])throws IOException { int a[][]
=new int[4][4]; BufferedReader aa=new BufferedReader(new InputStreamReader(Syste
m.in)); int x,y,z,Sum=0; System.out.println("Enter the array"); for(x=0;x<4;x++)
{ for(y=0;y<4;y++) { z=Integer.parseInt(aa.readLine()); a[x][y]=z; } } System.o
ut.println("Array -"); for(x=0;x<4;x++) { 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.prin
tln("Sum of diagonal is " +Sum); Sum=0; } }
Output:
Enter the array 1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 4 Array 1234 5678 9098 7654 Sum of
diagonal is 20
PROGRAM 30 - To calculate the commission of a salesman as per the following data
:

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 STEP 3 - IF (sales>=100000) THEN comm=0.25 *
sales GOTO STEP 4 STEP 4 - IF (sales>=80000) THEN comm=0.225*sales GOTO STEP 5 S
TEP 5 - IF (sales>=60000) THEN comm=0.2 *sales GOTO STEP 6 STEP 6 - IF (sales>=4
0000) THEN comm=0.15 *sales GOTO STEP 7 STEP 7 - comm=0.125*sales STEP 8 - PRINT
"Commission of the employee="+comm STEP 9 - END
OTHERWISE OTHERWISE OTHERWISE OTHERWISE
Solution:
class p14 { public static void main(String args[])throws IOException { double sa
les,comm; BufferedReader aa=new BufferedReader(new InputStreamReader(System.in))
; System.out.println( Enter sales ); sales=Double.parseDouble(aa.readLine());//reading s
ales from the keyboard 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; els


e comm=0.125*sales; System.out.println("Commission of the employee="+comm); } }
Output:
Enter sales 60000 Commission of employee=12000

Das könnte Ihnen auch gefallen