i.c.s.e computer project

Upload: stephenwei

Post on 15-Oct-2015

538 views

Category:

Documents


8 download

DESCRIPTION

For all ICSE students.

TRANSCRIPT

Programs on Sequential Structure1. W.A.P to swap the values stored in two variables (using a third variable).public class prog1 { public static void main() { int a=10 , b = 20,c; System.out.println("THE VALUES BEFORE SWAPPING ARE"); System.out.println("a=\t"+ a +"\nb=\t"+b); c=a; a=b; b=c; System.out.println("THE VALUES AFTER SWAPPING ARE"); System.out.println("a=\t"+ a +"\nb=\t"+b); } }Name Of Variable Data TypeDescription

a intTo store the first value.

b intTo store the second value.

c int3rd variable for swapping.

Variable Description

2. W.A.P to swap the values stored in two variables (without using a third variable).public class prog2 { public static void main () { int a=10 , b = 20; System.out.println("THE VALUES BEFORE SWAPPING ARE "); System.out.println("a=\t"+ a +"\nb=\t"+b); a = a+b; b = a-b; a = a-b; System.out.println("THE VALUES AFTER SWAPPING ARE"); System.out.println("a=\t"+ a +"\nb=\t"+b); } }

Variable DescriptionName Of Variable Data TypeDescription

a intTo store the first value.

b intTo store the second value.

3. Find out the total equivalent resistance in a circuit in which 2 resistors are connected in parallel and series.import java.io.*;public class prog3 { public static void main () throws IOException { BufferedReader obj= new BufferedReader( new InputStreamReader (System.in )); int R1,R2,s= 0; float p=0.0f; System.out.println("Enter the resistance in first resistor"); R1 = Integer.parseInt(obj.readLine()); System.out.println("Enter the resistance in second resistor"); R2 = Integer.parseInt(obj.readLine()); p = (float) (R1*R2)/(R1+R2); s = R1 + R2 ; System.out.println( "Total equivalent resistance in parallel circuit =" + p); System.out.println( "Total equivalent resistance in series circuit =" + s); } }

Variable DescriptionName Of Variable Data TypeDescription

R1 intTo store the resistance in first resistor.

R2 intTo store the resistance in second resistor.

s intTo store the equivalent resistance in series circuit.

p floatTo store the equivalent resistance in parallel circuit.

4. W.A.P to input total amount in Rupees and find out the denominations in least possible numbers of all possible currency notes (i.e. Rs 1000/- , Rs 500/- , Rs 100/- , Rs 50/- , Rs 20/- , Rs 10/-,Rs 5/- , Rs 2/- , Rs 1/- ) for the amount entered by the user.import java.io.*;public class prog4 { public static void main () throws IOException { BufferedReader obj= new BufferedReader( new InputStreamReader (System.in )); int amt,th,fhrd,hrd,fty,tw,ten,five,two,one; System.out.println("Enter the Amount " ); amt = Integer.parseInt(obj.readLine()); th = amt / 1000; amt = amt % 1000; fhrd = amt / 500; amt = amt % 500; hrd = amt / 100; amt = amt % 100; fty = amt / 50; amt = amt % 50; tw = amt / 20; amt = amt % 20;

ten = amt / 10; amt = amt % 10;

five = amt / 5; amt = amt % 5;

two = amt / 2; amt = amt % 2; one = amt / 1; amt = amt % 1; System.out.println( "The number of Rs 1000 notes are =" + th); System.out.println( "The number of Rs 500 notes are =" + fhrd); System.out.println( "The number of Rs 100 notes are =" + hrd ); System.out.println( "The number of Rs 50 notes are =" + fty); System.out.println( "The number of Rs 20 notes are =" + tw); System.out.println( "The number of Rs 10 notes are =" + ten); System.out.println( "The number of Rs 5 notes are =" + five); System.out.println( "The number of Rs 2 coins are =" + two); System.out.println( "The number of Rs 1 coins are =" + one); }} Variable DescriptionName Of Variable Data Type Description

amt intTo store the amount entered by the user.

th intTo calculate and store the number of thousand rupee notes.

fhrd intTo calculate and store the number of five hundred rupee notes.

