Q1.Write a program to enter five strings in a single dimensional array. Display those strings which are a palindrome (a word which is read same from left side as well as right side).
import java.io.*; class q1{ public static void main(String rj[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n,l,len,l1; String temp="",t; System.out.println("Enter array size"); n=Integer.parseInt(br.readLine()); String nt[]=new String[n]; System.out.println("Enter Strings"); for(l=0;l<n;l++){ t=br.readLine(); nt[l]=t; } System.out.println("Palindromes are:"); for(l=0;l<n;l++){ t=nt[l]; len=t.length(); for(l1=0;l1<len;l1++){ char x=t.charAt(l1); temp=x+temp; } if(t.equals(temp)){ System.out.println(t); } temp=""; }}} Q2. Write a class to input 10 names of your friend , sort it alphabetically and print the names in column wise by making the first character of the name in capital letter. import java.io.*; public class q2{ public static void main(String rags[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String name[]=new String[10];String s=""; System.out.println("Enter The String"); for(int l=0;l<10;l++){ name[l]=br.readLine(); } System.out.println("The output is"); for(int i=0;i<10;i++){ for(int j=0;j<9;j++){ int y=name[j].compareTo(name[j+1]); if(y>0){ String t=name[j]; name[j]=name[j+1]; name[j+1]=t; } }} for(int l=0;l<10;l++){ s=""; int ln=name[l].length(); char n=name[l].charAt(0); char u=Character.toUpperCase(n); String x=name[l].substring(1,ln); s=u+x; System.out.println(s); } } } Q3. Write a program to input 10 integers and arrange them in ascending order using selection sort techniques. import java.io.*; class q3 { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int a[]=new int[10]; int l,min=0,i,t; System.out.println("ENTER THE NUMBERS"); for(l=0;l<10;l++){ a[l]=Integer.parseInt(br.readLine()); } for(l=0;l<9;l++){ min=l; for(i=l+1;i<10;i++){ if(a[i]<a[min]){ min=i; } } t=a[l]; a[l]=a[min]; a[min]=t; } System.out.println("THE SORTED LIST IS -->"); for(l=0;l<10;l++){ System.out.println(a[l]); } } } Q4. Write a program to store 6 elements in an array P, and 4 elements in an array Q and produce a third array R, containing all the elements of array P and Q import java.io.*; public class q4 { public static void main (String rj[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int p[]=new int[6]; int q[]=new int[4]; int r[]=new int[10]; int l; System.out.println("ENTER THE ELEMENTS OF P"); for(l=0;l<6;l++){ p[l]=Integer.parseInt(br.readLine()); r[l]=p[l]; } System.out.println("ENTER THE ELEMENTS OF Q"); for(l=0;l<4;l++){ q[l]=Integer.parseInt(br.readLine()); r[l+6]=q[l]; } System.out.println("THE NEW ARRAY R IS --> "); for(l=0;l<10;l++){ System.out.println(r[l]); } } } Q5. Computer keeps information of 10 persons with their phone no., Name and addresses. Write a program using Binary search technique in which user enters any phone number and computer prints the phone no. , name and address of the person if found. In case information is not traced out print the message "Information does not exist". import java.io.*; class q5{ public static void main (String args[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String name[]=new String[10]; String adr[]=new String[10]; int no[]=new int[10]; int l,sp=0,ep=1,mid=0,se,c=0; System.out.println("ENTER THE DATA AS INSTRUCTED In ASCENDING ORDER"); for(l=0;l<10;l++) { System.out.println("ENTER THE "+(l+1)+"NAME --> "); name[l]=br.readLine(); System.out.println("ENTER THE "+(l+1)+"ADDRESS --> "); adr[l]=br.readLine(); System.out.println("ENTER THE "+(l+1)+"PHONE No. --> "); no[l]=Integer.parseInt(br.readLine()); } System.out.println("ENTER THE SEARCH ELEMENT --> "); se=Integer.parseInt(br.readLine()); ep=9; while(sp<=ep){ mid=(sp+ep)/2; if(se==no[mid]){ c=1; break; } if(se>no[mid]){ sp=mid+1; } if(se<no[mid]){ ep=mid-1; }} if(c==1){ System.out.println("SEARCH SUCCESSFUL. RECORD FOUND."); System.out.println("PHONE No. "+no[mid]); System.out.println("NAME "+name[mid]); System.out.println("ADDRESS "+adr[mid]); } else{ System.out.println("INFORMATION DOESN'T EXIST"); } } } |
Q6. WAP to enter ten characters in an array and sort them in descending order using selection sorting
import java.io.*; class q6{ public static void main (String args[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); char ar[]=new char[10]; int l=0,max,i=0; char x; System.out.println("ENTER THE CHARACTERS IN A SINGLE LINE"); for(l=0;l<10;l++){ ar[l]=(char)(br.read()); } max=0; for(l=0;l<9;l++){ max=l; for(i=l+1;i<10;i++){ if(ar[max]<ar[i]){ max=i; } } x=ar[l]; ar[l]=ar[max]; ar[max]=x; } System.out.println("THE SORTED LIST IS :--"); for(l=0;l<10;l++){ System.out.println(ar[l]); } } } Q7. Define a class RESULT with following specification: Data members/instance variables int rollno - bill number String name - name of the student int marks[] – array to store marks in 5 subjects String result– result of the student Member functions: RESULT () - constructor to create marks[] array and initialize data members with 0 or null RESULT (…) - parameterized constructor to accept rollno, name andmarks in 5 subjects through array. compute() - to calculate the result based on average marks as per given conditionsCreate an object in the main() method and invoke the above function to perform the desired task. import java.io.*; class q7 { static int rollno; static String name,result; static int marks[]=new int[5]; RESULT(){ rollno=0; name=""; result =""; } RESULT(int r,String n)throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); rollno=r; name=n; for(int l=0;l<=4;l++){ System.out.println("ENTER THE ELEMENT "+(l+1)); marks[l]=Integer.parseInt(br.readLine()); } } void compute(){ int tot=0; for(int l=0;l<=4;l++){ tot=marks[l]+tot; } double avg=tot/5; if(avg>=80){ result = "First Class With Distinction"; } if((avg>=60)&&(avg<80)){ result = "First class"; } if((avg>=50)&&(avg<60)){ result = "Second class"; } if((avg>=35)&&(avg<50)){ result = "Pass"; } if(avg<35){ result = "Promo not granted"; } } void display(){ System.out.println(result); } static void main(String args[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER ROLL No."); int r=Integer.parseInt(br.readLine()); System.out.println("ENTER THE NAME"); String n=br.readLine(); RESULT obj=new RESULT(); RESULT obj1=new RESULT(r,n); obj1.compute(); obj1.display(); } } Q8. Write a program to accept 15 integers from the keyboard, assuming that no integer entered is a zero. Enter a number from keyboard and search in an array using Binary Search Technique. If the number is present display " 67 is found at 5 index number" If the number is 67 and stored at 5 index number in an array. Else display " Number is not present in the array". import java.io.*; class q8{ public static void main (String args[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int no[]=new int[15]; int l,sp=0,ep=1,mid=0,se,c=0; System.out.println("ENTER THE DATA AS INSTRUCTED IN ASCENDING ORDER"); for(l=0;l<15;l++) { System.out.println("ENTER THE "+(l+1)+"PHONE No. --> "); no[l]=Integer.parseInt(br.readLine()); } System.out.println("ENTER THE SEARCH ELEMENT --> "); se=Integer.parseInt(br.readLine()); ep=14; while(sp<=ep){ mid=(sp+ep)/2; if(se==no[mid]){ c=1; break; } if(se>no[mid]){ sp=mid+1;} if(se<no[mid]){ ep=mid-1; }} if(c==1){ System.out.println(no[mid]+" IS FOUND AT "+mid+" INDEX NUMBER");} else{ System.out.println("NUMBER IS NOT PRESENT IN THE ARRAY"); }}} Q9. Write a Program to declare an array of 100 integers and get the numbers from the user. Sort the first 50 numbers from the array in ascending order and sort the next 50 numbers in descending order. Display the sorted array. import java.io.*; class q9 { public static void main(String rj[])throws IOException { BufferedReader mm=new BufferedReader(new InputStreamReader(System.in)); int ar[]=new int[10]; int l; System.out.println("Enter the Numbers:"); for(l=0;l<10;l++) { ar[l]=Integer.parseInt(mm.readLine()); } int m,t; for(l=0;l<5;l++) { for(m=0;m<4;m++) { if(ar[m]>ar[m+1]) { t=ar[m]; ar[m]=ar[m+1]; ar[m+1]=t; } } } for(l=0;l<5;l++) { for(m=5;m<9;m++) { if(ar[m]<ar[m+1]) { t=ar[m]; ar[m]=ar[m+1]; ar[m+1]=t; } } } System.out.println("Sorted Array:"); for(l=0;l<10;l++) { System.out.println(ar[l]); } } } |
Q10. Write a program to accept names of 100 persons and their telephones nos in a two different 1 Dimensional array and sort the telephone directory in alphabetical order of their name using bubble sort .
import java.io.*; public class q10{ public static void main(String args[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String name[]=new String[100]; int tele[]=new int[100]; System.out.println("Enter each name along with number"); for(int l=0;l<100;l++){ name[l]=br.readLine(); tele[l]=Integer.parseInt(br.readLine()); } for(int i=0;i<100;i++){ for(int j=0;j<99;j++){ int y=name[j].compareTo(name[j+1]); if(y>0){ String t=name[j]; name[j]=name[j+1]; name[j+1]=t; int t1=tele[i]; tele[i]=tele[j+1]; tele[j+1]=t1; }}} for(int l=0;l<100;l++){ System.out.println(name[l]); System.out.println(tele[l]); }}} Q11. WAP to accept ‘n’ elements from the user and store it in an array ‘a’. Reverse the array elements of array ‘a’ and store it in another array ‘b’. Find the sum of the array elements and display the output in a tabular form? import java.io.*; class q11 { public static void main(String rj[])throws IOException { BufferedReader mm=new BufferedReader(new InputStreamReader(System.in)); int n; System.out.println("Enter the No. of elements:"); n=Integer.parseInt(mm.readLine()); int a[]=new int[n]; int b[]=new int[n]; int l,rev=0,last; System.out.println("Enter the Elements:"); for(l=0;l<n;l++) { a[l]=Integer.parseInt(mm.readLine()); } for(l=0;l<n;l++) { while(a[l]>0) { last=a[l]%10; rev=rev*10+last; a[l]=a[l]/10; } b[l]=rev; rev=0; } int tot=0; System.out.println("Total of the Reversed Array:"); System.out.println("============================"); for(l=0;l<n;l++) { if(l<=n-2) { System.out.print(b[l]+" + "); } else { System.out.print(b[l]); } tot=tot+b[l]; } System.out.print(" = "+tot); } } Q12. WAP to accept 10 names in a SDA and display all those names whose first alphabet matches with the alphabet given by the user? import java.io.*; class q12 { public static void main(String rj[])throws IOException { BufferedReader mm=new BufferedReader(new InputStreamReader(System.in)); String name[]=new String[10]; int l; System.out.println("Enter the Names:"); for(l=0;l<10;l++) { name[l]=mm.readLine(); } char x; System.out.println("Enter the First Alphabet for Searching:"); x=(char)(mm.read()); char y; int cnt=0; for(l=0;l<10;l++) { y=name[l].charAt(0); if(y==x) { System.out.println(name[l]); } else { cnt++; } } if(cnt==10) { System.out.println("Sorry Search Element does not exists"); } } } |