Ics e Programs Answersi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

Please note may be other correct solutions to these programs. I have tried to put up the simplest and direct logics in the solutions, so that they remain easy to understand. Thanks //Leap Year 1 import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Input year"); int y=Integer.parseInt(br.readLine()); if(y%100==0)//check for century year { if(y%400==0) { System.out.println("Leap"); } else { System.out.println("Not Leap"); } } else { if(y%4==0) { System.out.println("Leap"); } else { System.out.println("Not Leap"); } } }//main }//class //Leap Year 2 import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Input year"); int y=Integer.parseInt(br.readLine()); if(y%400==0 || (y%100!=0 && y%4==0)) { System.out.println("Leap"); } else { System.out.println("Not Leap"); } }//main KP/JAVA/ICSE/programs.doc Page 1/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

}//class //Marks - Grade import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Input marks"); int m=Integer.parseInt(br.readLine()); if(m<=39) { System.out.println("Grade = D"); } else if(m>=40 && m<=59)//m<=59 alone also correct { System.out.println("Grade = C"); } else if(m>=60 && m<=74)//m<=74 alone also correct { System.out.println("Grade = B"); } else if(m>=75)//will work without condition also { System.out.println("Grade = A"); } }//main }//class

//Net Pay import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); double basic, hra, da, pf, tds, net; System.out.println("Input basic pay"); basic=Double.parseDouble(br.readLine()); if(basic<=10000) { hra=1000; da=basic*.35; pf=200; tds=basic*.1; } else if(basic<=25000)//>10000 optional coz of else { hra=1200; da=basic*.35; pf=400; KP/JAVA/ICSE/programs.doc Page 2/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

tds=(basic-10000)*.12+(10000*.1); } else { hra=1500; da=basic*.35; pf=800; tds=(basic-25000)*.15+(15000*.12)+(10000*.1); } net=basic+hra+da-pf-tds; System.out.println("Basic = "+basic); System.out.println("hra = "+hra); System.out.println("da = "+da); System.out.println("pf = "+pf); System.out.println("tds = "+tds); System.out.println("Net pay = "+net); }//main }//class

//Lib Fine import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); int days, fine; System.out.println("Input days"); days=Integer.parseInt(br.readLine()); if(days<=3) { fine=2*days; } else if(days<=7) { fine=3*(days-3)+(3*2); } else { fine=5*(days-7)+(3*2)+(4*3); } System.out.println("fine = "+fine); }//main }//class

//Menu - perimeter, circumference import java.io.*; public class ICSE KP/JAVA/ICSE/programs.doc Page 3/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

{ public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); int choice; do//display the menu again and again { System.out.println("1. Perimeter of a rectangle"); System.out.println("2. Circumference of a circle"); System.out.println("3. Exit program"); System.out.println("Enter your choice"); choice=Integer.parseInt(br.readLine()); switch(choice) { case 1: int l,b,p; System.out.println("Enter length"); l=Integer.parseInt(br.readLine()); System.out.println("Enter breadth"); b=Integer.parseInt(br.readLine()); p=2*(l+b); System.out.println("Perimeter = "+p); break; case 2: double c,r; System.out.println("Input radius"); r=Double.parseDouble(br.readLine()); c=2*3.14*r; System.out.println("Circumference = "+c); break; case 3: break; default: System.out.println("Invalid choice"); break; }//switch }while(choice!=3); }//main }//class

//Calculator import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a number "); int n1=Integer.parseInt(br.readLine()); System.out.println("Enter another number "); int n2=Integer.parseInt(br.readLine()); System.out.println("Enter an operator "); KP/JAVA/ICSE/programs.doc Page 4/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

char op=(char)br.read(); int ans; switch(op) { case '+': ans=n1+n2; System.out.println("Sum = "+ans); break; case '-': ans=Math.abs(n1-n2); System.out.println("Absolute difference = "+ans); break; case '*': ans=n1*n2; System.out.println("Product = "+ans); break; case '/': int g=Math.max(n1,n2); int s=Math.min(n1,n2); if(s!=0) { double q=(double)g/s; System.out.println("Dividing greater by the smaller"); System.out.println("Quotient = "+q); } else { System.out.println("Division by 0 not allowed"); } break; default: System.out.println("Wrong operator"); break; }//switch }//main }//class

//Digit in words import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a digit "); int d=Integer.parseInt(br.readLine()); switch(d) { case 0: System.out.println("Zero"); break; case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; KP/JAVA/ICSE/programs.doc Page 5/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