hrd intTo calculate and store the number of hundred rupee notes.

fty intTo calculate and store the number of fifty rupee notes.

tw intTo calculate and store the number of twenty rupee notes.

ten intTo calculate and store the number of ten rupee notes.

five intTo calculate and store the number of five rupee notes.

two intTo calculate and store the number of two rupee coins.

one intTo calculate and store the number of one rupee coins.

5. W.A.P to input the Basic Salary of an employee and calculate his Gross Salary and Net Salary based on the information given below :- D.A. (Dearness Allowance) = 75% of Basic Salary H.R.A. (House Rent Allowance) = 25% of Basic Salary P.F. (Provident Fund) = 12% (Basic + D.A.) Gross Salary = Basic + D.A. + H.R.A. Net Salary = Gross Salary P.F.

import java.util.Scanner;public class prog5 { public static void main() { int BS,DA,HRA,PF,GS,NS; Scanner x=new Scanner(System.in); System.out.println("Enter the Basic salary of an employee"); BS=x.nextInt(); DA= (75*BS)/100; HRA= (25*BS)/100; PF=12*(BS+DA)/100; GS=BS+DA+HRA; NS=GS-PF; System.out.println("Gross salary="+GS); System.out.println("Net salary="+NS); } } Variable DescriptionName Of Variable Data Type Description

BS intTo store the Basic Salary of an employee.

DA intTo calculate and store the Dearness Allowance.

HRA intTo calculate and store the House Rent Allowance.

PF intTo calculate and store the Provident Fund.

GS intTo calculate and store the Gross Salary.

NS intTo calculate and store the Net Salary.

Programs on Selection Structure1. W.A.P to input three numbers and print the 1st largest, 2nd largest and the 3rd largest in sequence without using arrays and without implementing sorting.import java.io.*;public class prog6 { public static void main () throws IOException { BufferedReader obj= new BufferedReader( new InputStreamReader (System.in)); int a , b , c, max=0, min = 0, mid = 0; System.out.println("Enter the 1st number"); a=Integer.parseInt(obj.readLine()); System.out.println("Enter the 2nd number"); b=Integer.parseInt(obj.readLine()); System.out.println("Enter the 3rd number"); c=Integer.parseInt(obj.readLine());

if( a > b && a >c) { max= a; }

if( b > a && b >c) { max = b; }

if( c > a && c >b) { max= c; }

if ( a < b && a < c) { min = a; }

if ( b < a && b < c) { min = b; }

if ( c < b && c < a) { min = c; }

if ( a< max && a> min) { mid = a; }

if ( b< max && b> min) { mid = b; }

if ( c< max && c> min) { mid = c; }

System.out.println("The largest number=" + max); System.out.println("The 2nd largest number=" + mid); System.out.println("The 3rd largest number=" + min); } }

Name Of Variable Data Type Description

a intTo store the first number entered by the user.

b intTo store the second number entered by the user.

c intTo store the third number entered by the user.

max intTo store the 1st largest number.

mid intTo store the 2nd largest number.

min intTo store the 3rd largest number.

Variable Description

2. W.A.P to enter the co-efficient of Quadratic Equation and find out the roots, Roots of a Quadratic Equation and mention whether the Roots are Real, Equal or Imaginary.

3. JSEB charges their consumer according to the tariff chart given below, Units Consumed Charges Up to 50 units Free 51 100 0.75p 101 250 1.25p 252 500 2.50p Above 500 3.00p WAP in java to input the units consumed and display the electricity bill calculated as per following. A surcharge of Rs. 75.00 is levied if the units consumed exceed Rs. 250. A fixed rental of Rs. 180.00 is charged to all the consumers. Over an above a Service Tax of 12 % is addon to the above and an Edu. Cess of 2% on the Service Tax is charges as extra. Calculate the Bill amount if on the whole a rebate (discount) of 4% is allowed.import java.util.Scanner;public class prog8 { public static void main() { int u,st; double a1,a2; Scanner x=new Scanner(System.in); System.out.println("Enter the units consumed"); u=x.nextInt(); if(u=51)&&(u=101)&&(u=251)&&(u500) { st=(25/2*u); a1=(u*3.00)+180+(2*st/100)+75; a2=(96*a1)/100; System.out.println("Bill amount=Rs "+a2); } } }

