150
//write a program to inherit a class student_detail (which takes and displays the name,admission number and address) in another class academic_detail(which takes marks of five subjects and displays the total, average, name, admission number and address) import java.io.*; class student_detail //super class { String name="",address=""; int admission_number=0; void input() throws IOException //to input the name, admission number and address { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter name,admission number and address of the student"); name=ob.readLine(); admission_number=Integer.parseInt(ob.readLine()); address=ob.readLine(); } void display() //to display name, addmission number and address { System.out.println("name-"+name+"\naddmission number-"+admission_number+"\naddress-"+address); } } public class academic_detail extends student_detail //sub class { int marks[]=new int[5],total=0,avg=0; void input() throws IOException //to call the function input() of the super class and input marks and calculate total and average { DataInputStream ob=new DataInputStream(System.in); super.input(); for(int i=0;i<5;i++) { System.out.println("enter marks"); marks[i]=Integer.parseInt(ob.readLine()); total+=marks[i]; } avg=total/5; }

12 programs.docx

Embed Size (px)

Citation preview

Page 1: 12 programs.docx

//write a program to inherit a class student_detail (which takes and displays the name,admission number and address) in another class academic_detail(which takes marks of five subjects and displays the total, average, name, admission number and address) import java.io.*;class student_detail //super class{ String name="",address=""; int admission_number=0; void input() throws IOException //to input the name, admission number and address { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter name,admission number and address of the student"); name=ob.readLine(); admission_number=Integer.parseInt(ob.readLine()); address=ob.readLine(); } void display() //to display name, addmission number and address { System.out.println("name-"+name+"\naddmission number-"+admission_number+"\naddress-"+address); }}public class academic_detail extends student_detail //sub class{ int marks[]=new int[5],total=0,avg=0; void input() throws IOException //to call the function input() of the super class and input marks and calculate total and average { DataInputStream ob=new DataInputStream(System.in); super.input(); for(int i=0;i<5;i++) { System.out.println("enter marks"); marks[i]=Integer.parseInt(ob.readLine()); total+=marks[i]; } avg=total/5; } void display() //to call display() function of super class and display total and average { super.display(); System.out.println("total="+total+"\naverage="+avg); } static void main() throws IOException { academic_detail ob1=new academic_detail(); ob1.input(); ob1.display(); }

Page 2: 12 programs.docx

}//write a program to find the sum of two angles. use object passingimport java.io.*;public class angle{ int deg, min; public angle() //constructor { deg=0; min=0; } public void inputangle() throws IOException //input degrees and minutes of the angle { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter degrees and minutes"); this.deg=Integer.parseInt(ob.readLine()); this.min=Integer.parseInt(ob.readLine()); } public void dispangle() //to display the angle { System.out.println("angle is "+this.deg+" deg "+this.min+"\'"); } public angle sumangle(angle a, angle b) //to find the sum of two angles { angle c=new angle(); //the object which has the sum and is to be returned c.deg=a.deg+b.deg; c.min=a.min+b.min; if(c.min>=60) { c.deg=c.deg+1; c.min=c.min-60; } return c; } public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); angle a=new angle(); angle b=new angle(); angle c=new angle(); //object to recieve the object sent by sumangle and store the sum of angles a.inputangle(); b.inputangle(); c=c.sumangle(a,b); c.dispangle(); }}

Page 3: 12 programs.docx

//program to insert and delete in an array queueimport java.io.*;class queue{ int num[]; int size; int front; int rear; public queue(int s) //parametrized constructor { size=s; num=new int[s]; front=-1; rear=-1; } void insert(int n) //to insert value in queue from the rear { if(rear==-1) { front=rear=0; num[rear]=n; } else if(rear==size) { System.out.println("overflow"); } else if(rear+1<num.length) { rear++; num[rear]=n; } } int delete() //to delete value from the queue from the front { int p=0; if(rear==-1) { p=0; } else { p=num[front]; if(front==rear) { front=-1; rear=-1; } else

Page 4: 12 programs.docx

{ front++; } } return p; } void show() //to display the values in the queue { if(front==-1) System.out.println("underflow!!!"); else { for(int i=front;i<=rear;i++) { System.out.println(num[i]); } } } }public class array_queue{ static queue q; public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter size"); int size=Integer.parseInt(ob.readLine()); q=new queue(size); int ch=0; while(ch!=4) { System.out.println("1.insert\n2.delete\n3.display\n4.exit\nenter choice"); ch=Integer.parseInt(ob.readLine()); switch(ch) //to check the choice of user to insert or delete value or display the queue { case 1:System.out.println("enter number"); int n=Integer.parseInt(ob.readLine()); q.insert(n); break; case 2:System.out.println(q.delete()+" deleted"); break; case 3:q.show(); break; } } }}

Page 5: 12 programs.docx

//TO INPUT 10 NUMBERS IN A ARRAY & STORE ALL UNIQUE NUMBERS IN ANOTHER ARRAYimport java.io.*;public class ARRAY_UNIQUE_NUMBER{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[]= new int[10];//ARRAY DECLARED TO GET 10 NUMBER'S FROM USER int b[]= new int[10];//ARRAY TO STORE ALL UNIQUE NUMBER'S FROM ARRAY a int k=0; for(int i=0;i<10;i++)//LOOP TO GET 10 NUMBER'S FROM USER { System.out.print("Input a["+i+"]"); a[i]= Integer.parseInt(ob.readLine()); int flag=0; for(int j=0;j<i;j++)//TO CHECK THE COUNT OF a[i] IN ARRAY a { if(a[i]==a[j]) flag++; } if(flag==0) { b[k]=a[i];//TO STORE THE UNIQUE NUMBER'S FROM ARRAY a[] TO ARRAY b[] k++; } } for(int i=0;i<k;i++)//TO PRINT THE UNIQUE NUMBER'S STORED IN ARRAY b { System.out.println(b[i]); } }}

//WRITE A PROGRAM TO SORT A 2D ARRAY ROW WISEimport java.io.*;public class ARRAYSORTROWWISE{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[3][3];//TO DECLARE AN ARRAY OF 3X3 for(int i=0;i<3;i++) //LOOPS TO INPUT VALUES IN THE ARRAY { System.out.println(); for(int j=0;j<3;j++) { a[i][j]= Integer.parseInt(ob.readLine()); System.out.print(" ");

Page 6: 12 programs.docx

} } //LOOPS TO SORT THE ARRAY ROW WISE for(int k=0;k<3;k++) { for(int i=0;i<3;i++) { for(int j=0;j<2-k;j++) { if(a[i][j]>a[i][j+1]) { int b=a[i][j]; a[i][j]=a[i][j+1]; a[i][j+1]=b; } } } } //LOOPS TO PRINT THE SORTED ARRAY ROW-WISE for(int i=0;i<3;i++) { System.out.println(); for(int j=0;j<3;j++) { System.out.print(a[i][j] +" "); } } }}

Page 7: 12 programs.docx

/** * Write a description of class b here. * * @author (your name) * @version (a version number or a date) */public class b{ // instance variables - replace the example below with your own private int x;

/** * Constructor for objects of class b */ public b() { // initialise instance variables x = 0; }

/** * An example of a method - replace this comment with your own * * @param y a sample parameter for a method * @return the sum of x and y */ public int sampleMethod(int y) { // put your code here return x + y; }}]

//WRITE A PROGRAM TO PERFORM BUBBLE SORTINGimport java.io.*;public class BUBBLE_SORTING{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[] =new int[10]; //TO DAECLARE A ARRAY OF SIZE 10 for(int i=0;i<10;i++) //LOOP TO INPUT NUMBER IN AN ARRAY {

Page 8: 12 programs.docx

System.out.println("INPUT NUMBER"); a[i]=Integer.parseInt(ob.readLine()); } //LOOPS FOR PERFOMATION OF BUBBLE SORTING for(int i=0;i<10;i++) { for(int j=0;j<9-i;j++) { if(a[j]<a[j+1])//FOR COMPARISION OF THE VALUES { //FOR SWAPING int t=a[j]; a[j]=a[j+1]; a[j+1] =t; } } } //LOOP FOR DISPLAYING THE SORTED ARRAY ON SCREEN for(int k=0;k<10;k++) { System.out.println(a[k]); } }}

//check if a number is a buzz number.Buzz number is such a number which ios either completely divisible by 7 or extreme right side digit of the number is 7. Here is the bluej program on buzz number.import java.io.*;class buzz_number{ public void main()throws IOException { DataInputStream br=new DataInputStream(System.in); System.out.println("Enter the number:"); int a=Integer.parseInt(br.readLine()); if(a%10==7 || a%7==0) //to check if number is divisible by 7 or has last digit 7 System.out.println("Entered number is a Buzz number."); else System.out.println("Entered number is not a Buzz number.");

}}

Page 9: 12 programs.docx

//WRITE A PROGRAM TO PRINT THE DAY & DATE FOR THE DAY NUMBER WITH RESPECT TO THE DAY NUMBER OF YEARimport java.io.*;public class CALENDAR{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("input number"); int d= Integer.parseInt(ob.readLine()); //TO STORE THE DAY NUMBER WITH RESPECT TO YEAR String months[] ={"jan","feb","mar","apr","may","june","july","aug","sept","oct","nov","dec"}; String days[]={"wednesday","thursday","friday","saturday","sunday","monday","tuesday"}; int days_of_month[]={31,28,31,30,31,30,31,31,30,31,30,31}; int copy=d; for(int i=0;i<12;i++) //LOOP TO THE MONTH NUMBER OF THE DAY INPUTED { if(d>days_of_month[i]) d=d-days_of_month[i]; else { System.out.print(d+" "+months[i]); break; } } //TO GET THE SPECIFIC DAY OF WEEK int t=copy%7; if(t==0) System.out.print("Day is =" + days[6]); else System.out.print("day is =" +days[t-1]); }}

//input a month and display a proper calendar of that month of 2013import java.io.*;public class calendarofmonth{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); String days[]={"mon","tue","wed","thu","fri","sat","sun"}; //days of week to written in first row System.out.println("ENTER MONTH"); int month=Integer.parseInt(ob.readLine());

Page 10: 12 programs.docx

for(int i=0;i<7;i++) //to display the days of the week { System.out.print(days[i]+"\t"); } int dates[]={31,28,31,30,31,30,31,31,30,31,30,31}; //number of days in a month monthwise int max_days=dates[month-1]; int day=0; for(int j=0;j<=month-2;j++) //to count the number of days of year that passed before 1'st of that month { day=day+dates[j]; } int k=1; for(;k<=day%7;k++) //to leave those collumns of second row which are not under the headind of first day of that month { System.out.print("\t"); } int print=1; k--; for(;print<=7;print++) //to print the first dates in rest of collumns in second row { if(k==7) break; else { System.out.print(print+"\t"); k++; } } print--; System.out.println(); int final1=1; for(;print<=max_days;print++) //to print the remaining dates { if(final1==8) { System.out.println(); final1=1; print--; } else { System.out.print(print+"\t"); final1++; } } }

Page 11: 12 programs.docx

}