case 3: System.out.println("Three"); break; case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); break; case 6: System.out.println("Six"); break; case 7: System.out.println("Seven"); break; case 8: System.out.println("Eight"); break; case 9: System.out.println("Nine"); break; }//switch }//main }//class

//Making functions import java.io.*; public class ICSE { double si(double p, double r, double t) { double s=p*r*t/100; return s; }//fun() boolean isPerfect(int n) { int s=0; for(int i=1; i<n; i++) { if(n%i==0) { s+=i; } } if(n==s) { return true; } else { return false; } }//fun() //sample calls public static void main(String args[])throws IOException KP/JAVA/ICSE/programs.doc Page 6/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

{ InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); ICSE obj=new ICSE(); //calling function 1 System.out.println("Enter principle : "); double p=Double.parseDouble(br.readLine()); System.out.println("Enter rate : "); double r=Double.parseDouble(br.readLine()); System.out.println("Enter time : "); double t=Double.parseDouble(br.readLine()); double s=obj.si(p,r,t); System.out.println("Simple interest = "+s); //calling function 2 System.out.println("Enter a number "); int n=Integer.parseInt(br.readLine()); boolean perfect=obj.isPerfect(n); if(perfect==true) { System.out.println("Perfect number"); } else { System.out.println("Not a Perfect number"); } }//main }//class

//Overloading import java.io.*; public class ICSE { int area(int side) { int a=side*side; return a; } int area(int len, int brd) { int a=len*brd; return a; } //Sampel call public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); ICSE obj=new ICSE(); System.out.println("Enter a side "); KP/JAVA/ICSE/programs.doc Page 7/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

int s=Integer.parseInt(br.readLine()); int a1=obj.area(s); System.out.println("Area of the square = "+a1); System.out.println("Enter a length "); int l=Integer.parseInt(br.readLine()); System.out.println("Enter a breadth "); int b=Integer.parseInt(br.readLine()); int a2=l*b; System.out.println("Area of the rectangle = "+a2); }//main }//class

//Series public class ICSE { public static void main(String args[]) { System.out.println("Series 1"); int a=0; int b=1; System.out.print(a+" "); System.out.print(b+" "); for(int i=1; i<=8 ;i++ ) { int c=a+b; System.out.print(c+" "); a=b; b=c; } System.out.println(); System.out.println("Series 2"); for(int i=1; i<=25; i++) { if(i%2==0) { System.out.print(-i+" "); } else { System.out.print(i+" "); } } System.out.println(); System.out.println("Series 3"); for(int i=1; i<=10; i++) { int n=(i*i)-1; System.out.print(n+" "); } System.out.println(); System.out.println("Series 4"); KP/JAVA/ICSE/programs.doc Page 8/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

int add=1; for(int i=1; i<=12345; i=i*10+add) { System.out.print(i+" "); add++; } }//main }//class

//Prime import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a number "); int n=Integer.parseInt(br.readLine()); int count=0; for(int i=1;i<=n; i++) { if(n%i==0) { count++; } } if(count==2) { System.out.println("Prime"); } else { System.out.println("Not Prime"); } }//main }//class

//Factorial import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a number "); int n=Integer.parseInt(br.readLine()); int f=1; for(int i=1;i<=n; i++) { f=f*i; } KP/JAVA/ICSE/programs.doc Page 9/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

System.out.println("Factorial = "+f); }//main }//class

//Armstrong import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a number "); int num=Integer.parseInt(br.readLine()); int n=num; int sum=0; while(n>0) { int digit=n%10; sum=sum+(int)Math.pow(digit,3); n=n/10; } if(sum==num) { System.out.println("Armstrong"); } else { System.out.println("Not Armstring"); } }//main }//class

//HCF LCM import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a number "); int n1=Integer.parseInt(br.readLine()); System.out.println("Enter another number "); int n2=Integer.parseInt(br.readLine()); //HCF for(int i=n1; i>=1; i--) { if(n1%i==0 && n2%i==0) { System.out.println("Hcf = "+i); break; KP/JAVA/ICSE/programs.doc Page 10/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

} } //LCM for(int i=n1; i<=n1*n2; i++) { if(i%n1==0 && i%n2==0) { System.out.println("Lcm = "+i); break; } } }//main }//class

