Qwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqw ertyuiopasdfghjklzxcvbnmqwe rtyuiopasdfghjklzxcvbnmqwer tyuiopasdfghjklzxcvbnmqwert yuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyu iopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuio pasdfghjklzxcvbnmqwertyuiop asdfghjklzxcvbnmqwertyuiopa sdfghjklzxcvbnmqwertyuiopas dfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiop asdfghjklzxcvbnmqwertyuiopa sdfghjklzxcv 1 1 COMPUTER SCEIENCE COMPUTER SCEIENCE PROJECT NAME : TABISH HAIDER RIZVI CLASS : XII B ROLL NO : 25 SCHOOL : LA MARTINIERE COLLEGE
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
I would like to thank my Computer Science Teacher , Mr. J.V. Nagendra Rao, who guided me in making this project by giving some valuable points.
I would also like to thank my parents who helped me inmaking this project more presentable.
3
TABLE OF CONTENTS
S.No. PROGRAM PAGE No.1 TO create Pascal’s triangle 42 To display entered number in words 53 To display A.P. series and its sum 64 To display calendar of any month of any year 85 To calculate factorial using recursion 106 To display Fibonacci series using recursion 117 To calculate GCD using recursion 128 To display spiral matrix 139 To display magical square 15
10 To search an array using Linear Search 1711 To search an array using Binary Search 2012 To sort an array using Selection sort 2213 To sort an array using Bubble sort 2414 To convert a decimal no into it binary equivalent 2715 To display date from entered day no. 2816 To create a pattern from entered string 3017 To check if entered string is palindrome or not 3118 To display a frequency of each character in entered string 3219 To find a word in entered string 3420 To decode the entered string 3621 To display the entered string in alphabetical order. 3822 To create a string and count number of vowels and consonants. 4023 To create a string and count number of words 4124 To create a string and replace all vowels with * 4225 To create a double-dimensional array of 4*4 subscripts. 43
26To generate sum of all elements of a double dimensional array of 5*5 subscripts
44
27 To generate product of two arrays of 5 subscripts as a third array 4628 To find sum of each column of a double dimensional array 47
29To find sum of diagonal of a double dimensional array of 4*4 subscripts
49
30 To calculate the commission of a salesman 51
4
PROGRAM 1 - TO create Pascal’s triangle
Algorithm:
STEP 1 - STARTSTEP 2 - pas[0] = 1STEP 3 - IF i=0 THEN GOTO STEP 4STEP 4 - IF j=0 THEN GOTO STEP 5STEP 5 - PRINT pas[j]+" "STEP 6 - i++& IF i<n GOTO STEP 4STEP 7 - j=0 & IF j<=i GOTO STEP 5STEP 8 - IF j=i+1 THEN GOTO STEP 7STEP 9 - pas[j]=pas[j]+pas[j-1]STEP 10 - j--& IF j>0 GOTO STEP 9STEP 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)
class calendar{ public void dee(int month,int year) { int i,count=0,b,c,d=1; String w="SMTWTFS"; int days[]={31,28,31,30,31,30,31,31,30,31,30,31}; String month1[]={"January","February","March","April","May","June","July",
class factorial{ public static void main(String args[]) throws 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)); }}
11
Output:enter no =5factotial =120
PROGRAM 6 - To display Fibonacci series using recursion
Algorithm :
STEP 1 - STARTSTEP 2 - INPUT nSTEP 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 = new fibonacci(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter no of term ="); int n = Integer.parseInt(br.readLine()); System.out.println(); for(int i=1;i<=n;i++) { int f = obj.fib(i); System.out.print(f+" "); } } public int fib(int n) {
STEP 1 - STARTSTEP 2 - INPUT p,qSTEP 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 IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the numbers =");
13
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); } public int calc(int p,int q) { if(q==0) return p; else return calc(q,p%q); }}
STEP 6 - FROM i=0 to i<n REPEAT STEP 7STEP 7 - IF (a[i] == v) THEN flag =iSTEP 8 - IF (flag=-1) THEN GOTO STEP 9 OTHERWISE GOTO STEP 10STEP 9 - PRINT “ not found”STEP 10 - PRINT v+" found at position - "+flag STEP 11 - 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 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 flag=-1; for(int i=0; i<n ; i++)
19
{ 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 { linear_search obj = new linear_search(10); obj.input(); obj.display(); System.out.println("enter no. to be searched -"); int v = Integer.parseInt(br.readLine()); obj.search(v); }}
Output :enter elements5384164795
5 3 8 4 1 6 4 7 9 5 enter no. to be searched -11 found at position - 4
20
PROGRAM 11 - T o search an array using Binary Search
Algorithm:
STEP 1 - STARTSTEP 2 - INPUT a[]STEP 3 - FROM i=0 to i<n REPEAT STEP 4STEP 4 - PRINT a[i]+" "STEP 5 - flag=-1 , l=0, u=n-1STEP 6 - IF(l<=u && flag=-1) REPEAT STEP 7 AND Step 8STEP 7 - m = (l+u)/2STEP 8 - IF (a[m] == v) THEN flag =m OTHERWISE GOTO STEP 9STEP 9 - IF (a[m] < v) THEN l = m+1 OTHERWISE u =m-1STEP 10 - IF (flag=-1) THEN GOTO STEP 11 OTHERWISE GOTO STEP 12STEP 11 - PRINT “ not found”STEP 12 - PRINT v+" found at position - "+flag STEP 13 - END
STEP 11 - END SOLUTION:import java.io.*;
class binary_search{ int n,i; int a[] = new int[100]; static BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); public binary_search(int nn) { 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() { System.out.println();
21
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("not found"); else System.out.println(v+" found at position - "+flag); } public static void main(String args[]) throws IOException { binary_search obj = new binary_search(10); obj.input(); obj.display(); System.out.println("enter no. to be searched -"); int v = Integer.parseInt(br.readLine()); obj.search(v); }
22
}
Output :enter elements5384164795
5 3 8 4 1 6 4 7 9 5 enter no. to be searched -11 found at position - 4
PROGRAM 12 - T o sort an array using Selection sort
Algorithm:
STEP 1 - STARTSTEP 2 - INPUT a[]STEP 3 - FROM i=0 to i<n REPEAT STEP 4STEP 4 - PRINT a[i]+" "STEP 5 - flag=-1STEP 6 - FROM i=0 to i<n-1 REPEAT STEP 7 to STEP 11STEP 7 - min =iSTEP 8 - FROM j=i+1 to j<n REPEAT STEP 8STEP 9 - IF(a[j]<a[min]) then min =j STEP 10 - IF (min!=i) GOTO STEP 11STEP 11 - temp = a[i], a[i] =a[min], a[min] = temp
SOLUTION:import java.io.*;
23
class selection_sort{ int n,i; int a[] = new int[100]; public selection_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,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) {
PROGRAM 14 - To convert a decimal no into it binary equivalent
27
SOLUTION:
STEP 1 - STARTSTEP 2 - n = 30 STEP 3 - INPUT int noSTEP 4 - c =0 , temp = noSTEP 5 - IF (temp!=0) REPEAT STEP 6STEP 6 - a[c++] = temp%2, temp = temp / 2STEP 7 - FROM i=c-1 to i>0 REPEAT STEP 8STEP 8 - PRINT a[i]STEP 9 - END
Program :
import java.io.*;
class dec_bin{ int n,i; int a[] = new int[100]; static BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); public dec_bin(int nn) { n=nn; } public void dectobin(int no) { 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--) System.out.print( a[ i ] ); }
28
public 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 -56Binary eq. of 56 = 111000
PROGRAM 15 - To display date from entered day no.
SOLUTION:
STEP 1 - STARTSTEP 2 - INITIALISE a[ ] , m[ ]STEP 3 - INPUT n , yrSTEP 4 - IF ( yr%4=0) THEN a[1] = 29STEP 5 - t =0 , s = 0STEP 6 - IF ( t<n) REPEAT STEP 7STEP 7 - t =t + a[s++]STEP 8 - d = n + a[--s] - tSTEP 9 - IF ( d ==1|| d == 21 || d == 31 ) then PRINT d + "st" + m[s] + " , "+yrSTEP 10 - IF ( d ==2|| d == 22 ) then PRINT d + "nd" + m[s] + " , "+yrSTEP 11 - IF ( d ==3|| d == 23 ) then PRINT d + "rd" + m[s] + " , "+yr
OTHERWISE GOTO STEP 12STEP 12 - PRINT d + "th" + m[s] + " , "+yrSTEP 13 - END
import java.io.*;
class daytodate{
29
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 { System.out.println( d + "th" + m[s] + " , "+yr); } } public static void main(String args[]) throws IOException { daytodate obj = new daytodate(); System.out.println( "Enter day no = "); int n = Integer.parseInt(br.readLine());
30
System.out.println( "Enter year = "); int yr = Integer.parseInt(br.readLine()); obj.calc(n,yr); }}
Output:Enter day no = 192Enter year = 200911th Jul , 2009
PROGRAM 16 – To create a pattern from e ntered 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 ="); String s = br.readLine(); l=s.length(); 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++)
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 StringTokenizer(s," "); System.out.println("enter the word to be searched ="); String look = br.readLine(); int flag = -1; while(st.hasMoreElements()) { if(look.equals(st.nextElement())) 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
36
PROGRAM 20 - To decode the entered string
Algorithm :
STEP 1 - STARTSTEP 2 - INPUT name, nSTEP 3 - l=name.length()STEP 4 - PRINT original string is "+nameSTEP 5 - IF i=0 THEN GOTO STEP 6STEP 6 - char c1=name.charAt(i)STEP 7 - c=(int)c1 STEP 8 - IF n>0 THEN GOTO STEP 9 THERWISE GOTO STEP 12STEP 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 16STEP 15 - DISPLAY (char) (c-n1)STEP 16 - IF c>65 THEN GOTO STEP 17 OTHERWISE GOTO STEP 18STEP 17 - c=c-65,STEP 18 - c=n1 & PRINT (char)(90-(c-1))STEP 19 - ELSE IF n==0STEP 20 - DISPLAY "no change "+nameSTEP 21 - END
Solution :class decode { public void compute(String name,int n) { 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);
PROGRAM 22 - To create a string and count number of vowels and consonants .
Algorithm :
STEP 1 - STARTSTEP 2 - a = "Computer Applications"STEP 3 - z = a.length() STEP 4 - x= 0 , b= 0STEP 5 - FROM y =0 to y<z REPEAT STEP 6STEP 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+1STEP 7 - PRINT xSTEP 8 - PRINT bSTEP 9 - END
Solution:
class p42{public static void main(String args[]){String a="Computer Applications";//initialising stringint 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++;elseb++;
41
}System.out.println("Number of vowels in string ="+x);System.out.println("Number of consonants in string ="+b);}}
Output:Number of vowels in string =7Number of consonants in string =12
PROGRAM 23 - To create a string and count number of words .
Algorithm :
STEP 1 - STARTSTEP 2 - a = "Computer Applications"STEP 3 - z = a.length() STEP 4 - x= 0 STEP 5 - FROM y =0 to y<z REPEAT STEP 6STEP 6 - IF (a.charAt(y)==' ' ) then x =x+1STEP 7 - PRINT "Number of words in string ="+(x+1)STEP 8 - END
Solution:
class p45{public static void main(String args[]){String a="Computer Applications"; //initialising stringSystem.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));}}
42
Output:The string is -Computer ApplicationsNumber of words in string =2
PROGRAM 24 - To create a string and replace all vowels with * .
Algorithm :
STEP 1 - STARTSTEP 2 - a = "Computer Applications"STEP 3 - x= 0 STEP 4 - FROM z =0 to z<a.length() REPEAT STEP 5STEP 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 -"+aSTEP 7 - END
Solution:
import java.io.*;class p48{public static void main(String args[]){StringBuffer a=new StringBuffer("Computer Applications");System.out.println("Original String -"+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);}}
PROGRAM 25 - To create a double-dimensional array of 4*4 subscripts.
Algorithm :
STEP 1- STARTSTEP 2- INPUT a[]STEP 3- FROM x =0 to x<3 REPEAT STEP 4STEP 4- FROM y =0 to y<3 REPEAT STEP 5STEP 5- PRINT (a[x][y]+" "STEP 6- 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 InputStreamReader(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
44
123456789Array -1 2 34 5 67 8 9
PROGRAM 26 - To generate sum of all elements of a double dimensional array of 5*5 subscripts
Algorithm :
STEP 1 - STARTSTEP 2 - INPUT a[]STEP 3 - FROM x =0 to x<5 REPEAT STEP 4STEP 4 - FROM y =0 to y<5 REPEAT STEP 5STEP 5 - PRINT (a[x][y]+" "STEP 6 - FROM x =0 to x<5 REPEAT STEP 7STEP 7 - FROM y =0 to y<5 REPEAT STEP 8STEP 8 - Sum=Sum+a[x][y]STEP 9 - PRINT SumSTEP10 - END
Solution:
class p33{public static void main(String args[])throws IOException{ 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++)
45
{ 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]+" ");}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);}}
PROGRAM 27 - To generate product of two arrays of 5 subscripts as a third array.
Algorithm :
STEP 1 - STARTSTEP 2 - INPUT a[] , b[]STEP 3 - FROM i =0 to i<5 REPEAT STEP 4 and STEP 5STEP 4 - c[i] = a[i] * b[i]STEP 5 - PRINT c[i]STEP 6 - END
Solution:
class p35{public static void y(int a[],int b[]){ int c[]=new int[5];int i;System.out.println("Product of two arrays is-");for(i=0;i<5;i++)//loop for finding product of the two arrays{ c[i]=a[i]*b[i];System.out.print(+c[i]+" ");}}}
47
Output:Product of two arrays is-4 6 21 32 25
PROGRAM 28 - To find sum of each column of a double dimensional array.
Algorithm :
STEP 1 - STARTSTEP 2 - INPUT a[]STEP 3 - FROM x =0 to x<4 REPEAT STEP 4STEP 4 - FROM y =0 to y<4 REPEAT STEP 5STEP 5 - PRINT (a[x][y]+" "STEP 6 - FROM x =0 to x<4 REPEAT STEP 7 , STEP 9 and STEP 10STEP 7 - FROM y =0 to y<4 REPEAT STEP 8 STEP 8 - Sum=Sum+a[x][y] , STEP 9 - PRINT SumSTEP 10 - Sum = 0STEP11 - END
Solution:class p39{public static void main(String args[])throws IOException{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 arrayfor(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 formfor(x=0;x<4;x++){for(y=0;y<4;y++)
48
{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);//printing sum ofSum=0; //column}}}
Output:Enter the array1234567890987654Array -1 2 3 45 6 7 89 0 9 87 6 5 4Sum of column 1 is 22Sum of column 2 is 14Sum of column 3 is 24Sum of column 4 is 24
49
PROGRAM 29 - To find sum of diagonal of a double dimensional array of 4*4 subscripts.
Algorithm :
STEP 1- STARTSTEP 2- INPUT a[]STEP 3- FROM x =0 to x<4 REPEAT STEP 4STEP 4- FROM y =0 to y<4 REPEAT STEP 5STEP 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 SumSTEP 10 - Sum = 0STEP11- END
Solution:
class p40{public static void main(String args[])throws IOException{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++){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++){for(y=0;y<4;y++){System.out.print(a[x][y]+" ");}System.out.print("\n");
50
}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);Sum=0;}}
Output:Enter the array1234567890987654Array -1 2 3 45 6 7 89 0 9 87 6 5 4Sum of diagonal is 20
PROGRAM 30 - To calculate the commission of a salesman as per the following data:
Sales Commission>=100000 25% of sales80000-99999 22.5% of sales
51
60000-79999 20% of sales40000-59999 15% of sales<40000 12.5% of sales
Algorithm :
STEP 1 - STARTSTEP 2 - INPUT salesSTEP 3 - IF (sales>=100000) THEN comm=0.25 *sales OTHERWISE GOTO
STEP 4STEP 4 - IF (sales>=80000) THEN comm=0.225*sales OTHERWISE GOTO
STEP 5STEP 5 - IF (sales>=60000) THEN comm=0.2 *sales OTHERWISE GOTO
STEP 6STEP 6 - IF (sales>=40000) THEN comm=0.15 *sales OTHERWISE GOTO
STEP 7STEP 7 - comm=0.125*salesSTEP 8 - PRINT "Commission of the employee="+commSTEP 9 - END
Solution:
class p14{public static void main(String args[])throws IOException{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 keyboardif(sales>=100000)comm=0.25*sales;elseif(sales>=80000)comm=0.225*sales;elseif(sales>=60000)comm=0.2*sales;elseif(sales>=40000)comm=0.15*sales;elsecomm=0.125*sales;
52
System.out.println("Commission of the employee="+comm);}}
Output:Enter sales60000Commission of employee=12000