import java.io.*;public class codes{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter length and code"); int n=Integer.parseInt(ob.readLine()); String s=ob.readLine(); int t=0; char ch[]={'A','C','E','G','I','K'}; if(s.length()!=n || n>6) t=1; else { for(int i=0;i<n;i++) { char c=s.charAt(i); int k=0; for(int j=0;j<6;j++) { if(c==ch[j]) { k=1; break; } } if(c<65 || c>90 || k==0) { t=1; break; } } } if(t==1) System.out.println("invalid"); else System.out.println("valid"); }}

//WRITE A PROGRAM TO PRINT THE CONSECUTIVE NUMBER WHOSE SUM IS THE INPUTED NUMBER//

Page 12: 12 programs.docx

import java.io.*;public class COMBINATION{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT NUMBER");//INPUT A NUMBER OF WHOSE COMBINATIONS HAVE TO BE FOUND// int n=Integer.parseInt(ob.readLine()); //LOOPS TO CHECK THE SUM OF CONSECUTIVE NUMBER IS THE NUMBER ITSELF OR NOT// for(int i=0;i<n;i++) { int s=0; for(int j=i;j<n;j++) { s=s+j;//TO STORE THE SUM OF THE CONSECUTIVE NUMBERS// if(s==n)//TO CHECK THE NUMBER IS EQUAL TO THE SUM OF CONSECUTIVE NUMBERS OR NOT// { //LOOP TO PRINT THE COMBINATION// for(int k=i;k<=j;k++) { System.out.print(k+"+"); } System.out.println("="+n); } } } }}

//create a class combine to merge two arrays with data members-int com[],int size-and member functions-void inputarray(), void mix(combine ,combine), void display(), void main() import java.io.*;public class combine{ int com[]; int size; public combine(int nn) {

Page 13: 12 programs.docx

size=nn; } public void inputarray()throws IOException { DataInputStream ob=new DataInputStream(System.in); com=new int[this.size]; for(int i=0;i<this.size;i++) { System.out.println("enter no."); com[i]=Integer.parseInt(ob.readLine()); } } public void mix(combine A,combine B) { int j;com=new int[this.size]; for( j=0;j<A.size;j++) { com[j]=A.com[j]; } for(int k=0;k<B.size;k++) { com[j+1]=B.com[k]; } } public void display() { for(int x=0;x<size;x++) { System.out.println(com[x]); } } public static void main()throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("enter size"); int n1=Integer.parseInt(ob.readLine()); int n2=Integer.parseInt(ob.readLine()); combine A=new combine(n1); combine B=new combine(n2); combine C=new combine(n1+n2); A.inputarray(); B.inputarray(); C.mix(A,B); C.display(); }}

Page 14: 12 programs.docx

/*a library issues books on rental basis at a 2% charge on the cost price of the book per day. if the book is returned after 7 days, fine is also charged as per the excess days. * number of excess days fine per day * 1 to 5 2.0 * 6 to 10 3.0 * above ten days 5.0 * design a super class library to display the name, author and price of the book. sub class compute to calculate fine and display the details of books with total days, fine and total amount */import java.io.*;class library //super class{ String name,author; double p; library(String n,String a,double p1) //parametrized constructor { name=n; author=a; p=p1; } void show() //display the details of the book { System.out.println("name-"+name+"\nauthor-"+author+"\nprice-Rs."+p); }}public class compute extends library //sub class{ int d,f; compute(String n,String a,double p1,int d1) //parametrized constructor { super(n,a,p1); d=d1; f=0; } void fine() //to calculate the fine as per the extra days { int extra=d-7; if(extra<=0) f=0; else if(extra<=5) f=2*extra; else if(extra<=10) f=3*extra; else f=5*extra; } void display() //to display details of book, days taken to return, fine and total amount to be paid

Page 15: 12 programs.docx

{ super.show(); fine(); double total=(2*super.p/100)+f; System.out.println("days taken to return are "+d+"\nfine is Rs."+f+"\nthe total amount to be paid is Rs."+total); } public static void main() throws IOException //to input details of book and call the other methods { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter the name of the book and name of the author"); String n=ob.readLine(); String a=ob.readLine(); System.out.println("price of the book"); double pr=Double.parseDouble(ob.readLine()); System.out.println("days of return"); int d=Integer.parseInt(ob.readLine()); compute ob1=new compute(n,a,pr,d); ob1.display(); }}//input a date and year and find the date after n days of the entered date(n be entered by the user)import java.io.*;public class days{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("DAY NUMBER"); int day=Integer.parseInt(ob.readLine()); System.out.println("YEAR"); int year=Integer.parseInt(ob.readLine()); System.out.println("DAY AFTER(N)"); int day_after=Integer.parseInt(ob.readLine()); int sum=0; int newdate=0; int p=0; String t=""; int days=day; int m[]={31,29,31,30,31,30,31,31,30,31,30,31}; String years[]={"January","February","March","April","May","June","July","August","September","October","November","December"}; if(year%4==0) //to check for leap year { m[1]=29; } else {

Page 16: 12 programs.docx

m[1]=28; } if((day>=1&&day<=366)&&(year>=1000&&year<=9999)&&(day_after>=1&&day_after<=100)) //to check the validity of date { for(int i=0;i<12;i++) //to print the date entered by user { if(day>=0&&day<=m[i]) { System.out.println(day+" "+years[i]+" "+year); break; } else { day=day-m[i]; p++; } } System.out.println("DAY AFTER "+day_after+" DAYS"); days=days+day_after; int k=0; if(days>=366) { days=days-366; k++; } for(int i=0;i<12;i++) //to display date if the date is valid else subtract the days of mont accordinf to month number { if(days>=0&&days<=m[i]) { if(k!=0) System.out.println(days+" "+years[i]+" "+(year+1)); else System.out.println(days+" "+years[i]+" "+(year)); break; } else { days=days-m[i]; } }

} }}

Page 17: 12 programs.docx

//decode the entered string. this encryption system uses a shifting process to hide a message. The value of the shift can be in the range 1 to 26. For example a shift of 7 means that A = U, B =V,C = W, etc.import java.io.*;public class Decode{ public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Coded Text : "); // inputting coded text String s = br.readLine(); int l = s.length(); s = s.toUpperCase(); // converting the coded text into Uppercase s = s + " "; // adding a space at the end if(l>=100) // checking whether length of inputted code is less than 100 System.out.println("!!! Invalid Length of Coded Text !!!"); else { System.out.print("Enter the Shift Value : "); int shift = Integer.parseInt(br.readLine()); if(shift<1 || shift>26) // checking whether shift value is between 1 and 26 System.out.println("!!! Invalid Shift Value !!!"); else { int a, b; char ch1, ch2; String dec=""; //new String for storing the decoded text for(int i=0; i<l; i++) { ch1 = s.charAt(i); // extracting characters one by one ch2 = s.charAt(i+1); // extracting the next character /* Below we are adding shift value to the characters * if ch1 = 'A' and shift = 7, * then ch1 + shift - 1 will give us: 'A'+7-1 = 65+7-1 = 71 * which is the ASCII value of 'G' */ a = ch1 + shift - 1; // storing ASCII values after adding shift to the current character b = ch2 + shift - 1; // storing ASCII values after adding shift to the next character /* If the currrent character and the next character are both 'Q' then we have a 'space' * hence the ASCII value should be 32 */ if((char)a == 'Q' && (char)b == 'Q')

Page 18: 12 programs.docx

{ a = 32; i++; } /* If ASCII value after adding the shift becomes more than 90, * then we subtract 26 from it, to make it circular, * eg. 'U'+7-1 = 85+7-1 = 91, but we want 'A' whose ASCII value is 65 * so 91-26 will give us 65 */ if(a>90) a = a - 26; if(ch1 != ' ') dec = dec + (char)a; // finally adding the decoded character to the new String } System.out.println("Decoded Text : "+dec); } } }}

// PROGRAM TO GET AN AMMOUNT FROM USER & TELL THE NUMBER OF NOTES OF EACH DENOMINATIONSimport java.io.*;public class DENOMINATIONS{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("Input ammount"); int sum= Integer.parseInt(ob.readLine());//TO GET THE AMMOUNT FROM THE USER int a[]={1000,500,100,50,20,10,5,2,1}; for(int i=0;i<9;sum=sum%a[i],i++)//LOOP FOR GATING NUMBER OF NOTES OF EACH DENOMINATIONS { int p=sum/a[i];//TO GET THE COUNT OF THE NOTES if(p==0) { continue; } System.out.println(a[i]+ "*"+ p+"="+a[i]*p); p=0; } } }

Page 19: 12 programs.docx

//WRITE A PROGRAM TO INPUT NUMBERS IN AN 2D ARRAY OF ORDER[5][4]&DISPLAY THE NUMBERS//import java.io.*;public class DISPARRAYVALSIMPLY{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[5][4];//FOR DECLARING ARRAY OF ORDER [5][4]// //LOOPS FOR INPUTING NUMBERS IN 2D ARRAY// for(int i=0;i<5;i++) { for(int j=0;j<4;j++) { System.out.print("input number at a[" + i+"][" +j+"]"); a[i][j]=Integer.parseInt(ob.readLine());

} } //LOOPS FOR DISPLAYING THE VALUES OF ARRAY// for(int i=0;i<5;i++) { for(int j=0;j<4;j++) { System.out.println("number at a[" + i+"][" +j+"]" +a[i][j]); } } } }

Page 20: 12 programs.docx

//WRITE A PROGRAM TO INPUT NUMBERS IN AN 2D ARRAY OF ORDER[5][4]&DISPLAY THE NUMBERS IN A MATRIX FORMimport java.io.*;public class DISPLAYMATRIX{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[5][4];//FOR DECLARING ARRAY OF ORDER [5][4] //LOOPS FOR INPUTING NUMBERS IN 2D ARRAY for(int i=0;i<5;i++) { for(int j=0;j<4;j++) { System.out.print("input number at a[" + i+"][" +j+"]"); a[i][j]=Integer.parseInt(ob.readLine()); } } //LOOPS FOR DISPLAYING THE VALUES OF ARRAY IN A MATRIX FORM for(int i=0;i<5;i++) { System.out.println(); for(int j=0;j<4;j++) { System.out.print(a[i][j]+ " "); } } } }

//input a no. and check if it is a part of fibnocci series or notimport java.io.*;public class fibnocci{

Page 21: 12 programs.docx

public static void main()throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("enter a no."); int n=Integer.parseInt(ob.readLine()); //the no input by user int n1=0; int n2=1; //to check if no. is a part of fibnocci series or not if(n==0) System.out.print("a no. in fibnocci series"); else {for(;;) //to check for infinite fibnocci series if(n2==n) { System.out.print("a no. in fibnocci series"); break; } else if(n2>n) { System.out.print("not a no. in fibnocci series"); break; } else //to generate fibnocci series { int n3=n2; n2=n1+n2; n1=n3; } }}}

//input the number of terms and display the fibnoccii series of strings. a,b,ba,bab,babba,babbabab.....import java.io.*;public class fibostring{ int n; String x,y,z; public fibostring() //constructor to give x,y and z their initial values { x="a"; y="b"; z=y+x; } public void accept() throws IOException //to input the number of terms { DataInputStream ob=new DataInputStream(System.in);

Page 22: 12 programs.docx

System.out.println("enter number of terms"); n=Integer.parseInt(ob.readLine()); } public void generate() //to generate the string fibnoccii series { System.out.print(x+","+y+","); for(int i=3;i<=n;i++) { System.out.print(z+","); x=y; y=z; z=y+x; } } public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); fibostring ob1=new fibostring(); ob1.accept(); ob1.generate(); }}