//Series import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); double X, M, m, L; L=50; System.out.println("Enter M "); M=Double.parseDouble(br.readLine()); for(m=.5; m<=5; m=m+.5) { X=(M*L)/(2*(M+m)); System.out.println("X = "+X); } }//main }//class

//Population import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); double p,g; System.out.println("Enter initial population "); p=Double.parseDouble(br.readLine()); System.out.println("Enter growth rate "); g=Double.parseDouble(br.readLine()); for(int i=1; i<=10; i++) { p=p+p*(g/100); System.out.print("Year = "+i); KP/JAVA/ICSE/programs.doc Page 11/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

System.out.println(" Population = "+p); g=g+g*.1;//increase growth rate by 10% } }//main }//class

//Series sum import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter n "); int n=Integer.parseInt(br.readLine()); double sum=0; for(int i=1; i<=n; i++) { int s=0; for(int j=1; j<=i; j++) { s=s+j; } int f=1; for(int j=1; j<=i; j++) { f=f*j; } if(i%2==0) { sum=sum-Math.sqrt(s)/f; } else { sum=sum+Math.sqrt(s)/f; } }//for System.out.println("Sum = "+sum); }//main }//class

//Prime numbers between 1-100 public class ICSE { public static void main(String args[]) { for(int i=1; i<=100; i++) { int count=0; for(int j=1; j<=i; j++) { if(i%j==0) { count++; KP/JAVA/ICSE/programs.doc Page 12/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

} } if(count==2) { System.out.println(i); } } }//main }//class

//Multiplication tables 13-19 public class ICSE { public static void main(String args[]) { for(int i=13; i<=19; i++) { for(int j=1; j<=10; j++) { int p=i*j; System.out.println(i+" x "+j+" = "+p); } System.out.println(); } }//main }//class

//Figures public class ICSE { public static void main(String args[]) { System.out.println("Figure 1"); for(int i=1; i<=5; i++) { for(int j=1; j<=i; j++) { System.out.print(j); }//j System.out.println(); }//i System.out.println("Figure 1"); for(int i=1; i<=5; i++) { for(int j=1; j<=i; j++) { System.out.print(j); }//j System.out.println(); }//i System.out.println("Figure 2"); for(int i=1; i<=5; i++) { for(int j=1; j<=i; j++) { System.out.print(i); KP/JAVA/ICSE/programs.doc Page 13/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

}//j System.out.println(); }//i System.out.println("Figure 3"); for(int i=1; i<=5; i++) { for(int j=1; j<=i; j++) { System.out.print("*"); }//j System.out.println(); }//i System.out.println("Figure 4"); for(int i=1; i<=5; i++) { for(int j=1; j<=i; j++) { System.out.print((char)(64+j)); }//j System.out.println(); }//i System.out.println("Figure 5"); for(int i=1; i<=5; i++) { for(int j=1; j<=i; j++) { System.out.print(j%2); }//j System.out.println(); }//i System.out.println("Figure 6"); int b=5; for(int i=1; i<=5; i++) { for(int j=1; j<=b; j++) { System.out.print(" "); }//j for(int j=1; j<=i; j++) { System.out.print(j); }//j System.out.println(); b--; }//i System.out.println("Figure 7"); b=5; for(int i=1; i<=9; i+=2) { for(int j=1; j<=b; j++) { System.out.print(" "); }//j for(int j=1; j<=i; j++) { System.out.print(j); }//j KP/JAVA/ICSE/programs.doc Page 14/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

System.out.println(); b--; }//i System.out.println("Figure 8"); int n=1; for(int i=1; i<=4; i++) { for(int j=1; j<=i; j++) { System.out.print(n); n++; }//j System.out.println(); }//i }//main }//class

//array - sum and avg of even nos import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); int a[]=new int[5]; //input System.out.println("Enter numbers "); for(int i=0; i<a.length; i++) { a[i]=Integer.parseInt(br.readLine()); } //processing double sum=0; int count=0; for(int i=0; i<a.length; i++) { if(a[i]%2==0) { sum+=a[i]; count++; } } double avg=sum/count; System.out.println("Sum = "+sum); System.out.println("Average = "+avg); }//main }//class

