I.C.S.E Computer Project



Comments



Description

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); } } Variable Description Name Of Variable Data Type Description a int To store the first value. b int To store the second value. c int 3 rd variable for swapping. 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 Description Name Of Variable Data Type Description a int To store the first value. b int To 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 Description Name Of Variable Data Type Description R1 int To store the resistance in first resistor. R2 int To store the resistance in second resistor. s int To store the equivalent resistance in series circuit. p float To 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 Description Name Of Variable Data Type Description amt int To store the amount entered by the user. th int To calculate and store the number of thousand rupee notes. fhrd int To calculate and store the number of five hundred rupee notes. hrd int To calculate and store the number of hundred rupee notes. fty int To calculate and store the number of fifty rupee notes. tw int To calculate and store the number of twenty rupee notes. ten int To calculate and store the number of ten rupee notes. five int To calculate and store the number of five rupee notes. two int To calculate and store the number of two rupee coins. one int To 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 Description Name Of Variable Data Type Description BS int To store the Basic Salary of an employee. DA int To calculate and store the Dearness Allowance. HRA int To calculate and store the House Rent Allowance. PF int To calculate and store the Provident Fund. GS int To calculate and store the Gross Salary. NS int To calculate and store the Net Salary. Programs on Selection Structure 1. W.A.P to input three numbers and print the 1 st largest, 2 nd largest and the 3 rd 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); } } Variable Description Name Of Variable Data Type Description a int To store the first number entered by the user. b int To store the second number entered by the user. c int To store the third number entered by the user. max int To store the 1 st largest number. mid int To store the 2 nd largest number. min int To store the 3 rd largest number. 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 add‐on 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<=50) { st=(25/2*u); a1=180+(2*st/100); a2=(96*a1)/100; System.out.println("Bill amount=Rs "+a2); } else if((u>=51)&&(u<=100)) { st=(25/2*u); a1=(u*0.75)+180+(2*st/100); a2=(96*a1)/100; System.out.println("Bill amount=Rs "+a2); } else if((u>=101)&&(u<=250)) { st=(25/2*u); a1=(u*1.25)+180+(2*st/100); a2=(96*a1)/100; System.out.println("Bill amount=Rs "+a2); } else if((u>=251)&&(u<=500)) { st=(25/2*u); a1=(u*2.50)+180+(2*st/100)+75; a2=(96*a1)/100; System.out.println("Bill amount=Rs "+a2); } else if(u>500) { 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 u int To store the units consumed entered by the user. st int To calculate and store the Service Tax. a1 double To calculate the total amount excluding the rebate. a2 double To 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&&km<6) ch=15+22; else ch=15+22+(km-6)*2.50; System.out.println("The cab charge is=Rs "+ch); } } Variable Description 5. Write a menu driven program to calculate the volume of  Sphere (4/3. .r*r*r)  Cube (side*side*side)  Cuboid (l*b*h) import java.util.Scanner; public class prog10 { public static void main() { int s,l,b,h,ch; double v,r; Scanner x=new Scanner(System.in); System.out.println("1. To calculate the volume of sphere"); Name Of Variable Data Type Description km int To store the distance in kilometer entered by the user. ch double To calculate and store the total cab charge. System.out.println("2. To calculate volume of cube"); System.out.println("3. To calculate the volume of cuboid"); System.out.println("Enter your choice"); ch=x.nextInt(); switch(ch) { case 1: System.out.println("Enter the radius of sphere"); r=x.nextDouble(); v=1.33*Math.PI*(Math.pow(r,3)); System.out.println("Volume of sphere="+v); break; case 2: System.out.println("Enter the side of cube"); s=x.nextInt(); v=Math.pow(s,3); System.out.println("Volume of cube="+v); break; case 3: System.out.println("Enter the length of cuboid"); l=x.nextInt(); System.out.println("Enter the breadth of cuboid"); b=x.nextInt(); System.out.println("Enter the height of cuboid"); h=x.nextInt(); v=l*b*h; System.out.println("Volume of cuboid="+v); break; default: System.out.println("Wrong Choice entered"); } } } Variable Description Name Of Variable Data Type Description ch int To store the choice entered by the user. r double To store the radius of sphere entered by the user. s int To store the side of cube entered by the user. l int To store the length of cuboid entered by the user. b int To store the breadth of cuboid entered by the user. h int To store the height of cuboid entered by the user. v double To calculate the volume as per the user’s choice. Programs on Iterative Structure 1.Input any number and check if it is a Special (Krishnamurthy) number or not. import java.io.*; public class prog11 { public static void main () throws IOException { BufferedReader obj= new BufferedReader( new InputStreamReader (System.in)); int n,temp,k,i,f=1,sum=0; System.out.println("Enter a number " ); n=Integer.parseInt(obj.readLine ()); temp= n; while (n>0) { k = n % 10; for ( i =1;i<=k;i++) { f=f*i; } sum= sum+ f; f=1; n= n/10; } if ( sum==temp) { System.out.println ("Is a Special number " ); } else { System.out.println ("Is not a special number "); } } } Variable Description Name Of Variable Data Type Description n int To store the number entered by the user. temp int To store the value of variable n. k int To store the last digit of the number. f int To calculate and store the factorial of digit. i int for loop sum int To store the sum of factorial of all the digits. 2. Input any number and check if it is an Armstrong (Narcissistic) number or not. import java.io.*; public class prog12 { public static void main () throws IOException { BufferedReader obj= new BufferedReader( new InputStreamReader (System.in)); int n, temp, sum=0,k; System.out.println("Enter a number " ); n=Integer.parseInt(obj.readLine ()); temp=n; while (n>0) { 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 int To store the number entered by the user. temp int To store the value of variable n. k int To store the last digit of the number. sum int To 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;i<n;i++) { if (n% i ==0) { sum = sum + i; } } if (sum== n) { System.out.println("Is a Perfect number "); } else { System.out.println("Is not a Perfect number "); } } } Variable Description Name Of Variable Data Type Description n int To store the number entered by the user. i int for loop sum int To store the sum of all factors excluding the number. 4. Input any number and check if it is a Palindrome or not. import java.io.*; public class prog14 { public static void main () throws IOException { BufferedReader obj= new BufferedReader( new InputStreamReader (System.in)); int n, rev=0, temp, k; System.out.println("Enter a number " ); n=Integer.parseInt(obj.readLine ()); temp= n; while (n>0) { 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 int To store the number entered by the user. temp int To store the value of variable n. k int To store the last digit of the number. rev int To store the reverse of the number entered by the user. 5. WAP to input a number and check whether it is a Kaprekar‟s 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 int To store the number entered by the user. temp int To store the value of variable n. k int To store the last digit of the number. n2 int To store the square of the number entered by the user. c int Counter variable used to count the number of digits in the number. g double To store the cube of 10 to the power c. m double To 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 int To store the number entered by the user. temp int To store the value of variable n. k int To store the binary digits of the number. c int To count the number of ones in the binary form of the number. 8. Input 2 nos. and check whether they make up Brun‟s Constant or not, Brun‟s 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<=n1;i++) { if(n1%i==0) c++; } for(i=1;i<=n2;i++) { if(n2%i==0) k++; } if(c==2 && k==2) { System.out.println("They make up Brun's Constant"); } else { System.out.println("They do not make up Brun's Constant"); } } } } Variable Description Name Of Variable Data Type Description n1 int To store the first number entered by the user. n2 int To store the second number entered by the user. i int For loop c int To count the number of factors of the first number. k int To count the number of factors of the second number. 9. WAP to check whether two numbers entered by the user belong to the set of Amicable Pair or not. import java.io.*; public class prog19 { public static void main () throws IOException { BufferedReader obj= new BufferedReader( new InputStreamReader(System.in)); int n1, n2, c=0, k=0, i, j; System.out.println("Enter the first number" ); n1=Integer.parseInt(obj.readLine ()); System.out.println("Enter the second number" ); n2=Integer.parseInt(obj.readLine ()); for(i=1;i<n1;i++) { if(n1%i==0) { c=c+i; } } for(j=1;j<n2;j++) { if(n2%j==0) { k=k+j; } } if(c==n2 && k==n1) { System.out.println("They are an Amicable pair"); } else { System.out.println("They are not an Amicable pair"); } } } Variable Description Name Of Variable Data Type Description n1 int To store the first number entered by the user. n2 int To store the second number entered by the user. i int For loop j int For loop c int To calculate and store the sum of factors of the first number excluding the number. k int To calculate and store the sum of factors of the second number excluding the number. 10. Write a program to check whether the number entered by the user is a Unique Integer or not. import java.io.*; public class prog20 { public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader (System.in)); int n,k,temp,k1,c=0; System.out.println("Enter a number " ); n=Integer.parseInt(obj.readLine ()); while(n>0) { k=n%10; n=n/10; temp=n; while (temp>0) { k1=temp%10; if(k==k1) { c++; } temp=temp/10; } } if(c>0) { System.out.println("Is not a Unique Integer"); } else { System.out.println("Is a Unique Integer"); } } } Variable Description Name Of Variable Data Type Description n int To store the number entered by the user. k int To store the last digit of the number. temp int To store the number excluding the last digit. k1 int To store the last digit of the number stored in variable temp. c int To count the number of digits repeated in the number. Programs on Single Dimensional Arrays 1. Input 10 nos. in an array and search a given number from within that array using Linear Search technique, and display whether found or not along with its position. import java.io.*; public class prog21 { public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader (System.in)); int i, n, c=0; int arr[]= new int [10]; System.out.println("Enter any 10 numbers"); for(i=0;i<10;i++) { arr[i]=Integer.parseInt(obj.readLine()); } System.out.println("Enter a search element"); n=Integer.parseInt(obj.readLine()); for(i=0;i<10;i++) { if (arr[i]==n) { System.out.println("Search element found"); System.out.println("Position of search element="+ i); c++; break; } } if(c==0) { System.out.println("Search element not found"); } } } Variable Description Name Of Variable Data Type Description i int For loop n int To store the search element that user enters. c int Counter Variable arr int Array variable to store 10 numbers user enters. 2. Input 10 nos. in an array and search a given number from within that array using Binary Search technique. import java.io.*; public class prog22 { public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader (System.in)); int i, n, low=0, up=9, c=0, mid=0; int arr[]= new int [10]; System.out.println("Enter any 10 numbers"); for(i=0;i<10;i++) { arr[i]=Integer.parseInt(obj.readLine()); } System.out.println("Enter the search element"); n=Integer.parseInt(obj.readLine()); while (low<=up) { mid=(low+up)/2; if(arr[mid] == n) { c++; break; } if(arr[mid] <n) { low=mid +1; } if(arr[mid] >n) { up=mid -1; } } if(c>0) { System.out.println("Search element found"); } else { System.out.println("Search element not found"); } } } Variable Description Name Of Variable Data Type Description i int For loop n int To store the search element that user enters. low int To store the first element of the array. up int To store the last element of the array. mid int To store the middle element of the array. c int Counter Variable. arr int Array variable to store 10 numbers that user enters. 3. Sort an array of 10 elements in Ascending Order using Bubble Sort technique. Display the elements of the array before and after sorting. import java.io.*; public class prog23 { public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader (System.in)); int i, j, t; int arr[]= new int [10]; System.out.println("Enter any 10 numbers"); for(i=0;i<10;i++) { arr[i]=Integer.parseInt(obj.readLine()); } System.out.println("The elements before sorting are"); for(i=0;i<10;i++) { System.out.println(arr[i]); } for(i=0;i<9;i++) { for(j=i+1;j<10;j++) { if(arr[i]>arr[j]) { t=arr[i]; arr[i]=arr[j]; arr[j]=t; } } } System.out.println("The elements after sorting are"); for(i=0;i<10;i++) { System.out.println(arr[i]); } } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop t int Variable used for swapping. arr int Array variable to store 10 numbers that user enters. 4. Sort an array of 10 elements in Descending Order using Selection Sort technique. Display the elements of the array before and after sorting. import java.io.*; public class prog24 { public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader (System.in)); int i, j, t, min, pos; int arr[]= new int[10]; System.out.println("Enter any 10 numbers"); for(i=0;i<10;i++) { arr[i]=Integer.parseInt(obj.readLine()); } System.out.println("The elements before sorting are"); for(i=0;i<10;i++) { System.out.println(arr[i]); } for(i=0;i<=8;i++) { min=arr[i]; pos=i; for(j=i+1;j<=9;j++) { if(arr[j]>min) { min=arr[j]; pos=j; } } t=arr[i]; arr[i]=min; arr[pos]=t; } System.out.println("The elements after sorting are"); for(i=0;i<10;i++) { System.out.println(arr[i]); } } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop min int To store the smallest element from within the array. pos int To store the position of the smallest element. t int Variable used for swapping. arr int Array variable to store 10 numbers that user enters. 5. Store 5 numbers in array A [ ] and 5 numbers in array B [ ] and merge them into array C [ ]. Display all the three arrays after merging. import java.io.*; public class prog25 { public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader(System.in)); int i; int a[]= new int [5]; int b[]= new int [5]; int c[]= new int [10]; System.out.println("Enter 5 numbers in array A"); for(i=0;i<5;i++) { a[i]=Integer.parseInt(obj.readLine()); } System.out.println("Enter 5 numbers in array B"); for(i=0;i<5;i++) { b[i]=Integer.parseInt(obj.readLine()); } for(i=0;i<5;i++) { c[i]=a[i]; c[i+5]=b[i]; } System.out.println("The elements of Array 1 are :- "); for(i=0;i<5;i++) { System.out.println(a[i]); } System.out.println("The elements of Array 2 are :-"); for(i=0;i<5;i++) { System.out.println(b[i]); } System.out.println("The elements of Array 3 are :-"); for(i=0;i<10;i++) { System.out.println(c[i]); } } } Variable Description Name Of Variable Data Type Description i int For loop a int Array variable to store 5 numbers that user enters in array a[]. b int Array variable to store 5 numbers that user enters in array b[]. c int Array variable to store 10 numbers after merging the 2 arrays. Programs on Functions 1. Create overloaded methods named void calc_volume ( ), that has been overloaded to perform the following functions – Volume of Sphere Volume of Cylinder Volume of Cone Write a menu driven program in Java to display the above 3 menus and execute the overloaded methods and display the volume in the respective functions :- calc_volume (double) for sphere calc_volume (double, double) for cylinder calc_volume (float, float) for cone import java.io.*; public class prog26 { public void calc_volume(double r) { double vol= (double) 4/3*3.14*r1*r1*r1; System.out.println("The volume of sphere=" + vol); } public void calc_volume(double r,double h) { double vol= 3.14*r*r*h; System.out.println("The volume of cylinder=" + vol); } public void calc_volume(float a,float b) { float vol= (float) 1/3*22/7*a*a*b; System.out.println("The volume of cone=" + vol); } public static void main () throws IOException { BufferedReader obj= new BufferedReader(new InputStreamReader(System.in)); { int ch; System.out.println("1. To find volume of sphere"); System.out.println("2. To find volume of cylinder"); System.out.println("3. To find volume of cone"); System.out.println("Enter your choice"); ch=Integer.parseInt(obj.readLine()); prog26 in=new prog26(); switch (ch) { case 1: { System.out.println("Enter the radius of sphere "); double r1= Double.parseDouble(obj.readLine()); in.calc_volume (r1); } break; case 2: { System.out.println("Enter the radius of cylinder "); double r= Double.parseDouble(obj.readLine()); System.out.println("Enter the height of cylinder "); double h= Double.parseDouble(obj.readLine()); in.calc_volume (r,h); } break; case 3: { System.out.println("Enter the radius of cone "); float a= Float.parseFloat(obj.readLine()); System.out.println("Enter the height of cone "); float b= Float.parseFloat(obj.readLine()); in.calc_volume (a,b); } break; default: { System.out.println("Wrong choice entered"); } } } } } Variable Description Name Of Variable Data Type Description a float To store the radius of cone. b float To store the height of cone. r double To store the radius of cylinder. h double To store the height of cylinder. r1 double To store the radius of sphere. ch int To store the choice entered by the user. vol float, double To calculate the volume of sphere, cone, cylinder as per the user’s choice. 2. Write a program to create a class which will have the following functions: int NumFun() : to create a single dimensional array of size 50 integers and return the smallest integer. int NumFun(int) : to calculate and return sum of factors, excluding the number, of the number returned by the above function. int NumFun(int, int) : to check whether the two results returned by the above functions are same or not and accordingly display “perfect” or “non- perfect”. void main() : to execute all the above functions accordingly, using the concept of function overloading. import java.io.*; public class prog27 { InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); int NumFun()throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); int arr[]= new int[50]; int i, j, s; System.out.println("Enter 50 numbers"); for(i=0;i<50;i++) { arr[i]=Integer.parseInt(in.readLine()); } s=arr[0]; for(i=1;i<50;i++) { if(arr[i]<s) s=arr[i]; } return(s); } int NumFun(int num) { int sum=0,i; for(i=1;i<num;i++) { if(num%i==0) { sum=sum+i; } } return(sum); } int NumFun(int a,int b) { if(a==b) return(1); else return(0); } public static void main()throws IOException { prog27 obj=new prog27(); int k=obj.NumFun(); int sf=obj.NumFun(k); int p=obj.NumFun(k,sf); if(p==1) { System.out.println("PERFECT"); } else { System.out.println("NOT PERFECT"); } } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop s int To store the smallest integer from within the array. arr int Array variable to store 50 integers that user inputs. sum int To store the sum of factors of the smallest integer excluding the number. p int To check if the smallest integer is a perfect number or not. 3. Define a class with the following specifications: int Armstr(int) : to check a number and return 1 if the number is Armstrong otherwise return 0. void prime(int, int ) : to display all prime numbers within the range entered by the user. void main : to input number and display the message depending upon the value returned by Armstr(int) and to input range required for prime(int, int). import java.io.*; public class prog28 { int Armstr(int num) { int t=num, d, nn=0; while(t>0) { d=t%10; nn=nn+(d*d*d); t=t/10; } if(nn==num) return(1); else return(0); } void prime(int a, int b) { int i ,num , j , c=0; System.out.println("The prime numbers in the range are:-"); for(i=a;i<=b;i++) { num= i; c=0; for(j=1;j<(num/2)+1;j++) { if(num%j==0) c=c+1; } if(c==1) System.out.print(num+","); } } public static void main()throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); prog28 obj=new prog28(); System.out.println("Enter a number to check if the number is armstrong or not"); int num=Integer.parseInt(in.readLine()); System.out.println("Enter the lower limit to print all the numbers which are prime within the range"); int a =Integer.parseInt(in.readLine()); System.out.println("Enter the upper limit to print all the numbers which are prime within the range"); int b =Integer.parseInt(in.readLine()); int k= obj.Armstr(num); if(k==1) { System.out.println("It is an Armstrong number"); } else { System.out.println("It is not an Armstrong number"); } obj.prime(a,b); } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop num int To store a number entered by the user and check if it is Armstrong or not. t int To store the value of variable n. d int To store the last digit of the number. nn int To store the sum of cubes of each digit of the number. c int To count the number of factors of each number within the range. k int To check if the number entered by the user is an Armstrong number or not. 4. A class Myseries is declared to find the sum of the following series. sum = 1*y2/1! + 2*y4/2! + 3*y6/3! + 4*y8/4! … till 10 terms Data members int y : integer number long sum : integer to store sum Member Functions void getnum( ) : to accept the integer „y‟ void getseries( ) : to find the sum of elements of the series long getpower(int, int) : to calculate „y‟ to the power „x‟ long getfact(int) : to calculate the factorial of the denominator void display( ) : to display finally the sum of the series 5. Write a function named int sort_digit (int) in Java to pass an integer number as argument that might contain any number of digits, and sort each individual digits of the number in ascending order and return the same, (without implementing arrays). e.g Sample Input: 4987 Sample Output: 4789 import java.io.*; public class prog30 { int sort_digit(int num) { int i, d, t; String str=""; for(i=0;i<=9;i++) { t=num; while(t>0) { d=t%10; if(d==i) str=str+d; t=t/10; } } return(Integer.parseInt(str)); } public static void main()throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); int num; prog30 obj=new prog30(); System.out.println("Enter any number to arrange in ascending order"); num=Integer.parseInt(in.readLine()); int k=obj.sort_digit(num); System.out.println("Digits in Ascending order\t "+k); } } Variable Description Name Of Variable Data Type Description i int For loop num int To store the number entered by the user. t int To store the value of variable n. d int To store the last digit of the number. str string To store the number after arranging each of the digits of the number. Programs on Strings 1. Input a sentence and display the total no. of letters, digits, uppercase letters, lowercase letters, spaces, special characters, vowels and consonants are present in it. import java.io.*; class prog31 { public static void main () throws IOException { BufferedReader obj= new BufferedReader new InputStreamReader(System.in)); int l, i, lc=0, uc=0, d=0, sp=0, lt=0, v=0, con=0, special=0, sum=0; char ch; String str=""; System.out.println("Enter a sentence " ); str=obj.readLine(); l=str.length(); for (i=0;i<l;i++) { ch=str.charAt(i); if(Character.isLowerCase(ch)) { lc++; } if(Character.isUpperCase(ch)) { uc++; } if(Character.isDigit(ch)) { d++; } if(ch==' ') { sp++; } if(Character.isLetter(ch)) { lt++; } if(ch == 'a' || ch== 'A' || ch== 'e' || ch== 'E' || ch== 'o' || ch== 'O' ||ch=='i' || ch== 'I'|| ch== 'u' ||ch== 'U') { v++; } } con=lt-v; sum=d+sp+v+con; special=l-sum; System.out.println("the number of lower case letters =" + lc); System.out.println("the number of upper case letters =" + uc); System.out.println("the number of digits =" + d); System.out.println("the number of spaces =" + sp); System.out.println("the number of letters =" + lt); System.out.println("the number of special characters =" + special); System.out.println("the number of vowels =" + v); System.out.println("the number of consonants =" + con); } } Variable Description Name Of Variable Data Type Description i int For loop str string To store the sentence entered by the user. l int To store the length of the string. ch char To store each of the characters of the string. lc int To store the number of characters in the string in lower case. uc int To store the number of characters in the string in upper case. d int To store the number of digits in the string. sp int To store the number of spaces in the string. lt int To store the number of letters in the string. v int To store the number of vowels in the string con int To store the number of consonants in the string. special int To store the number of special characters in the string. sum int To store the sum of numbers of letters including spaces. 2. Input the complete name of a person and display the initials, e.g. Billie Joe Armstrong as BJA. import java.io.*; public class prog32 { public static void main() throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); System.out.println("Enter a complete name"); String str=in.readLine(); int len,i; String temp="", abv=""; char ch; str=str+" "; len=str.length(); for(i=0;i<len;i++) { if(str.charAt(i)!=' ') { temp=temp+str.charAt(i); } else { ch=temp.charAt(0); abv=abv+ch; temp=""; } } System.out.println("Complete Name: "+str); System.out.println("Initials: "+abv); } } Variable Description Name Of Variable Data Type Description i int For loop str string To store the sentence entered by the user. len int To store the length of the string. ch char To store each of the characters of the string. abv string To store the initials of the complete name. len int To store the length of the string. 3. Input a sentence and reverse each word without reversing the sequence of the words. E.g. THREE DOORS DOWN as EERHT SROOD NWOD import java.io.*; public class prog33 { public static void main() throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); System.out.println("Enter a sentence"); String str=in.readLine(); int len, leng, i, j; String temp, rev, sub; temp=""; rev=""; sub=""; str=str+" "; len=str.length(); str=str.toUpperCase(); for(i=0;i<len;i++) { if(str.charAt(i)!=' ') { temp=temp+str.charAt(i); } else { leng=temp.length(); for(j=0;j<leng;j++) { rev=temp.charAt(j) + rev; } sub=sub + rev + " "; rev=""; temp=""; } } System.out.println(sub); } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop str string To store the sentence entered by the user. len int To store the length of the string. temp string To store the sentence in the string str. leng int To store the length of the string temp. rev string To store the reverse of each word the string. sub string To store the reverse of the string with spaces and in the same sequence of words. 4. Input a sentence and convert each word into a Piglatin. E.g. PUDDLE OF MUDD becomes UDDLEPAY OFAY UDDMAY import java.io.*; public class prog34 { public static void main() throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); System.out.println("Enter a sentence"); String str=in.readLine(); int len, leng, i, j; String temp, pig, sub; temp=""; sub=""; char ch; str=str+" "; len=str.length(); str=str.toUpperCase(); for(i=0;i<len;i++) { if(str.charAt(i)!=' ') { temp=temp+str.charAt(i); } else { leng=temp.length(); for(j=0;j<leng;j++) { ch=temp.charAt(j); if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { break; } } pig=temp.substring(j) + temp.substring(0,j) + "AY"; sub=sub + pig + " "; temp=""; } } System.out.println(sub); } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop str string To store the sentence entered by the user. len int To store the length of the string. ch char To store each of the characters of the string. temp string To store the sentence in the string str. leng int To store the length of the string temp. pig string To store the piglatin form of each word in the string. sub string To store the piglatin form of each word in the string in the same sequence of words. 5. Input a sentence and display only the Palindrome words, like NITIN ARORA PLAYS GUITAR, gives NITIN, ARORA import java.io.*; public class prog35 { public static void main() throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); System.out.println("Enter a sentence"); String str=in.readLine(); int len, i, j, leng; String temp, rev; temp=""; rev=""; str=str+" "; len=str.length(); str=str.toUpperCase(); for(i=0;i<len;i++) { if(str.charAt(i)!=' ') { temp=temp+str.charAt(i); } else { leng=temp.length(); for(j=0;j<leng;j++) { rev=temp.charAt(j) + rev; } if(rev.equals(temp)) { System.out.print(temp+", "); } rev=""; temp=""; } } } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop str string To store the sentence entered by the user. len int To store the length of the string. temp string To store the sentence in the string str. leng int To store the length of the string temp. rev string To store the reverse of each word in the string. 6. Input names of ten capital cities and arrange them in Lexicographic order and redisplay them. import java.io.*; public class prog36 { public static void main()throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); String str[]=new String[10]; int i, j; String t=""; System.out.println("Enter names of 10 capital cities"); for(i=0;i<10;i++) { str[i]=in.readLine(); } for(i=0;i<10;i++) { for(j=0;j<((10)-i-1);j++) { if(str[j].compareTo(str[j+1])>0) { t=str[j]; str[j]=str[j+1]; str[j+1]=t; } } } System.out.println("Names of cities in Lexicographical order are"); for(i=0;i<10;i++) { System.out.println(str[i]); } } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop str string Array variable to store the name of 10 cities. t string Variable used for swapping. 7. Input a sentence in any random case and capitalize the first and last letter of each word and convert the rest to lower case. import java.io.*; public class prog37 { public static void main() throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); System.out.println("Enter a sentence"); String str=in.readLine(); int len, leng, i, j; char ch; String temp, sub, sent; temp=""; sub=""; sent=""; str=str+" "; len=str.length(); str=str.toLowerCase(); for(i=0;i<len;i++) { if(str.charAt(i)!=' ') { temp=temp+str.charAt(i); } else { leng=temp.length(); for(j=0;j<leng;j++) { ch=temp.charAt(j); if(j==0 || j==(leng-1)) { ch=Character.toUpperCase(ch); } sub=sub + ch; } sent=sent + sub + " "; sub=""; temp=""; } } System.out.println(sent); } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop str string To store the sentence entered by the user. len int To store the length of the string. ch char To store each of the characters of the string. temp string To store the sentence in the string str. leng int To store the length of the string temp. sub string To store each word of the string with the first and last letter in upper case. sent string To store each word of the string with the first and last letter in upper case with spaces and in the same sequence of words. 8. Input a word and generate the following pattern. Input: MATHS Output: M MA MAT MATH MATHS import java.io.*; public class prog38 { public static void main() throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); System.out.println("Enter a word"); String str=in.readLine(); int len, i, j; len=str.length(); for(i=0;i<len;i++) { for(j=0;j<=i;j++) { System.out.print(str.charAt(j)); } System.out.println(); } } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop str string To store the word entered by the user. len int To store the length of the string. 9. Write a program in Java to accept a line of text from the user and create a new word formed out of the first letter of each word. After creating the new word reverse it and check whether it is a Palindrome or not. E.g. If the user enters: Mangoes Are Delivered After Midnight Output: MADAM Reverse: MADAM Yes, it is a Palindrome E.g. If the user enters: A Brick In The Wall Output: ABITW Reverse: WTIBA No, it is not a Palindrome import java.io.*; public class prog39 { public static void main() throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); System.out.println("Enter a sentence"); String str=in.readLine(); int len, i, j, leng; String temp, rev, sub; temp=""; rev=""; sub=""; str=str+" "; len=str.length(); str=str.toUpperCase(); for(i=0;i<len;i++) { if(str.charAt(i)!=' ') { temp=temp+str.charAt(i); } else { sub=sub+temp.charAt(0); temp=""; } } leng=sub.length(); for(j=0;j<leng;j++) { rev=sub.charAt(j) + rev; } System.out.println("The word formed out of the first letter of each word: "+sub); System.out.println("Reverse: "+rev); if(sub.equals(rev)) { System.out.println("Yes, it is a Palindrome"); } else { System.out.println("No, it is not a Palindrome"); } } } Variable Description Name Of Variable Data Type Description i int For loop j int For loop str string To store the sentence entered by the user. len int To store the length of the string. temp string To store the sentence in the string str. leng int To store the length of the string temp. sub string To store the word formed out of the first letter of each word in the string. rev string To store the reverse of the new word formed. 10. Input a string along with an encoding factor and write a cryptography program code to encrypt the data. E.g. Input word is “APPLE” and Encoding factor is 3 then each letter gets encoded by a factor of 3 by adding it to its ASCII equivalent resulting in “DSSOH”. import java.io.*; class prog40 { public static void main()throws IOException { BufferedReader obj=new BufferedReader(new InputStreamReader(System.in)); String s="",t=""; System.out.println("Enter a word"); s=obj.readLine(); int n; System.out.println("Enter the encoding factor"); n=Integer.parseInt(obj.readLine()); int l, i, d, e; l=s.length(); char f, c; for(i=0;i<l;i++) { c=s.charAt(i); d=(char)c; e=d+n; f=(char)e; t=t+f; } System.out.println(t); } } Variable Description Name Of Variable Data Type Description i int For loop s string To store the word entered by the user. l int To store the length of the string. n int To store the encoding factor entered by the user. c char To store each character in the word entered. d int e int
Copyright © 2024 DOKUMEN.SITE Inc.