//to display the marks from the file student which are above 75import java.io.*;public class FILE_READER_MARKS{ public static void read_File() throws IOException { FileReader fr=new FileReader("Student.txt"); BufferedReader br=new BufferedReader(fr); int s=0; int a[]=new int[10]; String t; while((t=br.readLine())!=null) { int b=Integer.parseInt(t); if(b>=75) { a[s]=b; s++; } } br.close(); fr.close();

Page 23: 12 programs.docx

for(int i=0;i<s;i++) { System.out.println(a[i]); } }}

//to store values entered by user in a fileimport java.io.*;public class FILE_WRITER{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); FileWriter fr=new FileWriter("Student.txt"); BufferedWriter br=new BufferedWriter(fr); PrintWriter pr=new PrintWriter(br); for(int i=0;i<5;i++) { System.out.print("enter marks"); int a=Integer.parseInt(ob.readLine()); pr.println(a); } pr.close(); br.close(); fr.close(); }}

//a super class record has been defined to store names and age of n players. define a sub class find to display the names and age of all players and find the name of youngest playerimport java.io.*;class record //super class{ int age[]; String name[]; int n; public record(int m) //parametrized constructor

Page 24: 12 programs.docx

{ n=m; age=new int[n]; name=new String[n]; } public void readvalues() throws IOException //to input names and age of players { DataInputStream ob=new DataInputStream(System.in); for(int i=0;i<n;i++) { System.out.println("enter name and age"); name[i]=ob.readLine(); age[i]=Integer.parseInt(ob.readLine()); } } public void display() //to display names and age { System.out.println("names\t\tage"); for(int i=0;i<n;i++) { System.out.println(name[i]+"\t\t"+age[i]); } }}public class find extends record //sub class{ int index; public find(int t) //parametrized constructor { super(t); index=0; } public void youngest() throws IOException //to find the name and age of the youngest player { DataInputStream ob=new DataInputStream(System.in); super.readvalues(); int min=super.age[0]; for(int i=1;i<super.n;i++) { if(super.age[i]<min) { min=super.age[i]; index=i; } } } public void display() //to display name and age of youngest player {

Page 25: 12 programs.docx

super.display(); System.out.println("the age of the youngest player "+super.name[index]+" is "+super.age[index]+" years."); } public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter number of players"); int n=Integer.parseInt(ob.readLine()); find ob1=new find(n); ob1.youngest(); ob1.display(); }}

//input two 2x2 matrix and find their productimport java.io.*;public class FUNCTION_PRODUCT{ static int d[][]=new int[2][2]; public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[2][2]; int b[][]=new int[2][2]; System.out.println("1st Array---- "); for(int i=0; i<2;i++) //to input values in first array(matrix) { for(int j=0; j<2; j++) { System.out.print("Enter no. in "+(i+1)+" row & "+(j+1)+" column- "); a[i][j]=Integer.parseInt(ob.readLine()); } } System.out.println("2nd Array---- "); for(int i=0; i<2;i++) //to input values in second array(matrix) { for(int j=0; j<2; j++) { System.out.print("Enter no. in "+(i+1)+" row & "+(j+1)+" column- "); b[i][j]=Integer.parseInt(ob.readLine()); }

Page 26: 12 programs.docx

} product(a,b); } public static void product(int a[][],int b[][]) { for(int r=0; r<2;r++) //to find and store the product of the two matrices in a third array(matrix) { int s=0; for(int c=0; c<2; c++) { for(int k=0; k<2; k++) { s=s+a[r][k]*b[k][c]; } d[r][c]=s; } } for(int i=0; i<2;i++) //to display the two matrices and their product { System.out.println(); System.out.print("["); for(int j=0; j<2; j++) { System.out.print(a[i][j]+" "); } System.out.print("] "); System.out.print(" X "); System.out.print("["); for(int j=0; j<2; j++) { System.out.print(b[i][j]+" "); } System.out.print("] "); System.out.print(" = "); System.out.print("["); for(int c=0; c<2; c++) { System.out.print(d[i][c]+" "); } System.out.print("]"); }

Page 27: 12 programs.docx

}}

//create a multiple functions program input a decimal number and display its equivalent octal and hexadecimal valueimport java.io.*;public class hexoct{ public static int input()throws IOException //to input the decimal number { DataInputStream ob=new DataInputStream(System.in); System.out.print("Input decimal number"); int z=Integer.parseInt(ob.readLine()); return z; } public static int convertToOctal(int x) //to convert the number into octal { int a[]=new int[20]; int i=0; for(;x>0;x=x/8) { a[i]=x%8; i++; } int number=0; for(int j=i-1;j>=0;j--) { number=(number*10)+a[j]; } return number; } public static String convertToHexadecimal(int y) //to convert the number into hexadecimal { char a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; int b[]= new int[16]; String result=""; int i=0; for(;y>0;y=y/16) { int r=y%16; b[i]=r; i++; } for(int j=i-1;j>=0;j--) {

Page 28: 12 programs.docx

result=result+a[b[j]]; } return result;} public void display() throws IOException //to display the caculated values { int num=input(); int octal=convertToOctal(num); System.out.println("number=" +num+"\n octalform="+octal); String hexa=convertToHexadecimal(num); System.out.print("number=" +num+"\n hexadecimalform="+hexa); } public static void main()throws IOException //to call the display function { hexoct ob=new hexoct(); ob.display(); }}

/*A super class Detail has been defined to store the details of a customer. Define a subclass Bill to compute the monthly telephone charge of the customer as per the chart given below:Number Of Calls Rate1 – 100 Only Rental charge101 – 200 60 paisa per call + rental charge201 – 300 80 paisa per call + rental chargeAbove 300 1 rupee per call + rental charge*/import java.io.*;class Detail //superclass { String name, address; long telno; double rent; Detail(String n1, String a1, long t1, double r1) { name = n1; address = a1; telno = t1;

Page 29: 12 programs.docx

rent = r1; } void show() { System.out.println("Name of customer = "+name); System.out.println("Address = "+address); System.out.println("Telephone Number = "+telno); System.out.println("Monthly Rental = Rs. "+rent); } } class Bill extends Detail //subclass { int n; double amt; Bill(String n1, String a1, long t1, double r1, int c) { super(n1,a1,t1,r1); //initializing data members of superclass by calling its constructor n = c; amt = 0.0; } void cal() { if(n>=1 && n<=100) amt = rent; else if(n>=101 && n<=200) amt = 0.6*n + rent; else if(n>=201 && n<=300) amt = 0.8*n + rent; else amt = 1*n + rent; } void show() { super.show(); //calling the superclass function show() System.out.println("No. of calls = "+n); System.out.println("Amount to be paid = Rs. "+amt); } } public class inheritence_bill //Class which will contain the main() method and execute the program { public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the name : ");

Page 30: 12 programs.docx

String n1=br.readLine(); System.out.print("Enter the address : "); String a1=br.readLine(); System.out.print("Enter the telephone number : "); long t1=Long.parseLong(br.readLine()); System.out.print("Enter the monthly rental : "); double r1=Double.parseDouble(br.readLine()); System.out.print("Enter the number of calls : "); int c=Integer.parseInt(br.readLine()); Bill ob=new Bill(n1,a1,t1,r1,c); //creating object of subclass System.out.println("*** Output ***"); ob.cal(); ob.show(); //calling show() function of subclass } }

Page 31: 12 programs.docx
Page 32: 12 programs.docx
Page 33: 12 programs.docx

//A super class Record has been defined to store the names and ranks of 50 students. Define a sub class Rank to find the highest rank along with the name.import java.io.*;class Record //superclass { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String name[]; int rnk[]; Record() { name = new String[5]; rnk = new int[5]; } void readvalues()throws IOException { System.out.println("*** Inputting The Names And Ranks ***"); for(int i=0;i<5;i++) { System.out.print("Enter name of student "+(i+1)+" : "); name[i]=br.readLine(); System.out.print("Enter his rank : "); rnk[i]=Integer.parseInt(br.readLine()); } } void display() { System.out.println("Name\t\tRank"); for(int i=0;i<5;i++) { System.out.println(name[i]+"\t\t"+rnk[i]); } } } class Rank extends Record//subclass { int index; Rank() { super(); //invoking the constructor of superclass index = 0; } void highest() { int min = rnk[0]; for(int i=0;i<5;i++) { if(rnk[i]<min) {

Page 34: 12 programs.docx

index = i; } } } void display() { super.display(); //calling the superclass function display() System.out.println("nTop most rank = "+rnk[index]); System.out.println("Student with topmost rank = "+name[index]); } } public class inheritence_rank extends Rank //Class which will contain the main() method and execute the program { public static void main()throws IOException { Rank ob=new Rank(); //creating object of subclass ob.readvalues(); //calling radvalues() function of superclass to input the names and ranks ob.highest(); //calling the function highest() for finding index of topmost rank System.out.println("*** Output ***"); ob.display(); //calling display() function of subclass } }

//create a mxm array(m entered by user) and arrange the values in that array from 1 to m^2 in a circular manner(from outside to inside)import java.io.*;public class INPUT_NUMBERS_IN_ARRAY_IN_CIRCULAR_MANNER{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int m=Integer.parseInt(ob.readLine()); int a[][]=new int[m][m]; int i=0,k=1,j=0,u=m-1,n=m,t=0,s=m-1,q=0; outer:while(k<m*m+1) { for(j=k;j<n;j++) //to input values in the upper row(left to right) { a[t][j]=k++; if(k==m*m+1) break outer; } for(i=t+1;i<n;i++) //to input the values in the right collumn(top to bottom) { a[i][s]=k++; if(k==m*m+1)

Page 35: 12 programs.docx

break outer; } for(j=n-2;j>=t;j--) //to input values in the lower row(right to left) { a[u][j]=k++; if(k==m*m+1) break outer; } for(i=n-2;i>t;i--) //to input values in the left collumn(bottom to top) { a[i][q]=k++; if(k==m*m+1) break outer; } t++; s--;n--;q++;u--; } for(int c=0;c<m;c++) { System.out.println(); for(int d=0;d<m;d++) { System.out.print(a[i][j]+"\t"); } } } }

//write a program to identify a karprekar number- the number having d digits is squared and split to two parts, right part having d digits and left part having d or d-1 digits.if sum of right and left part is equal to the number, the number is a karprekar numberimport java.io.*;public class karprekar_number{ int number, digits, right, left, sqrnum; public karprekar_number(int nn) //parametrized constructor

Page 36: 12 programs.docx

{ number=nn; } public void extract() //to extract the left and right part of the square of number { sqrnum=number*number; int l=0; for(int i=number;i>0;i=i/10) //to find the number of digits in the number { l=l+1; } digits=l; int r=0; right=sqrnum%(int)(Math.pow(10,l)); left=sqrnum/(int)(Math.pow(10,l)); System.out.println(right+" "+left); } public void iskarprekar() //to compare the sum of left and right and the original number { if((left+right)==number) System.out.println(number+" is a karprekar number"); else System.out.println(number+" is not a karprekar number"); } public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter number"); int n=Integer.parseInt(ob.readLine()); karprekar_number ob1=new karprekar_number(n); ob1.extract(); ob1.iskarprekar(); }}

Page 37: 12 programs.docx