//array - highest marks' name KP/JAVA/ICSE/programs.doc Page 15/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); String n[]=new String[5]; int m[]=new int[5]; //input System.out.println("Enter names and marks"); for(int i=0; i<n.length; i++) { n[i]=br.readLine(); m[i]=Integer.parseInt(br.readLine()); } //processing int high=m[0]; String name=n[0]; for(int i=1; i<m.length; i++) { if(m[i]>high) { high=m[i]; name=n[i]; } } System.out.println("Topper Name = "+name); System.out.println("Marks = "+high); }//main }//class

//array - name - phone num import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); String n[]=new String[5]; int p[]=new int[5]; //input System.out.println("Enter names and phone num"); for(int i=0; i<n.length; i++) { n[i]=br.readLine(); p[i]=Integer.parseInt(br.readLine()); } //processing System.out.println("Enter names to search"); String s=br.readLine(); KP/JAVA/ICSE/programs.doc Page 16/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

int found=0; for(int i=0; i<n.length; i++) { if(s.equals(n[i])) { System.out.println("Phone = "+p[i]); found=1; break; } } if(found==0) { System.out.println("Not found"); } }//main }//class

//array - bill import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); String n[]=new String[5]; int q[]=new int[5]; double p[]=new double[5]; //input System.out.println("Enter names, quan and price"); for(int i=0; i<n.length; i++) { n[i]=br.readLine(); q[i]=Integer.parseInt(br.readLine()); p[i]=Double.parseDouble(br.readLine()); } //processing System.out.println("Bill"); System.out.println("Name\tQuan\tPrice\tAmt"); double total=0; for(int i=0; i<n.length; i++) { double amt=p[i]*q[i]; System.out.println(n[i]+"\t"+q[i]+"\t"+p[i]+"\t"+amt); total+=amt; } System.out.println("Total = "+total); }//main }//class

KP/JAVA/ICSE/programs.doc

Page 17/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

//array - salesmen (sum of rows and cols) import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); double s[][]=new double[3][4]; //input for(int i=0; i<3; i++) { System.out.println("Enter sale for salesman "+(i+1)); for(int j=0; j<4; j++) { System.out.println("for zone "+(j+1)); s[i][j]=Double.parseDouble(br.readLine()); } } //processing System.out.println("Salesmen totals"); for(int i=0; i<3; i++) { double sum=0; for(int j=0; j<4; j++) { sum=sum+s[i][j]; } System.out.println("Salesman "+(i+1)+" = "+sum); sum=0; } System.out.println("Zone totals"); for(int i=0; i<4; i++) { double sum=0; for(int j=0; j<3; j++) { sum=sum+s[j][i]; } System.out.println("Zone "+(i+1)+" = "+sum); sum=0; } }//main }//class

//array - sum of diagonals import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); double a[][]=new double[3][3]; KP/JAVA/ICSE/programs.doc Page 18/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

//input System.out.println("Enter values for 3x3 array"); for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { a[i][j]=Double.parseDouble(br.readLine()); } } //processing double diag1=0; double diag2=0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { if(i==j) { diag1=diag1+a[i][j]; } if(i+j==2) { diag2=diag2+a[i][j]; } } } System.out.println("Sum of diagonal 1 = "+diag1); System.out.println("Sum of diagonal 2 = "+diag2); }//main }//class

//array - sum of diagonals import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); double a[][]=new double[3][3]; double b[][]=new double[3][3]; double c[][]=new double[3][3]; //input System.out.println("Enter values for a[2][2]"); for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { a[i][j]=Double.parseDouble(br.readLine()); } } System.out.println("Enter values for b[2][2]"); for(int i=0; i<2; i++) { for(int j=0; j<2; j++) KP/JAVA/ICSE/programs.doc Page 19/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

{ b[i][j]=Double.parseDouble(br.readLine()); } } //processing & output for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j]+"\t"); } System.out.println(); } }//main }//class

//Strings - count vowels import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a string : "); String s=br.readLine(); int count=0; for(int i=0; i<s.length(); i++) { char c=s.charAt(i); if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='a' || c=='e' || c=='i' || c=='o' || c=='u' ) { count++; } //switch can also be used } System.out.println("Vowels = "+count); }//main }//class

//Strings - palindrome import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a string : "); String s=br.readLine(); KP/JAVA/ICSE/programs.doc Page 20/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

String r=""; for(int i=s.length()-1; i>=0; i--) { char c=s.charAt(i); r=r+c; } if(r.equalsIgnoreCase(s)) { System.out.println("Plaindrome"); } else { System.out.println("Not a Plaindrome"); } }//main }//class