Variable Description

Name Of Variable Data Type Description

uintTo store the units consumed entered by the user.

stintTo calculate and store the Service Tax.

a1 doubleTo calculate the total amount excluding the rebate.

a2doubleTo calculate the total Bill amount including the rebate.

4. A cab driver charges Rs.15.00 on the first meter (km). He charges an extra of Rs.22.00 on the next 5 kms. Anything beyond that is charged at Rs.2.50 per km. W.A.P to input distance covered and calculate the cab charges.import java.io.*;public class prog9 { public static void main()throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); int km; double ch=0; System.out.println("Enter the distance in kilometer"); km=Integer.parseInt(in.readLine()); if(km==1) ch=15; else if(km>1&&km0) { k = n % 10; for ( i =1;i0) { k=n % 10; sum = sum + (k*k*k); n =n/10; } if ( sum ==temp) { System.out.println("Is an Armstrong number "); } else { System.out.println("Is not an Armstrong number "); } } }

Variable Description

Name Of Variable Data Type Description

n intTo store the number entered by the user.

temp intTo store the value of variable n.

kintTo store the last digit of the number.

sumintTo store the sum of cubes of each digit of the number.

3. Input any number and check if it is a Perfect number or not. import java.io.*;public class prog13 { public static void main () throws IOException { BufferedReader obj= new BufferedReader( new InputStreamReader (System.in)); int n, sum=0,i; System.out.println("Enter a number"); n=Integer.parseInt(obj.readLine ()); for (i=1;i0) { k= n% 10; rev = (rev*10) + k; n=n/10; } if (temp==rev) { System.out.println("Is a Palindrome number "); } else { System.out.println("Is not a Palindrome number "); } } }

Variable Description

Name Of Variable Data Type Description

n intTo store the number entered by the user.

temp intTo store the value of variable n.

kintTo store the last digit of the number.

revintTo store the reverse of the number entered by the user.

5. WAP to input a number and check whether it is a Kaprekars number or not.

6. WAP to check whether a number entered by the user is a Automorphic number.

import java.io.*;public class prog16 { public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader (System.in)); int n, n2, c=0 , temp, k; double g, m; System.out.println("Enter a number " ); n=Integer.parseInt(obj.readLine ()); temp=n; n2= n*n; while(n>0) { k=n%10; c++; n=n/10; }

g= Math.pow(10,c); m= n2% g; if( m== temp) { System.out.println("Is an Automorphic number "); } else { System.out.println("Is not an Automorphic number "); } } }

Variable Description Name Of Variable Data Type Description

n intTo store the number entered by the user.

temp intTo store the value of variable n.

kintTo store the last digit of the number.

n2 intTo store the square of the number entered by the user.

c intCounter variable used to count the number of digits in the number.

gdoubleTo store the cube of 10 to the power c.

mdoubleTo store the remainder of the square of the number divided by the variable g.

7. WAP to check whether a number entered by the user is an Evil number or not.import java.io.*;public class prog17 { public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader (System.in)); int n,c=0,temp,k; System.out.println("Enter a number " ); n=Integer.parseInt(obj.readLine ()); temp=n; while (n>0) { k=n%2; if (k==1) { c++; } n=n/2; } if(c%2==0) { System.out.println("Is an Evil number "); } else { System.out.println("Is not an Evil number "); } } } Variable Description

Name Of Variable Data Type Description

n intTo store the number entered by the user.

temp intTo store the value of variable n.

k intTo store the binary digits of the number.

c intTo count the number of ones in the binary form of the number.

8. Input 2 nos. and check whether they make up Bruns Constant or not, Bruns Constant is sum of reciprocal of Twin Prime Nos.

import java.util.Scanner;public class prog18 { public static void main() { Scanner in=new Scanner(System.in); System.out.println("Enter the first number"); int n1=in.nextInt(); System.out.println("Enter the second number"); int n2=in.nextInt(); int i,c=0,k=0; if(n1-n2==2 || n1-n2==-2) { for(i=1;i