//PROGRAM TO GET LCM OF TWO NUMBERSimport java.io.*;public class LCM{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int lcm=1; System.out.println("INPUT NUMBER A"); int a=Integer.parseInt(ob.readLine()); System.out.println("INPUT NUMBER B"); int b=Integer.parseInt(ob.readLine()); for(int i=a;i<=a*b;i++)//LOOP FOR GETTING LCM OF TWO NUMBERS { if(i%a==0&&i%b==0) { lcm=i; break; } } System.out.print("LCM OF 2NUMBERS ARE "+ lcm);//TO PRINT LCM OF TWO NUMBERS }}

//print the values in lower diagonal of a matriximport java.io.*;public class lowerdiagonal{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[3][3];//TO DECLARE AN 2D ARRAY// //LOOPS TO INPUT NUMBERS IN THE ARRAY// for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print("Input Numbers"); a[i][j]=Integer.parseInt(ob.readLine()); } } for(int i=0;i<3;i++) { for(int j=0;j<3;j++)

Page 38: 12 programs.docx

{ if(j<=i) //in the lower diagonal,the collumn number<=the row number { System.out.print(a[i][j]+" "); } else { System.out.print(" "); System.out.println(); } } } } }

Page 39: 12 programs.docx

//print the values in lower diagonal of a matriximport java.io.*;public class lowerdiagonal{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[3][3];//TO DECLARE AN 2D ARRAY// //LOOPS TO INPUT NUMBERS IN THE ARRAY// for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print("Input Numbers"); a[i][j]=Integer.parseInt(ob.readLine()); } } for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(j<=i) //in the lower diagonal,the collumn number<=the row number { System.out.print(a[i][j]+" "); } else { System.out.print(" "); System.out.println(); } } } } }

Page 40: 12 programs.docx
Page 41: 12 programs.docx
Page 42: 12 programs.docx
Page 43: 12 programs.docx
Page 44: 12 programs.docx

/* Generates a magic square of order N. A magic squares is an N-by-N matrix of the integers 1 to N^2, such that all row, column, and diagonal sums are equal. * One way to generate a magic square when N is odd is to assign the integers 1 to N^2 in ascending order, starting at the bottom, middle cell. Assign the next integer to the cell one right and one down. If this cell has already been assigned another integer, instead use the cell adjacently above of the cell frrom which you came there. If the cell goes out of the grid,, use wrap around, i.e., put the value in the cell at the opposite end. * java MagicSquare 3 * 4 9 2 * 3 5 7 * 8 1 6 */import java.io.*;public class MagicSquare{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter n"); int N = Integer.parseInt(ob.readLine()); if (N % 2 == 0) System.out.println("n must be odd"); int magic[][]= new int[N][N]; int row = N-1; int col = N/2; magic[row][col] = 1; for (int i = 2; i <= N*N; i++) //to generate the magic square { if (magic[(row + 1) % N][(col + 1) % N] == 0) //to check if the cell is empty { row = (row + 1) % N; col = (col + 1) % N; } else { row = (row - 1 + N) % N; // don't change col } magic[row][col] = i; } for (int i = 0; i < N; i++) //to print the magic square { for (int j = 0; j < N; j++) { if (magic[i][j] < 10) System.out.print(" "); // for alignment if (magic[i][j] < 100) System.out.print(" "); // for alignment System.out.print(magic[i][j] + " "); } System.out.println();

Page 45: 12 programs.docx

} }}

Page 46: 12 programs.docx

/*a class Matrix has a 2-d array of order m x n(m,n<25). find the difference of two matrices. * int arr[][], int m, int n * matrix(int mm,int nn) * void fillarray()-to input value in matrix * matrix submat(matrix a)-subtract matrix of current object from that of the parameter object and return the result * void display()-to display matrix */import java.io.*;public class matrix{ int arr[][],m,n; matrix(int mm,int nn) //parametrized constructor { m=mm; n=nn; arr=new int[m][n]; } void fillarray() throws IOException //to input values in array { DataInputStream ob=new DataInputStream(System.in); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { System.out.println("enter element "+i+","+j); this.arr[i][j]=Integer.parseInt(ob.readLine()); } } } matrix submat(matrix a) //to subtract the matrix of current object from that of the parameter object { matrix r=new matrix(m,n); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { r.arr[i][j]=a.arr[i][j]-this.arr[i][j]; } } return r; } void display() //to display the current object's matrix { for(int i=0;i<m;i++) { System.out.print("[");

Page 47: 12 programs.docx

for(int j=0;j<n;j++) { System.out.print(arr[i][j]+"\t"); } System.out.println("]"); } } public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter size of matrices m,n"); int m1=Integer.parseInt(ob.readLine()); int n1=Integer.parseInt(ob.readLine()); matrix a=new matrix(m1,n1); matrix b=new matrix(m1,n1); System.out.println("------1'st array------"); a.fillarray(); System.out.println("------2'nd array------"); b.fillarray(); matrix c=a.submat(b); System.out.println("---1'st matrix---"); a.display(); System.out.println("---2'nd matrix---"); b.display(); System.out.println("--their difference---"); c.display(); }}

Page 48: 12 programs.docx

//create a 4x4 matrix and display its mirror matrix,i.e., reverse the order of rowsimport java.io.*;public class MIRRORMAT2{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[4][4]; for(int i=0;i<4;i++) //to input values in array { for(int j=0;j<4;j++) { System.out.print("Input Number"); a[i][j]= Integer.parseInt(ob.readLine()); } } for(int i=3;i>=0;i--) //to display the mirror image of array { System.out.println(); for(int j=0;j<4;j++) { System.out.print(a[i][j]+" "); } }}}

Page 49: 12 programs.docx

//wap to input a 3x3 matrix and display its mirror matrix,i.e.,reverse the order of collumnsimport java.io.*;public class MIRRORMATRIX{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[4][4]; //to input values in the matrix for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { System.out.print("Input Number"); a[i][j]= Integer.parseInt(ob.readLine()); } } //to display the mirror matrix for(int i=0;i<=3;i++) { System.out.println(); for(int j=3;j>=0;j--) { System.out.print(a[i][j]+" "); } }}}

Page 50: 12 programs.docx

//check if a number is a neon number.A neon number is that where sum of digits of square of the number is equal to the number.import java.io.*;public class Neon{ public void main() throws Exception { DataInputStream br=new DataInputStream(System.in); System.out.println("Enter the number:"); int n=Integer.parseInt(br.readLine()); int sq=n*n; int sum=0; while(sq>0) //to find the sum of digits of square of number { sum=sum+sq%10; sq=sq/10; } if(n==sum) System.out.println(n+ " is a neon number"); else System.out.println(n+ " is not a neon number"); }}

Page 51: 12 programs.docx

//to input a number and print the longest series of non-prime numbers from 1 to that numberimport java.io.*;public class non_prime_series{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter n"); int n=Integer.parseInt(ob.readLine()); int j=0;int k=0; int pri[]=new int[n]; for(k=2;k<=n;k++) //to store all the prime numbers in the range in an array { if(prime(k)==true) { pri[j]=k; j++; } } if(pri[j-1]!=n) //to assure that the last number is the number entered by user { pri[j]=n+1; } int max=0,maxi=0,maxf=0; for(int i=0;i<(j);i++) // to find that the longest series lies between which two numbers(prime) { int diff=pri[i+1]-pri[i]; if(diff>max) { max=diff; maxi=pri[i]; maxf=pri[i+1]; } } System.out.println("series"); // to print the numbers between the two prime numbers seperated for(int i=maxi+1;i<maxf;i++) { System.out.println(i); } } public static boolean prime(int n) //to check if a number is prime { int f=0;

Page 52: 12 programs.docx

for(int i=1;i<=n;i++) { if((n%i)==0) f++; //to count number of factors } if(f==2) return true; else return false; }}

//INPUT 10 NUMBER IN A ARRAY & CHECK IF THEY ARE PERFECT NUMBER OR NOT IF IS THEN STORE IN ANOTHER ARRAYimport java.io.*;public class PERFECT_NUMBER{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[] =new int[10]; //TO DECLARE ARRAY A &PROVIDING SIZE OF 10 int b[] =new int[10]; //TO DECLARE ARRAY B &PROVIDING SIZE OF 10 int d=0; for(int i=0;i<10;i++) //TO INPUT 10 NUMBERS IN ARRAY A { System.out.println("input a["+i+"]"); a[i]=Integer.parseInt(ob.readLine()); int sum=0; for(int j=a[i];j>0;j=j/10) //TO EXTRACT DIGITS OF NUMBER { int r=j%10; int t=1; for(int l=1;l<=r;l++) //TO FIND FACTORIAL OF THE DIGITS OF THE NUMBER {

Page 53: 12 programs.docx

t=t*l; } sum=sum+t; //TO GET SUM OF THE FACTORIAL if(sum==a[i]) //TO COMPARE THE SUM WITH THE ORIGINAL NUMBER { b[d]=a[i]; //TO GET THE PERFECT NUMBER IN ARRAY A TO ARRAY B d++; } } } for(int k=0;k<d;k++) //TO PRINT THE PERFECT NUMBER { System.out.println("b[" +k+"]"+ b[d]); } }}

//enter a three letter word and find all the permutations of those letters(unrepeated)import java.io.*;public class PERMUTATION

Page 54: 12 programs.docx

{ //CONCEPT:fix one letter of the word and display the rest two letters by interchanging their places. repeat the same with other two letters. public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("Enter a three letter word "); String s=ob.readLine(); int index=0; for(int i=0;i<3;i++) { char ch=s.charAt(index); String r=remaining(s,ch); String s1=shuffle(r); System.out.println(ch+r); System.out.println(ch+s1); index++;

} } public static String remaining(String s,char ch) //to find the remaining letters { String r=""; for(int i=0;i<3;i++) { char c=s.charAt(i); if(c!=ch) r=r+c; } return r; } public static String shuffle(String r) //to interchange the place of the remaining letters { String k=""; for(int i=0;i<2;i++) { char c=r.charAt(i); k=c+k; } return k; }}

Page 55: 12 programs.docx

//Write a program that encodes a word into Piglatin. To translate a word into a Piglatin word, convert the word into uppercase and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel being shifted towards the end followed by “AY”.import java.io.*;class Piglatin { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); System.out.print("Enter any word: "); String s=br.readLine(); s=s.toUpperCase(); //converting the word into Uppercase int l=s.length(); int pos=-1; char ch; for(int i=0; i<l; i++) { ch=s.charAt(i); if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { pos=i; //storing the index of the first vowel break; } } if(pos!=-1) //printing piglatin only if vowel exists {

Page 56: 12 programs.docx

String a=s.substring(pos); //extracting all alphabets in the word beginning from the 1st vowel String b=s.substring(0,pos); //extracting the alphabets present before the first vowel String pig=a+b+"AY"; //adding "AY" at the end of the extracted words after joining them System.out.println("The Piglatin of the word = "+pig); } else System.out.println("No vowel, hence piglatin not possible"); } }

// A PROGRAM TO INPUT 10 NUMBER IN AN ARRAY STORE ONLY PRIME NUMBER'S IN ANOTHER ARRAY//import java.io.*;public class PRIME_NUMBER{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[] =new int[10];//ARRAY DECLARED TO GET 10 NUMBER'S FROM USER// int b[] =new int[10];//ARRAY DECLARED TO GET ALL PRIME NUMBER'S FROM ARRAY A TO B// int s=0; for(int i=0;i<10;i++)// LOOP FOR GETTING 10 NUMBER'S IN A ARRAY// { System.out.println("Input a[" +i+"]");