//Strings - Piglatin import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a string (in uppercase): "); String s=br.readLine(); for(int i=0; i<s.length(); i++) { char c=s.charAt(i); if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U') { String p=s.substring(i)+s.substring(0,i)+"AY"; System.out.println("Piglatin = "+p); break; } } }//main }//class

//Strings - Toggle Case import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a string : "); String s=br.readLine(); for(int i=0; i<s.length(); i++) { char c=s.charAt(i); KP/JAVA/ICSE/programs.doc Page 21/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

if(Character.isUpperCase(c)) { c=Character.toLowerCase(c); } else if(Character.isLowerCase(c)) { c=Character.toUpperCase(c); } System.out.print(c); }//i }//main }//class

//Strings - Words starting with A ot a import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a sentence : "); String s=br.readLine(); s=s+" "; String w=""; for(int i=0; i<s.length(); i++) { char c=s.charAt(i); w=w+c; if(c==' ') { if(w.charAt(0)=='A' || w.charAt(0)=='a') { System.out.println(w); } w=""; } } }//main }//class

//Strings - changing this to that (without replace()) import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a sentence : "); String s=br.readLine(); s=s+" "; KP/JAVA/ICSE/programs.doc Page 22/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

String w=""; for(int i=0; i<s.length(); i++) { char c=s.charAt(i); w=w+c; if(c==' ') { if(w.equals("this ")) { System.out.print("that "); } else { System.out.print(w); } w=""; } } }//main }//class

//Strings - changing this to that (without replace()) import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a sentence : "); String s=br.readLine(); s=s+" "; String w=""; String longest=""; for(int i=0; i<s.length(); i++) { char c=s.charAt(i); w=w+c; if(c==' ') { if(w.length()>longest.length()) { longest=w; } w=""; } } System.out.println("Longest word = "+longest); }//main }//class

//Strings - initials & name in reverse KP/JAVA/ICSE/programs.doc Page 23/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a name : "); String s=br.readLine(); int s1=s.indexOf(" "); int s2=s.lastIndexOf(" "); String f=s.substring(0,s1); String m=s.substring(s1+1,s2); String l=s.substring(s2+1); System.out.println(l+" "+m+" "+f); System.out.println(s.charAt(0)+"."+s.charAt(s1+1)+"."+s.charAt(s2+1)+"."); }//main }//class

//Strings - reversing a sentence import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a sentence : "); String s=br.readLine(); s=" "+s; String w=""; for(int i=s.length()-1; i>=0; i--) //will read in reverse { char c=s.charAt(i); w=c+w; //will straighten the word if(c==' ') { System.out.print(w); w=""; } } }//main }//class

//Triangle usinf OOP import java.io.*; class TRIANGLE { private double a,b,c,s; private void calcS() KP/JAVA/ICSE/programs.doc Page 24/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

{ s=(a+b+c)/2; } TRIANGLE() { a=b=c=s=0; } TRIANGLE(double a, double b, double c) { this.a=a; this.b=b; this.c=c; s=0; } double area() { calcS(); double ar=Math.sqrt(s*(s-a)*(s-b)*(s-c)); return ar; } void display() { double ar=area(); System.out.println("Area = "+ar); } } public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); System.out.println("Enter a : "); double a=Double.parseDouble(br.readLine()); System.out.println("Enter b : "); double b=Double.parseDouble(br.readLine()); System.out.println("Enter c : "); double c=Double.parseDouble(br.readLine()); TRIANGLE obj=new TRIANGLE(a,b,c); obj.display(); }//main }//class

//array - Bubble sorting numbers in ascending order import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); int a[]=new int[5]; //input KP/JAVA/ICSE/programs.doc Page 25/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

System.out.println("Enter numbers "); for(int i=0; i<a.length; i++) { a[i]=Integer.parseInt(br.readLine()); } //bubble sorting for(int i=0;i<a.length-1;i++) { for(int j=0;j<a.length-1-i;j++) { if(a[j]>a[j+1])//change sign for descending order, use compareTo() for strings { int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; }//if }//j }//i System.out.println("Sorted List "); for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } }//main }//class

//array - Selection sorting numbers in ascending order import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); int a[]=new int[5]; //input System.out.println("Enter numbers "); for(int i=0; i<a.length; i++) { a[i]=Integer.parseInt(br.readLine()); } //selection sorting for(int i=0;i<a.length-1;i++) { int s=a[i]; int p=i; for(int j=i+1;j<a.length;j++) { if(a[j]<s) //change sign for descending order, , use compareTo() for strings { s=a[j]; p=j; }//if }//j a[p]=a[i]; a[i]=s; }//i KP/JAVA/ICSE/programs.doc Page 26/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

