Académique Documents
Professionnel Documents
Culture Documents
Ques1:- PROGRAM TO FIND THE LOWEST EVEN AND HIGHEST ODD? // PROGRAM TO FIND THE LOWEST EVEN AND HIGHEST ODD import java.io.*; import java.lang.*; class prg28 {public void q28()throws IOException {BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int n=0,i=0,ho=0,le=0; for(i=1;i<=5;i++) { System.out.println("\n Enter any no="); n=Integer.parseInt(input.readLine()); if(n%2==0) { if((le==0)||(le>n)) le=n; } else { if(ho<n) ho=n; } } System.out.println("\n Lowest even no=" +le); System.out.println("\n Highest odd no=" +ho); } } Variable name type use Le int To store number ho int To store number n int To store number i int To store number
Ques2:- program to merge two array into third array? // program to merge two array into third array import java.io.*; import java.lang.*; class prg27 { public void q27()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int i=0,j=0,ar[]=new int[4],br[]=new int[6],cr[]=new int[10],c=0;
System.out.println("\n Enter elements for first array"); for(i=0;i<4;i++) { System.out.println("\n Enter the numbers="); ar[i]=Integer.parseInt(input.readLine()); cr[c]=ar[i]; c++; } System.out.println("\n Enter elements for second array"); for(i=0;i<6;i++) { System.out.println("\n Enter the numbers="); br[i]=Integer.parseInt(input.readLine()); cr[c]=br[i]; c++; } System.out.println("\n Elements for first array"); for(i=0;i<4;i++) System.out.print(+ar[i]+" "); System.out.println(); System.out.println("\n Elements for second array"); for(i=0;i<6;i++) System.out.print(+br[i]+" "); System.out.println(); System.out.println("\n Elements for Merge array"); for(i=0;i<10;i++) System.out.print(+cr[i]+" "); System.out.println(); System.out.println("\n\n Elements after removing duplicates"); for(i=0;i<10;i++) { c=0; for(j=0;j<10;j++) { if(cr[i]!=cr[j]) c++; if((cr[i]==cr[j]) && (i>j)) break; } if(c==9) System.out.print(+cr[i]+" ");
} System.out.println("\n\n All duplicates elements"); for(i=0;i<10;i++) { for(j=0;j<10;j++) { if((cr[i]==cr[j]) && (i<j)) System.out.print(+cr[i]+" "); } } } } Variable name Type use i int To store number j int To store number Ar[] int To store number Br[] int To store number Cr[] int To store number c int To store number Ques3:-program to search by binary search? // program to search by binary search import java.io.*; import java.lang.*; class prg26 { public void q26() throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int num[]=new int[10],i=0,j=0,t=0,n=0,p=0,m=0,f=0,l=9; for(i=0;i<10;i++) { System.out.println("\n Enter element ="); num[i]=Integer.parseInt(input.readLine()); } // program for ascending sort for(i=0;i<10;i++) { for(j=0;j<9;j++) { if(num[j]>num[j+1]) { t=num[j]; num[j]=num[j+1]; num[j+1]=t; } }
} for(i=0;i<10;i++) System.out.print(+num[i]+" "); System.out.println("\n\n Enter element to search="); n=Integer.parseInt(input.readLine()); while(f<=l) { m=(f+l)/2; if(num[m]==n) p=m+1; if(num[m]<n) f=m+1; else l=m-1; } if(p==0) System.out.println("\n search unsuccessful"); else { System.out.println("\n search successful"); System.out.println("\n Element found in "+p+ " position"); } } } Variable name num[] t n p m f l type int int int int int int int use To store value To store value To store value To store value To store value To store value To store value
Ques4:- program to return statement? // program to return statement import java.io.*; import java.lang.*; class work { public int a,b; double c;
work() { a=0; b=0; c=0.0; } double add(int a,int b) { c=(double)a+b; return c; } } class prg25 { public void q25()throws IOException { work obwork=new work(); BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int x=0,y=0; double sum=0.0; System.out.println("Enter first value ="); x=Integer.parseInt(input.readLine()); System.out.println("Enter second value ="); y=Integer.parseInt(input.readLine()); sum=obwork.add(x,y); System.out.println("Sum of two numbers =" +sum); } } Variable name type use a int To store value b int To store value c int To store value Ques5:- program to use constructor with a class to calculate netpay? // program to use constructor with a class to calculate netpay import java.io.*; import java.lang.*; class employee { public double bpay; employee(double bp) // use of construtor { bpay=bp; }
void calpay() { double da=0.0,hra=0.0,pf=0.0,npay=0.0,gpay=0.0; da=.25*bpay; hra=.15*bpay; pf=.0833*bpay; npay=bpay+da+hra; gpay=npay-pf; System.out.println("Emp BASIC pay =" +bpay); System.out.println("Emp GROSS pay =" +gpay); } public void main()throws IOException { employee oemp=new employee(10000.00); oemp.calpay(); } } Vaiable naame da hra pf npay gpay Type double double double double double use To store value To store value To store value To store value To store value
Ques6:-program to use bubble sort to print merit list? // program to use bubble sort to print merit list import java.io.*; import java.lang.*; class prg22 { public void q22()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int i=0,j=0,ar[]=new int[10],temp=0; String stname[]=new String[10],stemp=" "; for(i=0;i<10;i++) { System.out.println("\n Enter the name of student ="); stname[i]=input.readLine(); System.out.println("\n Enter the marks in computers="); ar[i]=Integer.parseInt(input.readLine()); } for(i=0;i<10;i++)
} System.out.println("\n\t\t\tMerit List"); System.out.println("\n\t\t\t**********"); System.out.println("\n S.No\t\t Name of Student \t\t Marks"); for(i=0;i<10;i++) { System.out.println("\n"+(i+1)+"\t\t" +stname[i]+"\t\t"+ar[i]); } } } Variable name i ar[]= temp stname[] stemp j type int string string string string int use For loop To store value To store value To store value To store value For loop
Ques7:-program to print the required format? // program to print the required format class prg21 { public void q21() { int i=1,j=0,d=0,t=0; System.out.print(i+"\n"); for(i=2;i<=5;i++) { System.out.print(+i+" "); d=4; t=i+d; for(j=1;j<i;j++)
} System.out.println(); } } } Variable name i j d t Function Recursive function Ques8:-pogram to use recursive function to print odd numbers // pogram to use recursive function to print odd numbers import java.io.*; import java.lang.*; class rec1 { public static void main() { rec1 ob1=new rec1(); System.out.println("Printing of even numbers"); ob1.rfodd(2); } void rfodd(int n) // defining of recursive function { if(n<10) { System.out.println(n+"\t"); rfodd(n+2); // calling of function type int int int int use To store value To store value To store value To store value
Ques9:-Program to use recursive function to print factorial of a numbers? import java.io.*; import java.lang.*; class rec2 { public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int num=0,i=0,f=0; rec2 ob2=new rec2(); System.out.println("Enter number ="); num=Integer.parseInt(br.readLine()); for(i=1;i<=num;i++) f=ob2.fact(i); System.out.println("factorial of "+num+"="+f); } int fact(int n) // defining of recursive function { if(n==0) return 1; else n*=fact(n-1); return n; } } Varible name num i f Type int int int Use To store number Foor loop To store number
Ques10:-program to use recursive function to print factorial of a numbers? //pogram to use recursive function to print factorial of a numbers import java.io.*; import java.lang.*; class rec3 { public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); rec3 ob=new rec3(); int i=0,term=0; System.out.println("Fibbonacci series =>");
for(i=0;i<10;i++) { term=ob.fib(i); System.out.print(term+" "); } } int fib(int n) { if(n==0) return 0; else if(n==1) return 1; else return fib(n-1)+fib(n-2); } }
Ques11:-program to use recursive function to print frequency of vowels? //pogram to use recursive function to print frequency of vowels import java.io.*; import java.lang.*; class rec4 { public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); rec4 ob=new rec4(); String st=" "; int f=0; System.out.println("Enter string =>"); st=br.readLine(); f=ob.vcount(st); System.out.println("Total frequency "+f); } int vcount(String s) { if(s.length()==0) return 0;
10
char ch=s.charAt(0); if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') return (1+vcount(s.substring(1))); else return vcount(s.substring(1)); } } Variable name st f type String int Use To store name To store number
Ques12:-program to use recursive function to print string in reverse? //pogram to use recursive function to print string in reverse import java.io.*; import java.lang.*; class rec5 { public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); rec5 ob=new rec5(); String st=" "; System.out.println("Enter string =>"); st=br.readLine(); System.out.println("Reverse string =>"); ob.rev(st); } void rev(String s) { if(s.length()==0) return; char ch=s.charAt(0); rev(s.substring(1)); System.out.print(ch); } } Variable name Type Use s String To store valueof name
11
Ques13:-program to use recurssive for binary search? // program to use recurssive for binary search import java.io.*; import java.lang.*; class rec6 { public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); rec6 ob=new rec6(); int res=0,ar[]=new int[5],i=0,low=0,hg=4,t=0; for(i=0;i<5;i++) { System.out.println("Enter number =>"); ar[i]=Integer.parseInt(br.readLine()); } System.out.println("Enter searching number =>"); t=Integer.parseInt(br.readLine()); res=ob.bin(ar,low,hg,t); if(res==1) System.out.println("Number is found"); else System.out.println("Number is not found"); } int bin(int a[],int lower,int high,int sr) { int mid=0; if(lower>high) return 0; else { mid=(lower+high)/2; if(sr>a[mid]) return bin(a,mid+1,high,sr); else if(sr<a[mid]) return bin(a,lower,mid-1,sr); else return 1; } } }
12
Use To store value To store no. For loop To store value of low no. To store value of hig. num To store value
Ques14:-program to use recursive method for printing of even numbers upto 10? class mrec5 { public void main() { prne(2); } void prne(int n) { if(n<10) { System.out.print(+n +" "); prne(n+2); } else System.out.print(+n+ " "); } } Variable name n type int use To store value
Ques15:-program to use recursive method for printing of ODD numbers upto 9? class mrec6 { void prno(int n) { if(n<9) { System.out.print(+n +" "); prno(n+2); } else System.out.print(+n+ " "); } public void main() { prno(1); }
13
} Variable name n
type int
Ques16:-program to use recursive method for printing of natural number upto 10? class mrec3 { void prn(int n) { if(n<=10) { System.out.print(+n +" "); prn(n+1); } } public void main() { prn(1); }
Variable name n
type int
Function overloading program Ques17:-to find out area of square,rectangle and circle? // Function overloading program //to find out area of square,rectangle and circle import java.io.*; import java.lang.*; class area { public int s1,s2,s3; double r,ar; area() { s1=0; s2=0; r=0.0; ar=0.0; } double area(int s1) { ar=(double)s1*s1; return ar; }
14
double area(int s1,int s2) { ar=(double)s1*s2; return ar; } double area(double r) { ar=3.142*r*r; return ar; } double area(int s1,int s2,int s3) { r=(s1+s2+s3)/2; ar=Math.sqrt(r*(r-s1)*(r-s2)*(r-s3)); return ar; } public void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); area obar=new area(); int a1,a2,a3; double r1,ar1; int op; System.out.println("1.Area of Square"); System.out.println("2.Area of Rectangle"); System.out.println("3.Area of Circle"); System.out.println("4.Area of Triangle"); System.out.println("5.Exit"); System.out.println("Enter your option="); op=Integer.parseInt(input.readLine()); switch(op) { case 1: // program for square System.out.println("Enter side for square="); a1=Integer.parseInt(input.readLine()); ar1=obar.area(a1); System.out.println("Area of square=" +ar1); break; case 2: // program for rectangle System.out.println("Enter the sides for rectangle="); a1=Integer.parseInt(input.readLine()); a2=Integer.parseInt(input.readLine());
15
ar1=obar.area(a1,a2); System.out.println("Area of rectangle=" +ar1); break; case 3: // program for circle System.out.println("Enter the radius of circle="); r1=Double.parseDouble(input.readLine()); ar1=obar.area(r1); System.out.println("Area of Circle=" +ar1); break; case 4: // program for triangle System.out.println("Enter the first side of triangle ="); a1=Integer.parseInt(input.readLine()); System.out.println("Enter the second side of triangle ="); a2=Integer.parseInt(input.readLine()); System.out.println("Enter the third side of triangle ="); a3=Integer.parseInt(input.readLine()); ar1=obar.area(a1,a2,a3); System.out.println("Area of Triangle=" +ar1); break; case 5: break; } } } Varible name S1 S2 S3 r ar Type int int int int int Use To store value To store value To store value To store value To store value
Ques18:-to find out area of square,rectangle and circle? // Function overloading program //to find out area of square,rectangle and circle import java.io.*; import java.lang.*; class q6 { double area(int s1) { double ar;
16
ar=(double)s1*s1; return ar; } double area(int s1,int s2) { double ar; ar=(double)s1*s2; return ar; } double area(double s1) { double ar; ar=3.142*s1*s1; return ar; } public void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int a1,a2; double r1,ar1; int op; System.out.println("1.Area of Square"); System.out.println("2.Area of Rectangle"); System.out.println("3.Area of Circle"); System.out.println("4.Exit"); System.out.println("Enter your option="); op=Integer.parseInt(input.readLine()); switch(op) { case 1: // program for square System.out.println("Enter side for square="); a1=Integer.parseInt(input.readLine()); ar1=area(a1); System.out.println("Area of square=" +ar1); break; case 2: // program for rectangle System.out.println("Enter the sides for rectangle="); a1=Integer.parseInt(input.readLine()); a2=Integer.parseInt(input.readLine()); ar1=area(a1,a2); System.out.println("Area of rectangle=" +ar1);
17
break; case 3: // program for circle System.out.println("Enter the radius of circle="); r1=Double.parseDouble(input.readLine()); ar1=area(r1); System.out.println("Area of Circle=" +ar1); break; case 4: break; } } } Variable name S1 ar Type int int use To store value To store value
Ques19:-menu driven program using class? // menu driven program using class import java.io.*; class num { public int no; num() //const { no=0; } void fact(int no) {int f=1,i=0; for(i=1;i<=no;i++) f*=i; System.out.println("Factorial of "+no+" = "+f); } public void main()throws IOException {BufferedReader input= new BufferedReader(new InputStreamReader(System.in)); num obnum=new num(); int op,n; System.out.println("1.Factorial of no"); System.out.println("2.Primr of no"); System.out.println("3.Perfect of no"); System.out.println("3.Enter option"); op=Integer.parseInt(input.readLine()); switch(op)
18
case 1: // for fact System.out.println("Enter any number="); n=Integer.parseInt(input.readLine()); obnum.fact(n); break; case 2: // for prime
break; case 3: // for perfect break; } } } Variable name no f i type int int int use To store value To store value For loop
conversion Ques20:-program to make a function dtob() to convert decimal to binary? // program to make a function dtob() to convert decimal to binary class q10 { void dtob(int num) { int r=0,b=0,c=0; System.out.println("Decimal number is ="+num); while(num>0) { r=num%2; b+=r*Math.pow(10,c); c++; num/=2; } System.out.println("Binary number is ="+b); } } Varible name type use r int To store value b int To store value c int To store value Ques21:- program to make a function btod() to convert binary to decimal?
19
class q11 { void btod(int b) { int r=0,d=0,c=0; System.out.println("Binary number is ="+b); while(b>0) { r=b%10; d+=r*Math.pow(2,c); c++; b/=10; } System.out.println(" Decimal number is="+d); } } Varible name type use r int To store value b int To store value c int To store value Ques22:-program to find the hcf of three numbers? // program to find the hcf of three numbers import java.io.*; import java.lang.*; class q12 { void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int a=0,b=0,c=0,p=0,i=0,h=0; System.out.println("Enter any three numbers="); a=Integer.parseInt(input.readLine()); b=Integer.parseInt(input.readLine()); c=Integer.parseInt(input.readLine()); p=a*b*c; for(i=1;i<=p;i++) { if(a%i==0 && b%i==0 && c%i==0) h=i; } System.out.println("HCF ="+h); } }
20
Variable name a b c p i
use To store value To store value To store value To store value For loop
Ques23:-program to find out the angle? import java.io.*; import java.lang.*; class q13 { void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int a=0,b=0,c=0,p=0,i=0,h=0; System.out.println("Enter any measurement ="); a=Integer.parseInt(input.readLine()); if(a<90) System.out.println("Acute angle"); else if(a==90) System.out.println("Right angle"); else if(a<180) System.out.println("Obtuse angle"); else if(a==180) System.out.println("Straight angle"); else if(a<360) System.out.println("Reflex angle"); else System.out.println("Complete angle"); } } Variable name Type Use a int To store value b int To store value c int To store value p int To store value i int To store value h int To store value
21
Ques24:-program to merge two array into third array? import java.io.*; import java.lang.*; class prg27 { public void q27()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int i=0,j=0,ar[]=new int[4],br[]=new int[6],cr[]=new int[10],c=0; System.out.println("\n Enter elements for first array"); for(i=0;i<4;i++) { System.out.println("\n Enter the numbers="); ar[i]=Integer.parseInt(input.readLine()); cr[c]=ar[i]; c++; } System.out.println("\n Enter elements for second array"); for(i=0;i<6;i++) { System.out.println("\n Enter the numbers="); br[i]=Integer.parseInt(input.readLine()); cr[c]=br[i]; c++; } System.out.println("\n Elements for first array"); for(i=0;i<4;i++) System.out.print(+ar[i]+" "); System.out.println(); System.out.println("\n Elements for second array"); for(i=0;i<6;i++) System.out.print(+br[i]+" "); System.out.println(); System.out.println("\n Elements for Merge array"); for(i=0;i<10;i++) System.out.print(+cr[i]+" "); System.out.println(); System.out.println("\n\n Elements after removing duplicates"); for(i=0;i<10;i++) { c=0; for(j=0;j<10;j++)
22
} if(c==9) System.out.print(+cr[i]+" "); } System.out.println("\n\n All duplicates elements"); for(i=0;i<10;i++) { for(j=0;j<10;j++) { if((cr[i]==cr[j]) && (i<j)) System.out.print(+cr[i]+" "); } } } } Variable name i j Ar[] Br[] Cr[] c
use To store value To store value To store value To store value To store value To store value
Ques25:-PROGRAM TO FIND THE LOWEST EVEN AND HIGHEST ODD? import java.io.*; import java.lang.*; class prg28 {public void q28()throws IOException {BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int n=0,i=0,ho=0,le=0; for(i=1;i<=5;i++) { System.out.println("\n Enter any no="); n=Integer.parseInt(input.readLine()); if(n%2==0) { if((le==0)||(le>n)) le=n; }
23
else { }
if(ho<n) ho=n;
} System.out.println("\n Lowest even no=" +le); System.out.println("\n Highest odd no=" +ho); } } Variable name n i ho le
Strings Ques26:-program to declare the function pallin()? // program to declare the function pallin() import java.io.*; import java.lang.*; class q8 { void pallin(String st1) { int i=0,len=0,pos,c=0; char ch; len=st1.length(); for(i=0;i<len;i++) { ch=st1.charAt(len-(i+1)); if(st1.charAt(i)==ch) c++; } if(c!=len) System.out.println("String is not Pallindrome"); else System.out.println("String is Pallindrome"); } void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); String st;
24
System.out.println("Enter any string="); st=input.readLine(); pallin(st); } } Variable name i len pos c ch type int int int int char use To store value To store value To store value To store value To store value
Ques:-program to print TOTAL VOWELS? // program to print TOTAL VOWELS class st14 { public void q14(String st) { int len=0,i=0,v=0; char ch; System.out.println("Original string ="+st); len=st.length(); for(i=0;i<len;i++) { ch=st.charAt(i); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u' ) v++; } System.out.println("Total vowels ="+v); } } Ques27:-program to print welcome in format? // program to print welcome in format class St4 {public void q4() { String wd="WELCOME"; int len=0,i=0,c=0,sp=0; System.out.println("\n The word is ="+wd+"\n\n"); len=wd.length(); System.out.println(); sp=len;
25
for(i=0;i<len;i++) { System.out.print(wd.substring(0,sp)); sp--; for(c=1;c<=((2*i)+1);c++) System.out.print(" "); System.out.print(wd.substring(i,len)); System.out.println(); } sp=2; for(i=1;i<len;i++) { System.out.print(wd.substring(0,sp)); for(c=11;c>=((2*i)-1);c--) System.out.print(" "); System.out.print(wd.substring((len-sp),len)); sp++; System.out.println(); } } }
Ques28:-program to print different printing format using poly? // program to print different printing format using poly import java.io.*; import java.lang.*; class draw{public int a,b,i,j; char ch; BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); draw() { a=0; b=0; i=0; j=0;
26
ch=' '; } void dr() { System.out.println(); for(i=1;i<=5;i++) System.out.print("*"); } void dr(char ch,int a) { System.out.println(); for(i=1;i<=a;i++) System.out.print(ch+" "); } void dr(char ch,int a,int b) { System.out.println(); for(i=1;i<=a;i++) { for(j=1;j<=b;j++) System.out.print(ch+""); System.out.println(); } } // begining of main function public void main()throws IOException int op=0; int n=0,r=0,c=0; char dh; draw d1=new draw(); System.out.println("\n\t\t\tMain Menu"); System.out.println("1. Five Star"); System.out.println("2. Char till a number"); System.out.println("3. Char in rectangle form"); System.out.println("\nEnter your option (1-3)="); op=Integer.parseInt(input.readLine()); switch(op) { case 1: // to print five stars d1.dr(); break; case 2: // char in a row System.out.println("\nEnter a number=");
27
n=Integer.parseInt(input.readLine()); System.out.println("\nEnter character="); dh=(char)(input.read()); d1.dr(dh,n); break; case 3: // char in rec form System.out.println("\nEnter a row number="); r=Integer.parseInt(input.readLine()); System.out.println("\nEnter a col number="); c=Integer.parseInt(input.readLine()); System.out.println("\nEnter character="); dh=(char)input.read(); d1.dr(dh,r,c); break; } } } Variable name type use a int To store value b int To store value i int To store value j int To store value ch char To store value Ques29:-PROGRAM TO PRINT INITIAL OF A NAME // PROGRAM TO PRINT INITIAL OF A NAME class St2 { public void q2() { String sen="MOHANDAS KARAMCHAND GANDHI"; char ch; int len=0,i=0,p=0; len=sen.length(); System.out.println("\nThe Name is =" +sen+"\n"); System.out.print("The Required Initial of the name ="); System.out.print(sen.charAt(0)); for(i=1;i<len;i++) { ch=sen.charAt(i); if(ch==' ') { System.out.print("."+sen.charAt(i+1)); p=i+2;
28
} if(i==(len-1)) System.out.print(sen.substring(p,len)); } } } Ques:-program to input country and capital to search for a capital? import java.io.*; import java.lang.*; class St7 { public void Q7()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in));String wd="India"; String co[]=new String[5], cp[]=new String[5], sco; int i=0,c=0; for(i=0;i<5;i++) { System.out.println("Enter the country name="); co[i]=input.readLine(); System.out.println("Enter the capital name="); cp[i]=input.readLine(); } System.out.println("Enter the country name for its capital="); sco=input.readLine(); for(i=0;i<5;i++) { if(sco.equals(co[i])==true) { System.out.println("\n The searched country name=" +sco); System.out.println("\n And its capital name=" +cp[i]); c++; } } if(c==0) System.out.println("\n The searched is not successfull"); } Variable name Type Use co[] int To store value Cp[] int To store value sco int To store value i int To store value c int To store value
29
Inheritance Ques30:-program to use inheritance? // program to use inheritance import java.io.*; import java.lang.*; class emp { private String name; double bpay; emp(String na,double bp) { this.name=na; this.bpay=bp; } void display() { System.out.println("Name of Employee " +name); System.out.println("Basic pay of Employee "+bpay); } } class tax extends emp { public int rate,hr; double tot; tax(String na, double bp,int rt,int h) { super(na,bp); this.rate=rt; this.hr=h; } void cal() { tot=bpay +rate *hr; super.display(); System.out.println("Total rate per hour ="+rate); System.out.println("Total hour of working ="+hr); System.out.println("Total pay of employee ="+tot); } public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String n; double bap; int hour,ra;
30
System.out.println("Enter name , basic pay , total rate per hour and total number of hours ="); n=br.readLine(); bap=Double.parseDouble(br.readLine()); ra=Integer.parseInt(br.readLine()); hour=Integer.parseInt(br.readLine()); tax obj=new tax(n,bap,ra,hour); obj.cal(); } } Variable name Type Use name string To store value bpay int To store value Ques31:-program to use inheritance? package jpro; // program to use inheritance import java.io.*; import java.lang.*; class emp { private String name; double bpay; emp(String na,double bp) { this.name=na; this.bpay=bp; } void display() { System.out.println("Name of Employee " +name); System.out.println("Basic pay of Employee "+bpay); } } class tax extends emp { public int rate,hr, bp; double tot; tax(String na, double bp,int rt,int h) { super(na,bp); this.rate=rt; this.hr=h; }
31
void cal() { tot=bp +rate *hr; System.out.println("Total pay of employee ="+tot); } public static void main() { tax obj=new tax("Amit",5000,200,20); obj.cal(); } } Variable name name bpay Type string int Use To store value To store value
Ques32:-prg to show inheritance between two classes EMP AND INHEMP? // prg to show inheritance between two classes EMP AND INHEMP import java.io.*; import java.lang.*; class emp { private int ecode; String ename; double esal; BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); emp() { ecode=0; ename=" "; esal=0.0; } void indata()throws IOException { System.out.println("\n Enter Employee Code ="); ecode=Integer.parseInt(input.readLine()); System.out.println("\n Enter Employee Name ="); ename=input.readLine(); System.out.println("\n Enter Employee Basic Salary ="); esal=Double.parseDouble(input.readLine()); } void disdata() { System.out.println("\n\t\tEmployee Details"); System.out.println("\n Employee Code =" +ecode);
32
System.out.println("\n Employee Name =" +ename); System.out.println("\n Employee Basic Salary =" +esal); } } class inhemp extends emp { public double da,hra,npay; inhemp() { da=0.0; hra=0.0; npay=0.0; } void in()throws IOException { indata(); } void cal() { da=.57*esal; hra=.12*esal; npay=esal+da+hra; } void dis() { disdata(); System.out.println("\n Employee D.A =" +da); System.out.println("\n Employee H.R.A =" +hra); System.out.println("\n Employee Netpay =" +npay); } public void main()throws IOException { inhemp obemp=new inhemp(); obemp.in(); obemp.cal(); obemp.dis(); } } Variable name Type Ename ecode string int
33
Ques33:-program to use selection sort to print in descending? import java.io.*; class q19 { public void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int num[]=new int[5],i=0,j=0,tm=0,sl=0; //input loop for(i=0;i<5;i++) { System.out.println("\n Enter the value="); num[i]=Integer.parseInt(input.readLine()); } // sorting loop for(i=0;i<4;i++) { sl=i; for(j=i+1;j<5;j++) { if(num[sl]<num[j]) sl=j; } tm=num[i]; num[i]=num[sl]; num[sl]=tm; } System.out.println("List in Descending order"); for(i=0;i<5;i++) System.out.println(num[i]); } } Variable name type use num[] int To store value i int To store value j int To store value tm int To store value sl int To store value
34
Ques34:-program to use bubble sort to print merit list? // program to use bubble sort to print merit list import java.io.*; class q18 { public void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int num[]=new int[5],i=0,j=0,tm=0; String name[]=new String[10],st=" "; //input loop for(i=0;i<5;i++) { System.out.println("\n Enter the name of student ="); name[i]=input.readLine(); System.out.println("\n Enter the value="); num[i]=Integer.parseInt(input.readLine()); } // sorting loop for(i=0;i<4;i++) { for(j=i+1;j<5;j++) { if(num[i]<num[j]) { st=name[i]; tm=num[i]; name[i]=name[j]; num[i]=num[j]; name[j]=st; num[j]=tm; } } } System.out.println("\t\tMerit list"); System.out.println("S.No\t\tStudent Name \t\tMarks"); for(i=0;i<5;i++) System.out.println(+(i+1)+"\t\t"+name[i]+"\t\t"+num[i]); } } Variable name type use num[] int To store value i int To store value
35
j tm name st
Ques35:-program to search by binary search? // program to search by binary search import java.io.*; import java.lang.*; class prg26 { public void q26() throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int num[]=new int[10],i=0,j=0,t=0,n=0,p=0,m=0,f=0,l=9; for(i=0;i<10;i++) { System.out.println("\n Enter element ="); num[i]=Integer.parseInt(input.readLine()); } // program for ascending sort for(i=0;i<10;i++) { for(j=0;j<9;j++) { if(num[j]>num[j+1]) { t=num[j]; num[j]=num[j+1]; num[j+1]=t; } } } for(i=0;i<10;i++) System.out.print(+num[i]+" "); System.out.println("\n\n Enter element to search="); n=Integer.parseInt(input.readLine()); while(f<=l) { m=(f+l)/2; if(num[m]==n) p=m+1; if(num[m]<n) f=m+1;
36
else l=m-1; } if(p==0) System.out.println("\n search unsuccessful"); else { System.out.println("\n search successful"); System.out.println("\n Element found in "+p+ " position"); } } } Variable name num[]; t n p m f l type int int int int int int int use To store value To store value To store value To store value To store value To store value To store value
Ques36:-program to sort the string alphabetically? // program to sort the string alphabetically import java.io.*; import java.lang.*; class St9 { public void Q9() throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); String st=" "; int i=0,j=0,len=0; System.out.println("Enter any name without space="); st=input.readLine(); len=st.length(); int a[]=new int[len],te; char c[]=new char[len],t=' '; System.out.println("Original name is given below"); System.out.println(st); for(i=0;i<len;i++) { a[i]=st.charAt(i); c[i]=st.charAt(i);
37
} for(i=0;i<len;i++) { for(j=0;j<len-1;j++) { if(a[j]>a[j+1]) { te=a[j]; t=c[j]; a[j]=a[j+1]; c[j]=c[j+1]; a[j+1]=te; c[j+1]=t; } } } System.out.println("Sorted name is given below"); for(i=0;i<len;i++) System.out.print(c[i]); } } Variable name st i j len Type string int int int use To store value For loop For loop To store value
Ques37:-program to sort the string alphabetically? // program to sort the string alphabetically import java.io.*; import java.lang.*; class St9 { public void Q9() throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); String st=" "; int i=0,j=0,len=0; System.out.println("Enter any name without space="); st=input.readLine(); len=st.length(); int a[]=new int[len],te; char c[]=new char[len],t=' '; System.out.println("Original name is given below");
38
System.out.println(st); for(i=0;i<len;i++) { a[i]=st.charAt(i); c[i]=st.charAt(i); } for(i=0;i<len;i++) { for(j=0;j<len-1;j++) { if(a[j]>a[j+1]) { te=a[j]; t=c[j]; a[j]=a[j+1]; c[j]=c[j+1]; a[j+1]=te; c[j+1]=t; } } } System.out.println("Sorted name is given below"); for(i=0;i<len;i++) System.out.print(c[i]); } } Variable name st i j len Type string int int int use To store value For loop For loop To store value
Ques38:-program to convert decimal to bin no? class prg19 { public void q19(int dn) { int r=0,p=0,bn=0; System.out.println("\n Decimal number ="+ dn); while(dn>0) { r=dn%2; bn+=Math.pow(10,p)*r; p++; dn/=2;
39
}; System.out.println("\n Binary no =" +bn); } } Variable name r p bn type int int int use To store value To store value To store value
Ques39:-program to find hcf? class prg18 { public void q18(int a,int b) { int r=0; System.out.println("\n Number are =" +a +" and " + b); r=a%b; while(r>0) { a=b; b=r; r=a%b; }; System.out.println("\n h.c.f=" +b); } } Variable name type use a int To store value b int To store value r int To store value Ques40:-program to use selection sort to print in descending? // program to use selection sort to print in descending import java.io.*; class q19 { public void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int num[]=new int[5],i=0,j=0,tm=0,sl=0; //input loop for(i=0;i<5;i++)
40
} // sorting loop for(i=0;i<4;i++) { sl=i; for(j=i+1;j<5;j++) { if(num[sl]<num[j]) sl=j; } tm=num[i]; num[i]=num[sl]; num[sl]=tm; } System.out.println("List in Descending order"); for(i=0;i<5;i++) System.out.println(num[i]); } } Variable name Num[] i tl j sl type int int int int int use To store value For loop To store value For loop To store value
Ques41:-program to find frequency of odd & even no? // program to find frequency of odd & even no import java.io.*; import java.lang.*; class prg15 {public void q15()throws IOException {BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int n=0,i=0,o=0,e=0; for(i=1;i<=5;i++) { System.out.println("\n Enter any no="); n=Integer.parseInt(input.readLine()); if(n%2==0) e++;
41
else o++; } System.out.println("\n Total even no=" +e); System.out.println("\n Total odd no=" +o); } } Variable name n i o e type int int int int use To store value To store value To store value To store value
Ques42:- program to find sum of -ve & sum of +ve odd no , even no? // program to find sum of -ve & sum of +ve odd no , even no import java.io.*; import java.lang.*; class prg16 {public void q16(int t)throws IOException {BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int n=0,i=0,sng=0,po=0,pe=0; for(i=1;i<=t;i++) { System.out.println("\n Enter any no="); n=Integer.parseInt(input.readLine()); if(n<0) sng+=n; if(n>0) { if(n%2==0) pe+=n; else po+=n; } if(n==0) break; } System.out.println("\n Total sum of negative no=" +sng);
42
System.out.println("\n Total sum of odd no=" +po); System.out.println("\n Total sum of even no=" +pe); } } Variable name n i sng po pe type int int int int int use To store value To store value To store value To store value To store value
Ques43:-program to check s number to be perfect or not? // program to check s number to be perfect or not class prg17 { public void q17(int n) { int i=0,r=0,s=0; for(i=1;i<n;i++) { r=n%i; if(r==0) s+=i; } if(s==n) System.out.println("\n No is perfect"); else System.out.println("\n No is not perfect"); } } Variable name type use i int For loop r int To store value c int To store value Ques44:- program to print the format Aa, Bb Cc, Dd Ee Ff? // program to print the format Aa, Bb Cc, Dd Ee Ff class q15 { public void main() { int i=0,j=0,a=65,b=97; char ch1,ch2; for(i=1;i<=3;i++)
43
} } } Variable name i a b type int int int use For loop To store value To store value
Ques45:- program to use menu driven on mathematical operation? //program to use menu driven on mathematical operation import java.io.*; import java.lang.*; class prg11 { public void q11()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int a=0,b=0,op=0; System.out.println("\n\t\t\t Main Menu"); System.out.println("\n\t 1. Addition"); System.out.println("\n\t 2. Subtraction"); System.out.println("\n\t 3. Exit"); System.out.println("\n\t Enter your option ( 1-3)="); op= Integer.parseInt(input.readLine()); switch(op) { case 1: // program to add System.out.println("Enter first value ="); a=Integer.parseInt(input.readLine()); System.out.println("Enter second value ="); b=Integer.parseInt(input.readLine()); System.out.println("\n Sum of two Numbers =" +(a+b)); break; case 2: // program to subtract System.out.println("Enter first value =");
44
a=Integer.parseInt(input.readLine()); System.out.println("Enter second value ="); b=Integer.parseInt(input.readLine()); System.out.println("\n Difference of two Numbers =" +(a-b)); break; case 3: break; default:System.out.println("\n Sorry wrong option"); break; } } } Variable name a b op type int int int use To store value To store value To store value
Ques46:-program to use bubble sort to print merit list? // program to use bubble sort to print merit list import java.io.*; class q18 { public void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int num[]=new int[5],i=0,j=0,tm=0; String name[]=new String[10],st=" "; //input loop for(i=0;i<5;i++) { System.out.println("\n Enter the name of student ="); name[i]=input.readLine(); System.out.println("\n Enter the value="); num[i]=Integer.parseInt(input.readLine()); } // sorting loop for(i=0;i<4;i++) { for(j=i+1;j<5;j++) { if(num[i]<num[j]) { st=name[i]; tm=num[i]; name[i]=name[j];
45
num[i]=num[j]; name[j]=st; num[j]=tm; } } } System.out.println("\t\tMerit list"); System.out.println("S.No\t\tStudent Name \t\tMarks"); for(i=0;i<5;i++) System.out.println(+(i+1)+"\t\t"+name[i]+"\t\t"+num[i]); } } Variable name num[] i j tm Name[] st type int int inti int string int use To store value For loop To store value To store value To store value To store value
Ques47:-program to use menu driven on conversion of temp? //program to use menu driven on conversion of temp import java.io.*; import java.lang.*; class prg14 { public void q14()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int f=0,c=0,op=0; System.out.println("\n\t\t\t Main Menu"); System.out.println("\n\t 1. Fer to Cel"); System.out.println("\n\t 2. cel to fer"); System.out.println("\n\t 3. Exit"); System.out.println("\n\t Enter your option ( 1-3)="); op= Integer.parseInt(input.readLine()); switch(op) { case 1: // program fre to cel System.out.println("Enter temp in Fre="); f=Integer.parseInt(input.readLine()); c=(5*(f-32))/9;
46
System.out.println("\n temp in Cel =" +c); break; case 2: // program cel to fre System.out.println("Enter temp in cel ="); c=Integer.parseInt(input.readLine()); f=(9*c)/5 +32; System.out.println("\n Temp in fre =" +f); break; case 3: break; default:System.out.println("\n Sorry wrong option"); break; } } } Variable name f c op type int int int Use To store value To store value To store valueof chioce
Ques48:-program to use linear search? // program to use linear search import java.io.*; class q17 { public void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int i=0,c=0; double tn[]=new double[5],stn; String cn[]=new String[5]; for(i=0;i<5;i++) { System.out.println("Enter the customer name= "); cn[i]=input.readLine(); System.out.println("Enter his telephone number= "); tn[i]=Double.parseDouble(input.readLine()); } System.out.println("Enter the telephone number for searching ="); stn=Double.parseDouble(input.readLine());
47
for(i=0;i<5;i++) { if(stn==tn[i]) { System.out.println("Customer name=" +cn[i]); System.out.println("The telephone number=" +tn[i]); c++; } } if(c==0) System.out.println("the telephone number is not found"); } } Variable name i c tn[] Cn[] type int int int int use To store value To store value To store value To store value
Ques49:-program to use menu driven lcm & hcf? //program to use menu driven lcm & hcf import java.io.*; import java.lang.*; class prg13 { public void q13()throws IOException { int op=0,a=0,b=0,p=0,r=0; BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); System.out.println("\n\t\t\t Main Menu"); System.out.println("\n\t 1. L.C.M"); System.out.println("\n\t 2. H.C.F"); System.out.println("\n\t 3. Exit"); System.out.println("\n\t Enter your option ( 1-3)="); op=Integer.parseInt(input.readLine()); switch(op) { case 1: // program on lcm System.out.println("Enter first value ="); a=Integer.parseInt(input.readLine()); System.out.println("Enter second value ="); b=Integer.parseInt(input.readLine()); p=a*b;
48
r=a%b; while(r>0) { a=b; b=r; r=a%b; }; System.out.println("\n lcm of two Numbers =" +p/b); break; case 2: // program on hcf System.out.println("Enter first value ="); a=Integer.parseInt(input.readLine()); System.out.println("Enter second value ="); b=Integer.parseInt(input.readLine()); r=a%b; while(r>0) { a=b; b=r; r=a%b; }; System.out.println("\n hcf of two Numbers =" +b); break; case 3: break; default: System.out.println("\n Sorry wrong option"); break; } } } Variable name a b p r op Type int int int int int Use To store value To store value To store value To store value To store value
49
Ques50:- program to store 10 values to find out mean and fre dis table? // program to store 10 values to find out mean and fre dis table import java.io.*; class q16 { public void main()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int i=0,num[]=new int[10],a=0,b=0,c=0,d=0; double sum=0.0,mean=0.0; System.out.println("Enter the ten values "); for(i=0;i<10;i++) { num[i]=Integer.parseInt(input.readLine()); if(num[i]>80) a++; else if(num[i]>60) b++; else if(num[i]>40) c++; else d++; sum+=num[i]; } mean=sum/10; System.out.println("\t\tFrequency Distribution Table"); System.out.println("Class Interval\t\tFrequency"); System.out.println("81-100\t\t\t\t" +a); System.out.println("61-80\t\t\t\t" +b); System.out.println("41-60\t\t\t\t" +c); System.out.println("below =40\t\t\t" +d); System.out.println("Mean of the ten numbers ="+mean); } } Variable name type use i int To store value d int To store value sum int To store value mean int To store value a int To store value b int To store value
50
Ques51:-program to use menu driven on mathematical operation? //program to use menu driven on mathematical operation // using loop do-while import java.io.*; import java.lang.*; class prg12 { public void q12()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int op=0; int a=0,b=0; System.out.println("\n\t\t\t Main Menu"); System.out.println("\n\t 1. Addition"); System.out.println("\n\t 2. Subtraction"); System.out.println("\n\t 3. Exit"); System.out.println("\n\t Enter your option ( 1-3)="); op= Integer.parseInt(input.readLine()); switch(op) { case 1: // program to add System.out.println("Enter first value ="); a=Integer.parseInt(input.readLine()); System.out.println("Enter second value ="); b=Integer.parseInt(input.readLine()); System.out.println("\n Sum of two Numbers =" +(a+b)); break; case 2: // program to subtract System.out.println("Enter first value ="); a=Integer.parseInt(input.readLine()); System.out.println("Enter second value ="); b=Integer.parseInt(input.readLine()); System.out.println("\n Difference of two Numbers =" +(a-b)); break; case 3: break; default:System.out.println("Sorry wrong option "); break; } } }
51
Variable name a b op
Ques52:- loop in desending order? class q14 { public void main() { int i=0,j=0; for(i=4;i>=1;i--) { for(j=i;j>=1;j--) System.out.print(j+" "); System.out.println(); } } } Variable name i j type int int use For loop For loop
Ques53:- program to use nested if to find out fine? // program to use nested if to find out fine class prg10 { public void q10(int n) { double fn; if(n<=5) fn=n*2; else if(n<=10) fn=(10)+(n-5)*3.50; else fn=27.5+(n-10)*5; System.out.println("\n\t Total days System.out.println("\n\t Total fine } } Variable name type fn int
52
Ques54:- program to return statement? // program to return statement import java.io.*; import java.lang.*; class work { public int a,b; double c; work() { a=0; b=0; c=0.0; } double add(int a,int b) { c=(double)a+b; return c; } } class prg25 { public void q25()throws IOException { work obwork=new work(); BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); int x=0,y=0; double sum=0.0; System.out.println("Enter first value ="); x=Integer.parseInt(input.readLine()); System.out.println("Enter second value ="); y=Integer.parseInt(input.readLine()); sum=obwork.add(x,y); System.out.println("Sum of two numbers =" +sum); } } VARIABLE NAME TYPE USE a int To accept a numberfor method b Int To accept a number for method c double To add number Ques55:- to find highest,middle and lowest?
53
//to find highest,middle and lowest class pr11 { public void q11(int a,int b,int c) { if((a>b)&&(a>c)) { System.out.println("The Highest number is="+a); if(b>c) { System.out.println("The middle number is="+b); System.out.println("The lowest number is="+c); } else { System.out.println("The middle number is="+c); System.out.println("The lowest number is="+b); } } else if ((b>c)&&(b>a)) { System.out.println("The Highest number is="+b); if(c>a) { System.out.println("The middle number is="+c); System.out.println("The lowest number is="+a); } else { System.out.println("The middle number is="+a); System.out.println("The lowest number is="+c); } } else { System.out.println("The Highest numberis="+c); if(a>b) { System.out.println("The middle number is="+a); System.out.println("The lowest number is="+b); } else { System.out.println("The middle number is="+b); System.out.println("The lowest number is="+a); } } }
54
} Variable name a b c
Ques56:- program to check smith number? // program to check smith number import java.io.*; import java.lang.*; class csmith { public int num,temp,s,sum; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); void input()throws IOException { System.out.println("Enter any decimal number ="); num=Integer.parseInt(br.readLine()); } int sdigit(int n) { s=0; while(n>0) { s+=n%10; n/=10; } return s; } void pfact()throws IOException { input(); temp=num; int f=0,c=2,i=0; sum=0; while(num>1) { if(num%c==0) { f=0; for(i=1;i<=c;i++) { if(c%i==0) f++; } if(f==2) sum+=sdigit(c);
55
num/=c; } else c++; } if(sum==sdigit(temp)) System.out.println("Number is Smith number"); else System.out.println("number is not Smith number"); } public static void main()throws IOException { csmith obj=new csmith(); obj.pfact(); } } Variable name num temp s sum Type int int int int Uses To store number To store value of num To store value of remainder To store sum
Ques57:-pogram to create a class to check palindrome? // pogram to create a class to check pallindrome import java.io.*; import java.lang.*; class pallin { public String st[]=new String[5], rev[]=new String[5]; int i,j,c,len[]=new int[5]; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); void input()throws IOException { for(i=0;i<5;i++) { System.out.println("Enter name="); st[i]=br.readLine(); len[i]=st[i].length(); } } void reverse() { for(i=0;i<5;i++) { rev[i]="";
56
for(j=len[i]-1;j>=0;j--) { char ch=st[i].charAt(j); rev[i]+=ch; } } } void check() { for(i=0;i<5;i++) {if(rev[i].equals(st[i])) System.out.println("name is a pllindrome"+st[i]); else System.out.println("name is nota pllindrome"+st[i]); } } public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); pallin obj=new pallin(); obj.input(); obj.reverse(); obj.check(); } } Varible name St[] Rev[] Len[] i j c Type string string int int int int Uses To store name To store rev name To store length For loop For loop counter
Ques58:-program to print the biggest word and smallest word in a sentence? // program to print the biggest word and smallest word in a sentence import java.io.*; import java.lang.*; class St23 { public void main() throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in)); String st=" ",fst=" ",arrst[]=new String[20];
57
int s=0,c=0,len=0,i=0,j=0,l[]=new int[20],t=0; System.out.println("Enter the sentence ="); st=input.readLine(); len=st.length(); // for each word for(i=0;i<len;i++) { if(st.charAt(i)==' ') { arrst[c]=st.substring(s,i); l[c]=arrst[c].length(); s=i+1; c++; } if(i==len-1) { arrst[c]=st.substring(s,len); l[c]=arrst[c].length(); c++; } } // for sorting for(i=0;i<c;i++) { for(j=0;j<(c-1);j++) { if(l[j]>=l[j+1]) { t=l[j]; fst=arrst[j]; l[j]=l[j+1]; arrst[j]=arrst[j+1]; l[j+1]=t; arrst[j+1]=fst; } } } //for(i=0;i<c;i++) //System.out.println("The word ="+arrst[i]); //System.out.println("The total number of found word are=" +c); System.out.println("The smallest word ="+arrst[0]); System.out.println("The biggest word ="+arrst[c-1]); } }
58
Use To store value To store value To store value For loop For counter To store length For counter
Ques59:- program to search for a name in the list also display the phno? // program to search for a name in the list also display the phno import java.io.*; import java.lang.*; class St24 { public void Q16()throws IOException { BufferedReader input=new BufferedReader(new InputStreamReader(System.in));String wd="India"; String st[]=new String[5],temp; long ph[]=new long[5]; int i=0,c=0,sr=0; for(i=0;i<5;i++) { System.out.println("Enter the name ="); st[i]=input.readLine(); System.out.println("Enter the phone number ="); ph[i]=Integer.parseInt(input.readLine()); } System.out.println("Enter the name for search ="); temp=input.readLine(); for(i=0;i<5;i++) { if(temp.equals(st[i])) { sr=i; c++; } } if(c>0) { System.out.println("Search successful"); System.out.println("Name ="+st[sr] +"and tele phone numbers="+ph[sr]); }
59
else System.out.println("Search not successful"); } } Variable name i j sr c wd S[] type int int int int string string Use For loop For loop To store value To store value To store value To store valueof names
Ques60:-program to arrange all number by sorting? import java.io.*; import java.lang.*; class arrange { public int num[]=new int[100],temp,i,j,pos,n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); void input()throws IOException { for(i=0;i<10;i++) { System.out.println("Enter any number ="); num[i]=Integer.parseInt(br.readLine()); } } void bsort() { for(i=0;i<10;i++) { for(j=0;j<9;j++) { if(num[j]>num[j+1]) { temp=num[j]; num[j]=num[j+1]; num[j+1]=temp; } } } for(i=0;i<10;i++) System.out.println(num[i]); } void ssort()
60
{ for(i=0;i<9;i++) { for(j=i+1;j<10;j++) { if(num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } } } for(i=0;i<10;i++) System.out.println(num[i]); } void exsort() { for(i=0;i<10;i++) { n=num[i]; pos=i; for(j=i+1;j<10;j++) { if(n>num[j]) { n=num[j]; pos=j; } } temp=num[i]; num[i]=num[pos]; num[pos]=temp; } for(i=0;i<10;i++) System.out.println(num[i]); } public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); arrange obj=new arrange(); int op=0; System.out.println("Press 1, 2, 3 "); op=Integer.parseInt(br.readLine()); switch(op) { case 1: obj.input(); obj.bsort();
61
break; case 2: obj.input(); obj.ssort(); break; case 3: obj.input(); obj.exsort(); break; } } } Variable name num[] temp i j pos n Type int int int int int int Use To store number To store number Foor loop For loop To store number To store number
62