Page 57: 12 programs.docx

a[i]=Integer.parseInt(ob.readLine()); int factors =0; for(int j=1;j<=a[i];j++)// LOOP FOR TAKING THE COUNT OF FACTORS OF a[i]// { if(a[i]%j==0)// TO GET THE COUNT OF FACTORS // factors++; } if(factors==2)//TO COMPARE THE COUNT OF FACTORS WITH 2 TO CERTIFY IS IT A PRIME NUMBER// { b[s]=a[i];//TO STORE PRIME NUMBER IN ANOTHER ARRAY// s++; } } for(int f=0;f<s;f++)//TO PRINT THE PRIME NUMBER IN ARRAY B// { System.out.println("PRIME NUMBER'S"+b[f]); } } }

Page 58: 12 programs.docx

//input a number and display the pair of prime numbers from 1 to that number which sum up to the numberimport java.io.*;public class prime_sum{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int n=0; for(;;) //to input an even number { System.out.println("enter number"); n=Integer.parseInt(ob.readLine()); if(n>2&&n<(Math.pow(10,14))&&(n%2)==0) break; } int npr=0; for(int i=1;i<n;i++) //to count the number of prime numbers in the range { if(prime(i)==true) npr++; } int a[]=new int[npr]; int k=0; for(int i=0;i<n;i++) //to store the prime numbers in an array { if(prime(i)==true) { a[k]=i; k++; } } for(int i=0;i<npr;i++) //to display the pairs which sum up to the number { int r=a[i]; for(int j=i;j<npr;j++) { int b=r+a[j]; if(b==n) System.out.println(r+"+"+a[j]); } }

Page 59: 12 programs.docx

} public static boolean prime(int n) //to check if a number is prime { int f=0; for(int i=1;i<=n;i++) { if((n%i)==0) f++; } if(f==2) return true; else return false; }}

//make a multiple function program to input a sentence, store its words in an array and sort them lengthwise(those with same length are to be sorted lexographically)import java.util.*;import java.io.*;

Page 60: 12 programs.docx

public class pro7{ String S; String word[]; String newS; public void input() throws IOException //to input the sentence { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT S"); S=ob.readLine(); } public void store() //to store the words in an array using string tokenizer { StringTokenizer s1=new StringTokenizer(S); int c=s1.countTokens(); word=new String[c]; for(int i=0;i<c;i++) { word[i]=s1.nextToken(); } } public void Sort() { StringTokenizer s1=new StringTokenizer(S); int c=s1.countTokens(); for(int i=0;i<c;i++) { for(int j=0;j<c-1-i;j++) { if(word[j].length()<word[j+1].length()) //to sort the words lengthwise { String temp=word[j]; word[j]=word[j+1]; word[j+1]=temp; } else if(word[j].length()==word[j+1].length()) //if of same length, sort lexographically { if(word[j].compareTo(word[j+1])>0) { String temp=word[j]; word[j]=word[j+1]; word[j+1]=temp; } } } }

Page 61: 12 programs.docx

for(int k=0;k<c;k++) System.out.print(word[k]+" "); } public static void main() throws IOException //to call all the functions { pro7 p1=new pro7(); p1.input(); p1.store(); p1.Sort(); }}

//WRITE A PROGRAM TO INPUT 14 NUMBERS IN AN ARRAY &CHECK IF IT IS A PSUEDOPODIC SERIESimport java.io.*;public class PSUEDOPODIC_SERIES{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in);

Page 62: 12 programs.docx

System.out.println("enter the number of terms"); int n=Integer.parseInt(ob.readLine()); int a[] =new int[n];//TO DECLARE ARRAY OF SIZE 14 for(int i=0;i<n;i++)//LOOP TO INPUT NUMBER { System.out.println("INPUT NUMBERS"); a[i]=Integer.parseInt(ob.readLine()); } int k=n-1; int s=0; int d=0; if(n%2==0) d=n/2; else d=(n-1)/2; for(int j=0;j<d;j++)//LOOP FOR CHECKING FOR PSUEDOPODIC SERIES { if(a[j]==a[k]) { s++; k--; } } if(s==d) System.out.print("is a psudopodic series"); else System.out.print("not a psudopodic series"); }}

Page 63: 12 programs.docx

import java.io.*;public class PUSHPOP{ static int ctr=-1; static int stack[]=new int[10]; public static void abc()throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("--------starting stack test---------"); for(int i=0;i<10;i++) { System.out.print("enter a number"); int j=Integer.parseInt(ob.readLine()); push(j); System.out.print("pushed:" +j); } System.out.println(); while(!isEmpty()) { System.out.println("popped:" +pop()); } System.out.println("----------stack test over----------"); } public static boolean isEmpty() { return ctr==-1; } public static void push(int i) { if(ctr+1<stack.length) stack[++ctr]=i; } public static int pop() { if(isEmpty()) return 0; return stack[ctr--]; }

Page 64: 12 programs.docx

}

//create a class rack which can store the name of atmost 20 books from which a book can be added or removed only from top.create functions to display books whose name have less than 10 characters, all books, input a book name,remove book from stack and check if rack is empty or not.import java.io.*;public class rack{ String book[]; //array to store name of books String name; //to hold name of book int limit, top; //to hold the limit of books and to indicate the topmost book in the rack public rack(int nx) //parametrized constructor { limit=nx; book=new String[nx]; top=-1; } public boolean isempty() //to check if the rack is empty { if(top==-1) return true; else return false;

Page 65: 12 programs.docx

} public void display() //to display the name of books which have less than 10 characters { for(int i=0;i<=top;i++) { int l=book[i].length(); if(l<10) System.out.println(book[i]); } } public void putbook() throws IOException //to insert the name of book { DataInputStream ob=new DataInputStream(System.in); if(top>=limit) System.out.println("rack full"); else { System.out.println("enter name"); name=ob.readLine(); book[++top]=name; } } public void removebook() //to remove a book from rack { if(isempty()) System.out.println("rack empty"); else System.out.println(book[top--]+" removed"); } public void displayall() //to display the name of all books in rack { for(int i=0;i<=top;i++) { System.out.println(book[i]); } } public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int ch=0; rack ob1=new rack(20); do { System.out.println("1.add book\n2.remove book\n3.display books whose name is less than 10 characters\n4.display all\n5.exit\nenter choice"); ch=Integer.parseInt(ob.readLine()); switch(ch) {

Page 66: 12 programs.docx

case 1:ob1.putbook(); break; case 2:ob1.removebook(); break; case 3:ob1.display(); break; case 4:ob1.displayall(); break; } }while(ch!=5); }}

//display the details of the volunteers whose first name is entered by the user from the file volunteer.txt(there can be more than one volunteer of same first name)import java.io.*;import java.util.*;public class read_volunteer{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter the first name of volunteer");

Page 67: 12 programs.docx

String nf=ob.readLine(); FileReader f=new FileReader("volunteer.txt"); BufferedReader b=new BufferedReader(f); String a=""; while((a=b.readLine())!=null) { StringTokenizer s=new StringTokenizer(a); String s1=s.nextToken(); if(s1.equals(nf)) { System.out.println("name-"+s1+" "+s.nextToken()+"\naddress-"+s.nextToken()+"\ncontact number-"+s.nextToken()); } } }}

//input a string and count the number of vowels using recursionimport java.io.*;public class RECURSION_COUNT_VOWELS

Page 68: 12 programs.docx

{ static int s; public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT STRING"); String a=ob.readLine(); int length=a.length(); length-=1; s=0; count(a,length); } public static void count(String x,int l) { if(l>=0) { char b=x.charAt(l); if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u') s++; count(x,l-1); System.out.print("NUMBER OF VOWELS"+s); } }}

Page 69: 12 programs.docx

//input a number and find its factorial using recursion import java.io.*;public class RECURSION_FACTORIAL{ public static void main()throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT NUMBER OF WHICH FACTORIAL IS FOUND"); int a=Integer.parseInt(ob.readLine()); int result =factorial(a); System.out.print(result); } public static int factorial(int x) { if(x==1) return 1; else return (x*factorial(x-1)); } }

Page 70: 12 programs.docx

//input a number and find its factors using recursionimport java.io.*;public class RECURSION_FACTORS{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT NUMBERS"); int a=Integer.parseInt(ob.readLine()); int m=1; factors(a,m); } public static void factors(int n,int d) { if(d<=n) { if(n%d==0) System.out.println(d); factors(n,++d); } }}

Page 71: 12 programs.docx

//display the sum of first m terms of fibnocci series(m entered by user)import java.io.*;public class RECURSION_FIBNOCACI { public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("ENTER NUMBER"); int n=Integer.parseInt(ob.readLine()); for(int i=0;i<n;i++) { System.out.print(" "+fib(i)); } } public static int fib(int m) { if(m<2) return m; else return fib(m-1)+fib(m-2); }}

Page 72: 12 programs.docx

//input two numbers and find x^n using recursion import java.io.*;public class RECURSION_POWER{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT NUMBER TO FIND TH POWER OF"); int a=Integer.parseInt(ob.readLine()); System.out.print("INPUT NUMBER TO FIND THE POWER OF"); int b=Integer.parseInt(ob.readLine()); int result=power(a,b); System.out.print(result); } public static int power(int x,int n) { if(n==1) return x; else return (x*power(x,n-1)); } }

Page 73: 12 programs.docx

//input a number and use recusion to display numbers from 1 to that numberimport java.io.*;public class RECURSION_PRINT_1_N{ static int l; public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT NUMBER"); int a=Integer.parseInt(ob.readLine()); l=1; print(a,l); } public static void print(int n,int l)//recursion function { if(l<=n) { System.out.println(l); print(n,l+1); } } }

Page 74: 12 programs.docx

//input a sentence and extract the words using recursionimport java.io.*;public class RECURSION_SENTENCE_WORD_EXTRACT{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("Enter a Sentence "); String s=ob.readLine(); s=" "+s; int l=s.length(); String ne=" "; words(s,l-1,ne); }public static void words(String s,int l,String ne){ if(l>=0) { for(;l>=0;l--) { if(s.charAt(l)==' ') break; else ne=s.charAt(l)+ne; } s=s.substring(0,l); System.out.println(ne); ne=" "; words(s,l-1,ne); }}}

Page 75: 12 programs.docx

//input a and n and find the sum of series=>a^n + a^(n-2) +a^(n-4) +............import java.io.*;public class RECURSION_SUM{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT NUMBERS"); int a=Integer.parseInt(ob.readLine()); int n=Integer.parseInt(ob.readLine()); double sum=sum(a,n); System.out.print(sum); } public static double sum(int x,int d) { if(d==1) return x; else return Math.pow(x,d)+sum(x,d-2); } }

Page 76: 12 programs.docx

//input a number and find the sum of its digits using recursionimport java.io.*;public class RECURSION_SUM_OF_DIGITS_1_N{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("ENTER NUMBER"); int n=Integer.parseInt(ob.readLine()); int sum=sum(n); System.out.print(sum); } public static int sum(int a) { if(a==1) return 1; else return (a+sum(a-1)); } }

Page 77: 12 programs.docx

//input a number x and its maximum power n and find the sum of series-> (x/(2!)) + ((x^2)/(3!)) +((x^3)/(4!)) +...........+ ((x^n)/((n+1)!))import java.io.*;public class RECURSION_SUM_OF_SERIES{ static double sum; public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT NUMBER"); int a=Integer.parseInt(ob.readLine()); System.out.println("entter maximum power"); int b=Integer.parseInt(ob.readLine()); sum=0.0; sum(a,b); } public static void sum(int x,int n) //to find sum { if(n>0) { sum+=Math.pow(x,n)/fact(n+1); sum(x,n-1); } else System.out.print("SUM OF SERIES="+sum); } public static int fact(int c) //to find factorial,i.e., value in denomenator { if(c==1) return 1; else return c*fact(c-1); }}

Page 78: 12 programs.docx

//input a string and find its reverse using recursionimport java.io.*;public class recursionreverse{ static String s; public static void main()throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT STRING"); String a=ob.readLine(); int l=a.length(); s=""; reverse(a,l-1); } public static void reverse(String c,int e) { if(e>=0) { s+=c.charAt(e); reverse(c,e-1); } else System.out.print(s); } }

Page 79: 12 programs.docx

//use recursion to find the sum of a number entered by the userimport java.io.*;public class RECURSIONSUMDIGITOFNUMBER{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT NUMBER"); int a=Integer.parseInt(ob.readLine()); int sum=sum(a); System.out.print(sum); } public static int sum(int n) { if(n>0) { return((n%10)+sum(n/10)); } else return n; } }

Page 80: 12 programs.docx

//program to insert and delete items, count number of nodes, inorder, postorder and preorder traversal and search in a binary tree using recursive functionsimport java.io.*;class treenode{ treenode left,right; int data; treenode(int n) //parametrized constructor { data=n; left=null; right=null; } void setleft(treenode n) //to set the left part of the tree { left=n; } void setright(treenode n) //to set the right part of the tree { right=n; } treenode getright() //to access the right part of the tree { return right; } treenode getleft() //to access the left part of the tree { return left; } int getdata() //to get the value of the node of the tree { return data; }}class binarytree{ treenode root; binarytree() //default constructor { root=null; } void insert(int data) { root=insert(root,data); } treenode insert(treenode node,int data) //to insert a node in the tree

Page 81: 12 programs.docx

{ if(node==null) node=new treenode(data); else { if(data<=node.getdata()) node.left=insert(node.left,data); else node.right=insert(node.right,data); } return node; } void delete(int k) //to delete a value from the tree { if(root==null) System.out.println("empty tree"); else if(search(k)) { root=delete(root,k); System.out.println(k+" deleted"); } else System.out.println(k+" doesn't exist"); } treenode delete(treenode root,int k) //to delete a value from the tree { treenode p,p2,n; if(root.getdata()==k) { treenode lt=root.getleft(); treenode rt=root.getright(); if(lt==null && rt==null) //delete root return null; else if(lt==null) //if left subtree is null { p=rt; return p; } else if(rt==null) //if right subtree is null { p=lt; return p; } else //when it has both subtrees { p2=rt; p=p2; while(p.getleft()!=null)

Page 82: 12 programs.docx

{ p=p.getleft(); } p.setleft(lt); return p2; } } //to switch between left and right subtrees if(k<root.getdata()) { n=delete(root.getleft(),k); root.setleft(n); } else { n=delete(root.getright(),k); root.setright(n); } return root; } int countnodes() { return countnodes(root); } int countnodes(treenode r) //to count the number of nodes in the tree { if(r==null) return 0; else { int l=1; l=l+countnodes(r.getleft()); l=l+countnodes(r.getright()); return l; } } boolean search(int v) { return search(root,v); } boolean search(treenode r,int val) //to search for a value in the tree { boolean f=false; while(r!=null && f!=true) { int rval=r.getdata(); if(val<rval) r=r.getleft();