System.out.println("Sorted List "); for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } }//main }//class

//array - sequential / linear search import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); int a[]=new int[5]; //input System.out.println("Enter numbers "); for(int i=0; i<a.length; i++) { a[i]=Integer.parseInt(br.readLine()); } //search System.out.println("Enter value to search"); int s=Integer.parseInt(br.readLine()); boolean found=false; for(int i=0;i<a.length;i++) { if(s==a[i]) // use equals(),equalsIgnoreCase() OR compareTo() for strings { System.out.println("Found at "+i); found=true; break; } } if(!found) //found==false can also be written { System.out.println("Not found"); } }//main }//class

//array - binary search import java.io.*; public class ICSE { public static void main(String args[])throws IOException { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); int a[]=new int[5]; KP/JAVA/ICSE/programs.doc Page 27/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

//input System.out.println("Enter numbers "); for(int i=0; i<a.length; i++) { a[i]=Integer.parseInt(br.readLine()); } //search System.out.println("Enter a value to be seached "); int s=Integer.parseInt(br.readLine()); int b=0, e=a.length-1, m; while(b<=e) { m=(b+e)/2; if(s==a[m]) //use string functions if a string array { System.out.println("Found at "+m); break; } else if(s>a[m]) b=m+1; //change sign if array in descending order else e=m-1; }//while if(b>e) System.out.println("Not found"); }//main }//class

//Quick look on functions class ICSE { void mfunctions() { System.out.println(Math.pow(25,2)); System.out.println(Math.sqrt(25)); System.out.println(Math.cbrt(27)); System.out.println(Math.abs(-5.25)); System.out.println(Math.floor(-21.9)); System.out.println(Math.ceil(-21.1)); System.out.println(Math.max(8,3)); System.out.println(Math.min(5.0,3.0)); System.out.println(Math.random()); System.out.println(Math.round(27.5)); System.out.println(Math.rint(27.5)); //Some other functions //sin, cos, tan, acos, asin, atan, atan2, log, exp, log10 }//mfunction void cfunctions() { System.out.println(Character.isDigit('A')); System.out.println(Character.isLetter('A')); System.out.println(Character.isLetterOrDigit('A')); KP/JAVA/ICSE/programs.doc Page 28/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

System.out.println(Character.isLowerCase('A')); System.out.println(Character.isUpperCase('A')); System.out.println(Character.isWhitespace('A')); System.out.println(Character.toLowerCase('A')); System.out.println(Character.toUpperCase('A')); }//cfunctions void sfunctions() { String s="CANARA BANK"; System.out.println(s.charAt(2)); System.out.println(s.compareTo("ABC")); System.out.println(s.concat("APPLICATIONS")); System.out.println(s.endsWith("CAN")); System.out.println(s.startsWith("CAN")); System.out.println(s.equals("canara bank")); System.out.println(s.equalsIgnoreCase("canara bank")); System.out.println(s.indexOf('A',2)); System.out.println(s.indexOf("AN")); System.out.println(s.lastIndexOf('Z')); System.out.println(s.length()); System.out.println(s.replace("A","O")); System.out.println(s.substring(2,6)); System.out.println(s.toLowerCase()); System.out.println(s.toUpperCase()); System.out.println(s.trim()); }//sfunctions void sbfunctions() { StringBuffer s=new StringBuffer("ABC"); s.append("DE"); System.out.println(s); s.insert(3,"0"); System.out.println(s); s.setCharAt(3,'+'); System.out.println(s); s.delete(1,3); System.out.println(s); s.setLength(3); System.out.println(s); s.reverse(); System.out.println(s); }//sfunctions public static void main(String args[]) { ICSE obj=new ICSE(); KP/JAVA/ICSE/programs.doc Page 29/30

Fundamental and important ICSE 10 programs

Free download: www.kishorepandit.com

System.out.println("Math functions"); obj.mfunctions(); System.out.println("Char functions"); obj.cfunctions(); System.out.println("String functions"); obj.sfunctions(); System.out.println("S' Buffer functions"); obj.sbfunctions(); }//main }//class Notes:

[END] KP/JAVA/ICSE/programs.doc Page 30/30

You might also like