Page 83: 12 programs.docx

else if(val>rval) r=r.getright(); else { f=true; break; } f=search(r,val); } return f; } void inorder() { inorder(root); } void inorder(treenode r) //to display the values in the tree in inorder { if(r!=null) { inorder(r.getleft()); System.out.print(r.getdata()+" "); inorder(r.getright()); } } void preorder() { preorder(root); } void preorder(treenode r) //to display the values in the tree in preorder { if(r!=null) { System.out.print(r.getdata()+" "); preorder(r.getleft()); preorder(r.getright()); } } void postorder() { postorder(root); } void postorder(treenode r) //to display the values in the tree in postorder { if(r!=null) { postorder(r.getleft()); postorder(r.getright()); System.out.print(r.getdata()+" ");

Page 84: 12 programs.docx

} }}public class recursive_binary_tree{ static binarytree b=new binarytree(); public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int ch=1; do { //to display the menu and proceed accordingly System.out.println("1.insert\n2.delete\n3.count number of nodes\n4.inorder traversal\n5.preorder traversal\n6.postorder traversal\n7.search\n8.exit\nenter choice"); ch=Integer.parseInt(ob.readLine()); switch(ch) { case 1:System.out.println("enter number"); int k=Integer.parseInt(ob.readLine()); b.insert(k); break; case 2:System.out.println("enter number to be deleted"); int k2=Integer.parseInt(ob.readLine()); b.delete(k2); break; case 3:System.out.println("number of nodes in the tree is "+b.countnodes()); break; case 4:System.out.println("inorder traversal"); b.inorder(); System.out.println(); break; case 5:System.out.println("preorder traversal"); b.preorder(); System.out.println(); break; case 6:System.out.println("postorder traversal"); b.postorder(); System.out.println(); break; case 7:System.out.println("enter number to be searched"); int k1=Integer.parseInt(ob.readLine()); boolean a=b.search(k1); if(a==true) System.out.println("found"); else System.out.println("not found"); break;

Page 85: 12 programs.docx

} }while(ch<8); }}

//program to calculate sum of list elements, count number of nodes in the list and print the list using recursive functionsimport java.io.*;class node //implements node{ int data; node link; node() //default constructor { data=0; link=null; } node(int d,node l) //parametrized constructor { data=d;link=l; } void setlink(node n) //stores paased reference in link part of node {

Page 86: 12 programs.docx

link=n; } node getlink() //returns the link part of node { return link; } int getdata() //returns the data part of node { return data; }}class linkedlist{ node start; linkedlist() //default constructor { start=null; } void insert(int val) //insert node in the linked list { node nptr=new node(val,null); if(start==null) start=nptr; else if(val<=start.getdata()) //to insert node in the beginning { nptr.setlink(start); start=nptr; } else { node save=start; node ptr=start.getlink(); int f=0; while(ptr!=null) //to insert node in middle { if(val>=save.getdata() && val<=ptr.getdata()) { save.setlink(nptr); nptr.setlink(ptr); f=1; break; } else { save=ptr; ptr=ptr.getlink(); } }

Page 87: 12 programs.docx

if(f==0) //to insert in the end of the list save.setlink(nptr); } } void print_num() { print_num(start); } void print_num(node head) //to display numbers in list using recursion { if(head!=null) { System.out.print(head.getdata()+" "); print_num(head.getlink()); } } int sum_list() { return sum_list(start); } int sum_list(node head) //to find the sum of numbers in the list using recursion { if(head==null) return 0; else return head.getdata()+sum_list(head.getlink()); } int number() { return number(start); } int number(node head) //to find the number of nodes using recursion { if(head.getlink()==null) return 1; else return 1+number(head.getlink()); }}public class recursive_linked_list{ static linkedlist s=new linkedlist(); public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter 5 numbers"); for(int i=0;i<5;i++) {

Page 88: 12 programs.docx

int n=Integer.parseInt(ob.readLine()); s.insert(n); } System.out.println("list is"); s.print_num(); System.out.println("number of nodes in list is "+s.number()+" and their sum is "+s.sum_list()); }}

Page 89: 12 programs.docx

//WRITE A PROGRAM TO FIND SADDLE POINT OF A 2DIMENSIONAL ARRAY( saddle point is the point wher the sum of row and collumn values is equal)import java.io.*;public class saddle_point{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[3][3];//TO DECLARE AN 2D ARRAY //LOOPS FOR INPUTING NUMBERS IN THE ARRAY for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print("enter number"); a[i][j]=Integer.parseInt(ob.readLine()); } } //LOOPS TO FIND SADDLE POINT OF THE ARRAY int c=0; for(int i=0;i<3;i++) { int small=a[i][0]; for(int j=1;j<3;j++) { if(small>a[i][j]) { small=a[i][j]; c=j; } } int big=0; for(int k=0;k<3;k++) { if(big<a[k][c]) big=a[k][c]; } if(big==small) { System.out.print(big +" is a saddle point of array");//TO PRINT SADDLE POINT OF THE ARRAY break; } }}

Page 90: 12 programs.docx

}

//write a program to input names, contact numbers and salary of employees of a company in a file salary.txt .copy the details of the employees having salary>50000 in another file high_salary.txt.import java.io.*;public class salary_writer{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter the number of employees"); int n=Integer.parseInt(ob.readLine()); FileWriter f=new FileWriter("salary.txt"); //object of FileWriter class to create file salary BufferedWriter b=new BufferedWriter(f); //object of BufferedWriter class PrintWriter p=new PrintWriter(b); //object of PrintWriter class FileWriter f1=new FileWriter("high_salary.txt"); //object of FileWriter class to create file high_salary BufferedWriter b1=new BufferedWriter(f1); //object of BufferedWriter class PrintWriter p1=new PrintWriter(b1); for(int i=0;i<n;i++) { System.out.println("enter name, contact number and salary of the employee"); String s=ob.readLine(); String c=ob.readLine(); int sa=Integer.parseInt(ob.readLine());

Page 91: 12 programs.docx

p.println(s+" "+c+" "+sa); //to store details in salary.txt if(sa>50000) //to store details of employees having salary>50000 in high_salary.txt { System.out.println("copied in high_salary.txt"); p1.println(s+" "+c+" "+sa); } } p.close(); p1.close(); b.close(); b1.close(); f.close(); f1.close(); }}

Page 92: 12 programs.docx

//input a paragraph and display the number of words and vowels in each sentence import java.io.*;public class SENTENCE_COUNT_VOWORDS{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT A PARAGRAPH"); String a=ob.readLine(); int l=a.length(); String b=""; String c=""; int w=0; int v=0; for(int i=0;i<l;i++) { if((a.charAt(i)=='.')||(a.charAt(i)=='?')) //find where the sentence ends { b+=a.charAt(i); System.out.println(b); int n=b.length(); for(int j=0;j<n;j++) //to count the number of words and vowel { if((b.charAt(j)==' ')||(b.charAt(j)=='.')||(b.charAt(j)=='?')) { w++; for(int k=0;k<c.length();k++) { if((c.charAt(k)=='a')||(c.charAt(k)=='e')||(c.charAt(k)=='i')||(c.charAt(k)=='o')||(c.charAt(k)=='u')) v++; } c="";

} else c+=b.charAt(j); } b=""; System.out.println("WORDS COUNT IN THE SENTENCE="+w); w=0; } else { b+=a.charAt(i); }

Page 93: 12 programs.docx

System.out.println("VOWELS COUNT IN THE SENTENCE="+v); v=0; } }}

import java.io.*;public class SENTENCE_PALIND_REVERSE_FUNC{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT SENTENCE");

Page 94: 12 programs.docx

String a=ob.readLine(); int l=a.length(); String s=""; StringBuffer s1=new StringBuffer(); int e=0; for(int i=0;i<l;i++) { if(a.charAt(i)==' ') { s1.append(s); s1.reverse(); System.out.println(s1); String t=s1.toString(); if(t.equals(s)) e++; s=""; t=""; s1.delete(0,s1.length()); } else s+=a.charAt(i); } System.out.print(e); } }

Page 95: 12 programs.docx

//input a sentence and count the number of words which are palindromesimport java.io.*;public class SENTENCE_PALINDROME_SIMPLE{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT SENTENCE"); String a=ob.readLine(); int l=a.length(); String s=""; String t=""; int e=0; for(int i=0;i<l;i++) { if(a.charAt(i)==' ') { int b=s.length(); for(int j=b-1;j>=0;j--) //to check if the extracted word is a palindrome { t+=s.charAt(j); } if((t.equals(s))==true) //to count the number of palindromes { e++; s=""; t=""; } } else s+=a.charAt(i); } System.out.print("NUMBER OF PALIBNDROMES IN SENTENCE="+e); }}

Page 96: 12 programs.docx

//to insert node at end of the listimport java.io.*;class Node{ int data; Node link; public Node() //default constructor { data=0;link=null; } public Node(int d,Node n) //parameterised constructor { data=d; link=n; } public void setlink(Node m) //stores passed reference n in link part of node { link=m; } public Node getlink() //returns link partof node { return link; } public int getdata() //return data part of node { return data; }}class linkedlist{ Node start,end; public void insert(int v) { Node np=new Node(v,null); if(start==null) { start=np; end=start;

Page 97: 12 programs.docx

} else { end.setlink(np); end=np; } } public void display() //to display the node { Node ptr=start; System.out.print(start.getdata()+"--->"); ptr=start.getlink(); do { System.out.print(ptr.getdata()+"--->"); ptr=ptr.getlink(); }while(ptr.getlink()!=null); System.out.print(ptr.getdata()+"!!!!"); //last node's info printed System.out.println(); }}public class slist1{ public static void main()throws IOException { DataInputStream ob=new DataInputStream(System.in); linkedlist l=new linkedlist(); for(int i=0;i<5;i++) { System.out.print("enter no."); int p=Integer.parseInt(ob.readLine()); l.insert(p); } l.display(); }}

Page 98: 12 programs.docx

//write a program to enter 5 nos. in a stack in the beginningimport java.io.*;class node //this class implements a node{ int data; node link;public node() //default constructor{ data=0;link=null;}public node(int d,node n) //parameterised constructor{ data=d; link=n;}public void setlink(node m) //stores passed reference n in link part of node{ link=m;}public node getlink() //returns link partof node{ return link;}public void setdata(int v) //stores passed value v in data part of node{ data=v;}public int getdata() //return data part of node{ return data;}}class linkedlist{ node start;public linkedlist(){ start=null;}public void insert(int v) //insert a node in list pointed to by start{

Page 99: 12 programs.docx

node np=new node(v,null); if(start==null) start=np; else { np.setlink(start); start=np;}}

public void display() //to display the node{ node ptr=start; System.out.print(start.getdata()+"--->"); ptr=start.getlink(); while(ptr.getlink()!=null) { System.out.print(ptr.getdata()+"--->"); ptr=ptr.getlink(); } System.out.print(ptr.getdata()+"!!!!"); //last node's info printed System.out.println();}}public class slist{ public static void main()throws IOException { DataInputStream ob=new DataInputStream(System.in); linkedlist l=new linkedlist();

for(int i=0;i<5;i++){System.out.print("enter no.");int p=Integer.parseInt(ob.readLine());l.insert(p);}l.display();}}

Page 100: 12 programs.docx

//write a program to sort boundary values of a 2-d arrayimport java.io.*;public class SORT_BOUNDARY_3X3{ public static void sort() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[3][3]; int b[]=new int[8]; int d=0; for(int i=0;i<3;i++) //to input values in a 2-d array { for(int j=0;j<3;j++) { System.out.print("INPUT NUMBER"); a[i][j]=Integer.parseInt(ob.readLine()); if((i==1)&&(j==1)) continue; else { b[d]=a[i][j]; d++; } } } for(int i=0;i<8;i++) //to sort the values in the boundary { for(int j=0;j<7-i;j++) { if(b[j]>b[j+1]) { int t=b[j]; b[j]=b[j+1]; b[j+1]=t; } } } int r=0; int c=0; for(int i=0;i<8;i++) // to put the values in sorted order at their places

Page 101: 12 programs.docx

{ if(i<3) a[r][c++]=b[i]; else if(i<5) a[++r][c-1]=b[i]; else if(i<7) { c--; a[r][c-1]=b[i]; } } a[1][0]=b[7]; for(int i=0;i<3;i++) // to display the sorted array { System.out.println(); for(int j=0;j<3;j++) { System.out.print(a[i][j]+ " "); } } }}

//input a 2-d array and sort the values on the boundary import java.io.*;public class SORT_BOUNDARY_5X5{

Page 102: 12 programs.docx

public static void sort1() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[5][5]; int b[]=new int[16]; int d=0; for(int i=0;i<5;i++) //to input values in a 2-d array { for(int j=0;j<5;j++) { System.out.print("INPUT NUMBER"); a[i][j]=Integer.parseInt(ob.readLine()); if((i==1||i==2||i==3)&&(j==1||j==2||j==3)) //to store the values on the boundary in another 1-d array { continue; } else { b[d]=a[i][j]; d++; } } } for(int i=0;i<16;i++) //to sort the values extracted { for(int j=0;j<15-i;j++) { if(b[j]>b[j+1]) { int t=b[j]; b[j]=b[j+1]; b[j+1]=t; } } } for(int j=0;j<16;j++) { System.out.println(b[j]); } }}

Page 103: 12 programs.docx

//input a sentence and arrange the letters of the word by entering the position of word in circular mannerimport java.io.*;public class Special2{String Sentence;String Word;String Newsent;int A;public Special2(){ Word=""; Newsent="";}public void input() throws IOException //to input a sentence{ DataInputStream ob=new DataInputStream(System.in); System.out.print("Input Sentence"); Sentence= ob.readLine(); Sentence+=" "; int l=Sentence.length(); int a=1; for(int i=0;i<l;i++) { if(Sentence.charAt(i)==' ') a++; } boolean t=true; while(t) { read(); if(A>a) //to check if the nmber is valid { System.out.print("Sorry! Re-enter the number"); t=true;

Page 104: 12 programs.docx

} else t=false; }}public void read() throws IOException //to input the position of word to be changed as a number{ DataInputStream ob=new DataInputStream(System.in); System.out.print("Input A"); A=Integer.parseInt(ob.readLine());}public void Circular() //to replace each letter of the chosen word by the letter that follows it alphabaetically{ String z=""; int l=Sentence.length(); int a=0; for(int i=0;i<l;i++) { if(Sentence.charAt(i)==' ') { a++; if(a==A) { int t=Word.length(); for(int j=0;j<t;j++) { char ch=Word.charAt(j); ch=(char)(ch+1); if(ch>122) ch=(char)(ch-26); z=z+ch; } Newsent+=z+" "; z=""; Word=""; } else { Newsent+=Word+" "; Word=""; } } else Word+=Sentence.charAt(i); } System.out.print(Newsent);}

Page 105: 12 programs.docx

public static void main() throws IOException //to display the sentence with replaced word{ Special2 p1=new Special2(); p1.input(); p1.Circular();}}

import java.io.*;class node{

}class linked_stack{}public class STACK_LINKED{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int i; for(int a=0;a<5;a++) { i=Integer.parseInt(ob.readLine());

Page 106: 12 programs.docx

push(i); System.out.print("PUSHED ="+i); } }}

//input a sentence and extract the words. display the words sorted (i)lexicographically (ii)lengthwiseimport java.io.*;import java.util.*;public class stringlexico{ public static void main()throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT SENTENCE"); String sentence=ob.readLine(); StringTokenizer s=new StringTokenizer(sentence); int k=s.countTokens(); String words[]=new String[k]; for(int i=0;i<k;i++) { words[i]=s.nextToken(); } alphabets(words,k); len(words,k); } public static void alphabets(String alpha[],int range) //to arrange the words alphabaetically {

Page 107: 12 programs.docx

for(int i=0;i<range;i++) { String small=alpha[i]; int pos=i; for(int j=i+1;j<range;j++) { if(alpha[j].compareTo(small)<0) { small=alpha[j]; pos=j; } } String temp=alpha[pos]; alpha[pos]=alpha[i]; alpha[i]=temp; } System.out.print("alphabatically"); for(int k=0;k<range;k++) //to display the sorted words { System.out.print(alpha[k]+" "); } System.out.println(" "); } public static void len(String alpha[],int range) //to arrange the words lengthwise { for(int i=0;i<range;i++) { String small=alpha[i]; int pos=i; for(int j=i+1;j<range;j++) { if(alpha[j].length()>small.length()) { small=alpha[j]; pos=j; } } String temp=alpha[pos]; alpha[pos]=alpha[i]; alpha[i]=temp; } System.out.print("LENGTH WISE"); for(int k=0;k<range;k++) //to display the sorted words { System.out.print(alpha[k]+" "); } System.out.println(" ");

Page 108: 12 programs.docx

}}

//input a sentence and display the number of palindromes using string tokenizerimport java.io.*;import java.util.*;public class STRTOKENPALIN{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("Enter a string "); String s=ob.readLine(); StringTokenizer s1=new StringTokenizer(s);

Page 109: 12 programs.docx

int n=s1.countTokens(); int palin=0; //to store the number of palindromes for(int i=0;i<n;i++) //to count the number of palindromes { String word=s1.nextToken(); StringBuffer w=new StringBuffer(word); w.reverse(); String j=w.toString(); if(j.equals(word)) { System.out.println(word); palin++; } } System.out.print(palin); }}

Page 110: 12 programs.docx

//WRITE A PROGRAM TO INPUT NUMBERS IN AN 2D ARRAY&STORE SUM ROW WISE IN 1D ARRAY//import java.io.*;public class SUM_ROW_WISE{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int a[][]=new int[3][3];//TO DECLARE AN 2D ARRAY// int b[]=new int[3];//TO DECLARE AN 1D ARRAY// int sum =0;//TO STORE SUM OF A ROW// //LOOPS FOR INPUTING VALUES IN ARRAY// for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print("Input Numbers a["+i+"]["+j+"]"); a[i][j]=Integer.parseInt(ob.readLine()); sum+=a[i][j]; } b[i]=sum; sum=0; } //TO DISPLAY SUM OF ROWS// for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(a[i][j]+" "); } System.out.println("SUM="+b[i]); } } }

Page 111: 12 programs.docx

//write a menu driven program to input admission number and name of students, display all the values entered, display the corresponding name for an admission number and delete the admission number and name for the respective admission number.USE NODESimport java.io.*;import java.util.Scanner;class node{ String name; int admno; node link; public node(int a,String n,node l) { admno=a; name=n; link=l; } public void setlink(node l) { link=l; } public node getlink() { return link; } public int geta() { return admno; } public String getn() { return name; }}class linkedlist{ node start=null; public void insert(int a,String n) {

Page 112: 12 programs.docx

node np=new node(a,n,null); if(start==null) { start=np; } else { np.setlink(start); start=np; } } public void display(int a) { node ptr=start; int k=0; do { int a1=ptr.geta(); if(a1==a) { System.out.println(ptr.getn()); k++; break; } else ptr=ptr.getlink(); }while(ptr!=null); if(k==0) System.out.println("not found"); } public void displayall() { node nptr=start; do { System.out.println(nptr.geta()+"\t"+nptr.getn()); nptr=nptr.getlink(); }while(nptr.getlink()!=null); System.out.println(nptr.geta()+"\t"+nptr.getn()); } public void delete(int a) { node n=start; node n1=n.getlink(); int k=0,count=0; do { int a1=n.geta();

Page 113: 12 programs.docx

count++; if(a1==a) { if(count==1) {start=start.getlink();} else {n.setlink(n1.getlink());} k++; break; } else { n=n.getlink(); n1=n.getlink(); } }while(n1!=null); if(k==0) System.out.println("not found"); else System.out.println("deleted"); }}public class test{ static linkedlist l=new linkedlist(); public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); int ch=0; do { System.out.println("menu\n1.insert\n2.display\n3.display all\n4.delete\n5.exit\nenter choice"); ch=Integer.parseInt(ob.readLine()); switch(ch) { case 1:System.out.println("enter admission number and name"); l.insert(Integer.parseInt(ob.readLine()),ob.readLine()); break; case 2:System.out.println("enter admission number"); l.display(Integer.parseInt(ob.readLine())); break; case 3:l.displayall(); break; case 4:System.out.println("enter admission number"); l.delete(Integer.parseInt(ob.readLine())); break; } }while(ch<5);

Page 114: 12 programs.docx

}}

//WRITE A PROGRAM USING FUNCTIONS TO CHECK IF TWO NUMBERS ARE TWIN PRIME OR NOTimport java.io.*;public class TWIN_PRIME{

public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); //TO INPUT 1st NUMBER System.out.print("enter number"); int a=Integer.parseInt(ob.readLine()); boolean s=is_prime(a);//TO CHECK THE INPUTED NUMBER IS A PRIME NUMBER //TO INPUT 2nd NUMBER System.out.print("enter number"); int b=Integer.parseInt(ob.readLine()); boolean p=is_prime(b);//TO CHECK THE INPUTED NUMBER IS A PRIME NUMBER

Page 115: 12 programs.docx

if(s==true&&p==true&&((a-b==2)||(b-a==2)))//TO CHECK IF THE NUMBERS ARE TWIN PRIME NUMBERS { System.out.print(" THE TWO NUMBERS "+a+b+"ARE TWIN PRIME"); } else System.out.print("NUMBERS ARE NOT TWIN PRIME"); } //FUNCTIONS TO CHECK WHETHER THE NUMBER PASSED TO IT IS A PRIME NUMBER OR NOT public static boolean is_prime(int c) { int t=0; boolean d; for(int i=1;i<=c;i++) { if(c%i==0) t++; } if(t==2) d=true; else d=false; return d; }}

Page 116: 12 programs.docx

//WRITE A PROGRAM TO INPUT TWO NUMBERS m,n & PRINT ALL THE UNIQUE NUMBERS BETWEEN THEMimport java.io.*;public class UNIQUE_NUMBER{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); //STATEMENTS TO INPUT THE NUMBERS System.out.print("input 1st number"); int m=Integer.parseInt(ob.readLine()); System.out.print("input 2nd number"); int n=Integer.parseInt(ob.readLine()); int s=0; for(int i=m;i<=n;i++)//LOOP TO GET SINGLE NUMBER TO CHECK WHETHER THE NUMBER IS THE UNIQUE NUMBER OR NOT { int f=0;//VARIABLE TO STORE THE COUNT OF DIGITS OF THE NUMBER for(int j=i;j>0;j=j/10)////LOOP TO GET THE NUMBER OF THE DIGITS IN THE NUMBER { f++; } int a[]=new int[f];//ARRAY INITIALLISED SO AS TO STORE THE DIGITS SEPERATELY IN IT for(int j=i,k=0;j>0;k++,j=j/10)//LOOP TO STORE THE DIGITS SEPERATELY IN THE ARRAY DECLARED { int b=j%10; a[k]=b;//TO STORE THE DIGIT IN THE ARRAY AT THE INDEX } int count=0; int r=0; //LOOPS TO CHECK THE NUMBER IS THE UNIQUE NUMBER OR NOT for(int t=0;t<f;t++) { int t1=a[t]; for(int z=0;z<f;z++) { if(t1==a[z])//TO CHECK WHETHER THE DIGIT IN THE VARIABLE t1 IS EQUAL TO THE OTHER DIGITS count++;//IF THE DIGITS ARE EQUAL IT WILL GIVE AN INCREASE TO COUNT } if(count==1)//IF THERE IS SINGLE OCCURANCE IN THE ARRAY OF THE DIGIT THEN IT WILL GIVE AN INCREASE TO r

Page 117: 12 programs.docx

r++; count=0; } if (r==f)//IF THE OCCURANCE OF A NUMBER INT THE ARRAY IS EQUAL TO THE SIZE OF THE ARRAY THEN IT WILL PRINT THE NUMBER { System.out.println(i);//STATEMENT TO PRINT THE NUMBER s++;//TO STORE THE COUNT OF THE UNIQUE NUMBER BETWEEN m&n } } System.out.print(s);//TO PRINT THE COUNT OF THE UNIQUE NUMBERS } }

//input an array and check if it is a unit matrix or not import java.io.*;public class UNIT_MATRIX{ //CONCEPT:count the number of 0's and 1's. if there are n 1's and rest are 0's, the matrix is a unit matrix public static void main() throws IOException

Page 118: 12 programs.docx

{ DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT OF ROW NUMBERS & COLUMN NUMBER"); int n=Integer.parseInt(ob.readLine()); int a[][]=new int[n][n]; int one=0; int zero=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) //to count the number of zeros and ones { System.out.print("INPUT NUMBER IN ARRAY"); a[i][j]=Integer.parseInt(ob.readLine()); if(a[i][j]==1) one++; else zero++; } } int d=n*n-n; if((one==n)&&(zero==d)) System.out.print("UNIT MATRIX"); else System.out.print("NOT A UNIT MATRIX"); } }

Page 119: 12 programs.docx

//WRITE A PROGRAM TO PRINT THE UPPER DIAGONAL OF THE 2D ARRAYimport java.io.*;public class upperdiagonal{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); { int a[][]=new int[3][3];//TO DECLARE AN 2D ARRAY //LOOPS TO INPUT NUMBERS IN THE ARRAY for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print("Input Numbers"); a[i][j]=Integer.parseInt(ob.readLine()); } } //LOOP TO PRINT THE UPPER DIAGONAL OF TH ARRAY for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(i<=j) { System.out.print(a[i][j]+" "); } else { System.out.print(" "); System.out.println(); } } } }

Page 120: 12 programs.docx

}}

//write a program to calculate volume and perimeter of a cuboid using inheritence where volume is the derived class and perimeter is the super class. create a class to execute the functions performed in both classesimport java.io.*;class perimeter //super class{ double a; double b; perimeter(double a1,double b1) //parametrized constructor

Page 121: 12 programs.docx

{ a=a1; b=b1; } double calculate() //to calculate the perimeter of the base { double r=2*(a+b); return r; } void show() // to display length, breadth and perimeter { System .out.println("length="+a); System.out.println("breadth"+b); double r=calculate(); System.out.println("perimeter of the base="+r); }}class volume extends perimeter //derived class{ double h; double volume; volume(double a1,double b1,double ht) // parameterized constructor { super(a1,b1); h=ht; } void dovolume() // calculate volume and call the function of super class to calculate perimeter { super.calculate(); volume=super.b*h*super.a; } void show() //to display height and area and call the function of super class to display primeter, length and breath { super.show(); System.out.println("height="+h); System.out.println("volume="+volume); }}public class volumeandperimeterinheritence{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter length, breadth, height of cuboid"); double a=Double.parseDouble(ob.readLine()); double b=Double.parseDouble(ob.readLine()); double c=Double.parseDouble(ob.readLine());

Page 122: 12 programs.docx

volume ar=new volume(a,b,c); ar.dovolume(); ar.show(); }}

//store first name, last name, address and contact numbers of volunteers in a campaign in a file volunteer.txtimport java.io.*;public class volunteer_writer{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter the numbeer of volunteer"); //to store number of volunteers int n=Integer.parseInt(ob.readLine()); FileWriter f=new FileWriter("volunteer.txt"); //object of FileWriter class to create file volunteer BufferedWriter b=new BufferedWriter(f); //object of BufferedWriter class PrintWriter p=new PrintWriter(b); //object of PrintWriter class for(int i=0;i<n;i++) {

Page 123: 12 programs.docx

System.out.println("enter first name, last name, address and contact number of the volunteer"); String nf=ob.readLine(); String nl=ob.readLine(); String ad=ob.readLine(); String nu=ob.readLine(); p.println(nf+" "+nl+" "+ad+" "+nu); //to store the details of volunteer in the file } p.close(); b.close(); f.close(); }}

//design a super class worker which stores the name and basic pay of worker. subclass wages calculates monthly wages=basic pay + (overtime hours X rate). display all the details of workerimport java.io.*;class worker //super class

Page 124: 12 programs.docx

{ String name; double basic; worker(String n,double m) //parametrized constructor { name=n; basic=m; } void display() //to display details of worker { System.out.println(name+" has basic pay Rs."+basic); }}public class wages extends worker //sub class{ int hrs; double rate; double wage; wages(String n,double b,int h,double r) //parametrized constructor { super(n,b); hrs=h; rate=r; } double overtime() //to calculate overtime { return (hrs*rate); } void display() //to display the details of worker and his total wage { super.display(); System.out.println("wages is Rs."+(super.basic+overtime())); } public static void main() throws IOException //to input details of the worker and display wage { DataInputStream ob=new DataInputStream(System.in); System.out.println("enter name, basic pay, overtime hours and rate per hour of the worker"); String n=ob.readLine(); double b=Double.parseDouble(ob.readLine()); int h=Integer.parseInt(ob.readLine()); double r=Double.parseDouble(ob.readLine()); wages ob1=new wages(n,b,h,r); ob1.display(); }}

Page 125: 12 programs.docx
Page 126: 12 programs.docx

//input a sentence and display the number of digits, letters in uppercase, lowercase and special characters using string tockenizerimport java.io.*;import java.util.*;public class WRAPPERCOUNT_typeofchar{ public static void main() throws IOException { DataInputStream ob=new DataInputStream(System.in); System.out.print("INPUT SENTENCE"); String a=ob.readLine(); StringTokenizer w=new StringTokenizer(a); int tokens=w.countTokens(); int d=0; int u=0; int l=0; int sp=0; for(int i=0;i<tokens;i++) { String word=w.nextToken(); for(int j=0;j<word.length();j++) { char b=word.charAt(j); if(Character.isDigit(b)) //to count the number of digits d++; else if(Character.isUpperCase(b)) //to count the number of letters in upper case u++; else if(Character.isLowerCase(b)) //to count the number of letters in lower case l++; else //to count the number of special characters sp++; } } System.out.print("DIGITS IN THE SENTENCE ARE"+d +"\n"); System.out.print("THE UPPER CASE LETTERS IN THE SENTENCE ARE"+u +"\n"); System.out.print("THE LOWER CASE LETTERS IN THE SENTENCE ARE"+l +"\n"); System.out.print("SPECIAL CHARACTERS IN THE SENTENCE ARE"+sp +"\n"); } }

Page 127: 12 programs.docx