PROGRAM- 1 Algorithm 01: 1.Start 2.Display “Enter the number” Int number 3.Initial variables factorial_sum=0,digit_sum=0,fact=1 4.Repeat while(number>0) Digit=number%10 Fact=1 digit_sum=digit_sum+digit for(1,2,3,……digit) fact=fact*1 factorial_sum=factorial_sum+fact number=number/10 End while 5.if factorial_sum=digit_sum Display”Entered number is a special number” 6.else Display “Entered number is not a special number” 7.Stop Question: 1|Page A special number is a number in which the sum of the factorial of each digit is equal to number itself. For example :145=1!+4!+5!=1+24+120 Design a class Special to check if a given number is a Special number. Some of the members of the class are given below. Class Name : Special Data members n :Integer Member Functions Special() :constructor to assign 0 to n Special (int) :parameterized constructor to assign a value to 'n' void sum() :calculate and display the sum of the first and last void isSpecial() :check and display if the number 'n' is a Special number Specify the class Special giving the details of the constructors, void sum() and void isSpecial() _________________________________________________________________________ _________ Answer: public class Specialnumber { int n; public void number(int i) { n=i; } public void sum() int maxdivisor=0; for(int a=n;a>0;a=a//10) { if(maxdivisor =0} maxdivisor=1; else maxdivisor"=10; } int digfirst,diglast,sum; diglast=n%10; diglast=n/maxdivisor; sum stiglaste digfirst; System.out.println("Sum of 1st and last digits is: "+sum); } Public void isSpecial() { int a=n, fact-1,sum=0,dig, while(a>0) dig-a %10; fact=1; for(int i=1;i<-dig;i++) fact*=i; 2|Page sum+=fact; a/=10; } if(sum==n) System.out.println(" "+n+"is a Special number"); else System.out.println(" "+n+"is a not a Special number"); } } PROGRAM-2 Algorithm-2: 1. Start 3|Page 2. Display "Enter the string" String str 3. Repeat through step 6 for ch= str (1,2,........., str.length) 4. if ((ch>=’a’ and ch<=’z’)or(ch>’A’ and ch<’Z’)) c=(int)(ch+2) chr=(char)(c) 5. if ((ch>=’a’ and ch<=’z’)or(ch>’A’ and ch<’Z’)) c=(int)(ch-24) chr=(char)(c) 6. Display chr 7. Stop Question: You can encode or decode a String in many ways. One of the simplest ways is to replace each letter, by a letter at a fixed distance ahead or behind in the alphabet E.g: Given String: "COMPUTER" and encode =3, means each character moves three characters ahead. Now String "FRPSXWHU" 4|Page Write a program in java to accept a String and display the new String after encoding=2, means each character moves two character ahead. Sample Input:"ZEOLOGY" Sample Output: “BGQNQIA” Answer: import java.util.*; public class Encode { public static void main(String args[]) { Scanner in=new Scanner(System.in); int i.a. c = 0 ; String str; char=ch,chr = 0 System.out.println("Enter the String to be encoded"); Str=in nextLine(); a=str.length(); System.out.println("Encoded String"); for( i = 0; i < a ;i++) { ch=str.charAt(i); if((ch> =’a' && ch <=’x’ ) || ( ch >=’A’&& ch <=’X’) ) c=(int) (ch + 2); chr = (char)(c); if ((ch>=’y’&& ch<=’z’) || ( ch >=’Y’&&ch <=’Z’)) c=(int) (ch - 24); chr = (char)(c); System.out.println(chr); } } PROGRAM-3 Algoithm -3: 01) Start 02) Display “Enter the telephone number” long phno 03) Display “Enter the name of the customer” 5|Page String name 04) Display “Enter the total number of calls made ” Int calls 05) if(call<100) amount=500 06) or if(calls<=200) amount=(calls-100)*1+500 07) or if(calls<=300) amount=100*1+(calls-200)*1.20+500 08) or amount=100*1+100*1.20+(calls-300)*1.50+500 09) Display “Phone number” Display phno 10) Display “Name” Display name 11) Display “Total calls” Display calls 12) Display “Amount” Display amount 13) Stop Question: A class telcall calculates the monthly phone bill of a customer. Some of the members of the class are given below: Class name : telcall Data members/instance variables phno 6|Page :phone number name :name of the customer n :number of the calls made amt :amount of the bill Member functions telcall() :parameterized constructor to assign values to data members void compute :to calculate the phone bill amount base on the slabs given below void display :to display the details in the specified format Number of the calls Rate 1-100(Charge only) Rs.500/-rental 101-200(Rental charge) Rs.1.00 per call+ 201-300(Rental charge) Rs.1.20 per call+ Above 300(Rental charge) Rs.1.50 per call+ The calculations need to be done as per the slabs. Specify the class telcall, giving details of the constructor, void compute() and void display(). In the main function, create an object of type telall and display the phone bill in the following format: Phone number: XXXXXXX Name: XXXXXXX Total calls: XXX Amount: XXXX _________________________________________________________________________ _______________ Answer: public class telcall { long phno; String name; int n; double amt; public telcall(long ph,String nm,int no) { 7|Page phno=ph; name=nm; n=no; amt=0.0; } void compute() { if(n>300) amt=(1.50*n)+500.0; else if(n>200) amt=(1.20*n)+500.0; else if(n>100) amt=(1*n)+500.0; else amt=500.0; } void display() { System.out.println("Name: "+name); System.out.println("Phone number: "+phno); System.out.println("No. of calls: "+n); System.out.println("Amount to be paid: "+amt); } Public static void main() {Scanner sc=new Scanner(System.in); System.out.println(“enter phone number”); Long p=sc.nextLong();} System.out.println(“enter name”); String s=sc.nextLine(); System.out.println(“total no. of cells”); 8|Page Int c=sc.nextInt(); tellcal obj=new tellcall(p,s,c,0); obj.compute; obj.displsy(); } } PROGRAM-4 Algorithm -4: 01) Start 02) int pos =0,k=0 03) Repeat through step 09 for(i=(1,2,3,……,array length)) 04) small=array[i] 05) Repeat step 06 for(j=(1,2,3……,array length)) 06) if(array[j]<small) small=array[j] .pos j 07) temp=array[pos] 08) array[pos]=array[i] 9|Page 09) array[i]=temp 10) Stop Question: Write a program in java to perform selection sort in a given array of integers. Sample output: Initial array: 41 15 25 77 45 37 23 70 27 9 After sorting 9 15 23 25 27 41 45 70 77 Answer import java.util.*; 10 | P a g e public class selectionsort { public static void main() { Scanner sc=new Scanner(System.in); System.out.println("Enter the size of the array:"); int s=sc.nextInt(); int m[]=new int[s]; int min=0,t=0; System.out.println("Enter the elements of the array:"); for(int i=0;i<s;++i) { m[i]=sc.nextInt(); } for(int i=0;i<(s-1);++i) { min=i; for(int j=i+1;j<s;++j) { if(m[j]<m[min]) min=j; } t=m[i]; m[i]=m[min]; m[min]=t; } System.out.println("Elements of the array in Ascending order:"); for(int i=0;i<s;++i) { System.out.println(m[i]); 11 | P a g e } } } PROGRAM-5 Algorithm 05: 1.Start 2. String a="",st="" int k=0,x=0 3. Display("Enter the string") String name 4. Assign name to StringTokenizer object s 5.x= no of tokens 6.st=first token 7. Repeat through step 6 for m=(1,2....x) 12 | P a g e 8.a=s.nextToken() 6.if (length of st<length of a)(ignore case) st=a 7.k=st.length() 8.Display k Display ("the longest word is; "+st) 9.Stop 13 | P a g e Question: Write a program in java to accept a String and display and longest word of the String. Sample Input: YOU ARE APPEARING FOR ISC BOARD EXAMINATIONS 2020. Sample Output: The longest word is EXAMINATIONS. ______________________________________________________________________________ _____ Answer: import java.util.*; public class Longest { public static void main(String args[]) { Scanner in=new Scanner(System.in); int x=0,m,k=0,p; String a="",name,st; char ch; System.out.println("Enter your String"); name=in.nextLine(); StringTokenizer s=new StringTokenizer(name); x=s.countTokens(); st=s.nextToken(); for(m=1;m<x;m++) { a=s.nextToken(); if(st.compareToIgnoreCase(a)<0) st=a;} k=st.length(); System.out.println("The length of the longest word is "+k); System.out.println("The longest word of the String is:"+st); } } 14 | P a g e PROGRAM-6 Algorithm -6: 1.Start 2.Define array a[] with capacity 20. int ptr,temp,n,i 3.[store a highly negative number in the initial cell) a[0]=-999 4.accept number of values for array less than 20 5.[sort the elements] Repeat through step for (1,2,3,.....n) 4.[initialize temporary variables) ptr=i-1 temp=a[i] 5.[compare the elements backward] Repeat while(a[ptr>temp) a[ptr+1]= a[ptr] ptr=ptr-1 6.[insert the element] a[ptr+1]=temp 7. Display sorted array 8.Stop Question: 15 | P a g e Program in Java to perform insertion sort on a given array of integers Enter size of the array: 5 Enter array elements 3 2 5 4 1 Sample Output: Elements in ascending order: 12345 _________________________________________________________________________ _______________ Answer: import java.util.*; public class InsertionSort { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int a[]=new int[20]; int ptr,temp,n; int i; System.out.println("Enter size of array less than 20:"); n=sc.nextInt(); System.out.println("Enter array elements"); for(i=0;i<n;i++) a[i]=sc.nextInt(); for(i=1;i<n;i++) { temp=a[i]; 16 | P a g e ptr=i-1; while(a[ptr]>temp) a[ptr+1]=a[ptr]; ptr--; } a[ptr+1]=temp; System.out.println("Elements in ascending order"); for(i=1;i<n;i++) System.out.print(a[i]+" "); } } PROGRAM-7 Algorithm: 1.Start 17 | P a g e 2.int i,j,t=0 Int num[]=new int[10] 3.Accept numbers for array 4.Repeat through step 9 for i=(0,1,2....8) 5.Repeat through step 6 for j=(0,1,2....8-i) 6.if(num[j]<num[j+1]) 7.temp=num[i] 8.num[i]=num[i+1] 9.num[i+1]=temp 10.Display sorted array 11.Stop Question: Write a program in java to perform bubble sort on the given set of integers. Sample input: Initial array:10 9 8 5 6 7 4 3 2 1 18 | P a g e Sample output: 1 2 3 4 5 6 7 8 9 10 _________________________________________________________________________ _______________ Answer: import java.util.*; Class bubblesort { public static void main(String args[]) { Scanner in=new Scanner(System.in); int i,j,t=0; int num[]=new int[10]; System.out.println(“Enter the numbers”); for(i=0;i<10;i++) num[i]=in.nextInt(); for(i=0;i<=9;i++) { for(j=0;j<=(9-1);j++) { if(num[j]>num[j+1] { t=num[j]; num[j]=num[j+1]; num[j+1]=t; } } } System.out.println(“The number in ascending order”); for(i=0;i<10;i++) System.out.print(num[i]+” “); 19 | P a g e } } PROGRAM-8 Algorithm: 1.Start 2.QuickSort(arr,left,right) 3.if left>right Q=partition(arr,left,right) 20 | P a g e 4.QuickSort(arr,left,q-1) 5.QuickSort(arr,q+1,right) 6.partition(arr,left,right) 7.x=arr[right] 8.i=left-1 9.j=right+1 10.while repeat=j-1 until arr[i]>=x 11. If(i>j) Swap arr[i] and arr[right] 12.else Swap arr[i] and arr[left] 13.Return j 14.Stop Question: Write a program in java to perform quick sort on a given array of integers. Sample input: Initial array:10 9 8 5 6 7 4 3 2 1 Sample output: 1 2 3 4 5 6 7 8 9 10 _________________________________________________________________________ ___________ 21 | P a g e Answer: public class QuickSort { int pivot,left1,right1; public void qSort(int array[],int left,int right) { left1=left; right1=right; pivot=array[] while(left<right) { while((array[right]>=pivot)&&(left<right)) right--; I if(left!=right) { array[left]=array[right]; left++; } While((array[left]<=pivot)&&(left<right)) left++; if(left!=right) { array[right]=array[left]; right--; } } array[left]=pivot; pivot=left; left=left1; 22 | P a g e right=right1; if(left<pivot) qSort(array,pivot+1,right); } public static void main(String args[]) { int array[]={6,3,5,4,9,2,7}; int left=0,arraySize=7,right=arraySize-1; System.out.println(“Original is:”); for(int i=0;i<arraySize;i++) { System.out.println(array[i]+””); } QuickSort ob=new QuickSort(); ob.qSort(array,left,right); System.out.println(); System.out.println(“Sorted array is:”); for(int i=0;i<arraySize;i++) System.out.println(array[i]+””); } } PROGRAM-9 Algorithm: 1. Start 2. int flag=0 3. Repeat step 4 for i= (1, 2, 3, 4,…….,array.length) 4. if search_item=array[i] flag=1 5. if flag=1 Display "Search successful" 6. else Display "Search unsuccessful" 7. Stop 23 | P a g e Question Write a program in java to store 5 different numbers along with corresponding telephone numbers. Enter the numbers from the console and search whether the number is present or not. If the number is present then display an appropriate message using Linear Search technique. Sample Input: Enter the number: 15 25 4577 Enter the number to be searched: 4 Sample Output: The number 4 is present. _________________________________________________________________________ ____________ 24 | P a g e Answer: import java.util.*; public class LinearSearch { public static void main() { Scanner sc=new Scanner(System.in); int i,a,flag=0,ns=0; int m[]=new int[10]; System.out.println("Enter the numbers in each cell"); for(i=0;i<10;i++) { m[i]=sc.nextInt(); } System.out.println("Enter number to be searched"); ns=sc.nextInt(); for(i=0;i<10;i++) { if(m[i]==ns) { flag=1; break; } } if(flag==1) System.out.println("Search successful"); else System.out.println("Search unsuccessful "); } } 25 | P a g e PROGRAM-10 Algorithm: 1. Start 2. int first= 0, last= array.length-1,mid=(first+last)/2 3. Repeat through step 5 unless last< first 4. if array[mid]>search_item Last= mid-1 First=0 Mid=(first+last)/2 5. else First=mid+1 Last=array.length-1 Mid=(first+last)/2 6. if last> first Display "Search successful 7. else Display "Search unsuccessful" 26 | P a g e 8. Stop Question: Write a program in java to perform binary search on a given array of integers. Sample Input: Enter the number to be searched: 6 Sample Output: Number found Answer: import java.util.*; public class BinarySearch { public static void main() { int i,ns,k=0,p=0,lb=0,ub=9; Scanner sc =new Scanner(System.in); int m[]=new int[10]; System.out.println("Enter the numbers"); for(i=0;i<10;i++) { m[i]=sc.nextInt(); 27 | P a g e } System.out.println("Enter the number to be searched"); ns=sc.nextInt(); while(lb<=ub) { p=(lb+ub)/2; if(m[p]<ns) lb=p+1; if(m[p]>ns) ub=p-1; if(m[p]==ns) { k=1; break; }} if(k==1) System.out.println("Search successful"); else System.out.println("Search unsuccessful"); } } PROGRAM-11 Algorithm: 1. Start 2. Accept (“Size of an array”,n) 3. A[][] = new int[n][n] K = n*n; row1 = column1 =0; row2 = column2 = n-1; 4. Repeat 5 to 9, until(k>=1) 5. Repeat for (i=column2;i>=column1;i--) A[row1][i] = k, k 6. Repeat for(j=row1+1 ; j<=row2; j++) A[j][column2] = k, k 7. Repeat for(i=column1 + 1; i<=column2; i++) A[row2][i] = k, k – 8. Repeat for(j = row2 – 1; j>=row1; j--) A[j][column2] = k, k9. row1++ ; column1++ ; row2-- ; column2— 10. Stop 28 | P a g e 29 | P a g e Question: Write a program in java to fill numbers in circular fashion(clock-wise) with natural numbers from 1 to n*n, taking n as input .Input n is an odd number and element start filling from the central cell. E.g: if n =5 ,then n^2 = 25, the array is filled as: 21 20 19 18 17 22 7 6 5 16 23 8 1 4 15 24 9 2 3 14 25 10 11 12 13 _____________________________________________________________________ Answer: import java.util.*; public class ClockwiseInOut { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n,r1,r2,c1,c2,k=1,I,j; System.out.println(“Enter the size of the matrix”); n = in.nextInt(); int a[][] = new int[n][n]; c1 = 0;c2 = n-1;r1 = 0;r2 = n-1; do { for(i=c1;i<=c2;++i) { a[r1][i] = k; k++; 30 | P a g e } for(j=r1+1;j<=r2;j++) { a[j][c2] = k; k++; } for(i=c2-1;i>=c1;i--) { a[r2][i] = k; k++; } for(j=r2-1;j>=r1+1;j--) { a[j][c1] = k; k++; } c2--;c1++;r1++;r2--; } while(k<=n*n); System.out.println(“Circular matrix is isted below:”); for(i=0;i<n;++i) { for(j=0j<n;j++) { System.out.print(a[i][j]+”\t”); } System.outprintln(); 31 | P a g e } } } PROGRAM-12 32 | P a g e Algorithm : 1.Start 2.Implement a try block 3.Catch the Exception fi any Print(Exception e) 4.Stop 33 | P a g e Question: Write a program in java that generate exception and error handling routine. ______________________________________________________________________________ Answer: public class DivideException { public static void main(String args[]) { int result-division (100,0) System.out.println("result"+result); public static int division (int totalSum,int totalNumber) { int quotient=1; System.out.println("Computing division"); try { Quotient-totalSum/totalNumber; } catch(Exception e) { System.out.println("Exception:"+e.getMessage()); finally } If(quotient!=-1) { System.out.println("Finally block executes"); System.out.println("Result:"+quotient); } 34 | P a g e else { System.out.println("Finally block executes.Exception occured"); return quotient; } } return quotient; } } 35 | P a g e PROGRAM-13 Algorithm : 1.Start 2.Repeat step until(i=20) Accept(name,sex, age,roll-no,marks) 3. Repeat step until(i=20) print(name,sex,age, roll-no,marks) 4.Stop. 36 | P a g e Question: Write a class student to accept the details of a student as input and store them in a file using scanner class. ________________________________________________________________________________ Answer: import java.util.*; public class StudentData char[]name; char[]sex; int age; public void inputdetails() { Scanner in=new Scanner(System.in); name=new char[10]; sex=new char[8]; System.out.println("Enter the name"); String ch="";int =0; try { ch=in.next(); name=ch.toCharArray(); } catch(Exception e) { System.out.println("Error in reading in name:"+e); } System.out.println("Enter sex male/Female:"); ch="";i=0; try { ch=in.next(); 37 | P a g e sex=toCharArray(); } Catch(Exception e) { System.out.println("Error in reading sex"+e); } } System.out.println("Enter the age"); try { age=in.next(); } catch(Exeption e) { System.out.println("Error in reading age"+e); } } public void show1() { System.out.print("Name"); for(int i=0;i<name.length;i++) System.out.print(name[i]); System.out.println(); System.out.print("Sex"); for(int i=0;i<sex.length;i++) System.out.print(sex[i]); System.out.println(); System.out.println("Age:"age); } PROGRAM-14 Algorithm : 38 | P a g e 1. Start 2. Accept the values m = number of rows n = number of columns 3. Declare the arrays intA[][] = new int[m][n]; int B[][] = new int[m][n]; 4. Accept the array Repeat step until (i=m) Repeat step until (i=n) 5. Create image Repeat step until (i=m) Repeat step until (i=n) k = n; B[i][j] = A[i][k] k--; Stop. 39 | P a g e Question: Write a program in java to enter natural numbers in a double dimensional array m*n(where m is the number of rows and n is the number of columns) Display the new matrix in such a way that the new matrix is the mirror image of the original matrix. Answer: import java.util.*; public class MirrorMatrix public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println(“The size of the Matrix”); int n = in.nextInt(); int a[][] = new int[n][n]; int t,I,j; for(i=0;i<n;++i) { for(j=0;j<n;j++) { System.out.println(“Enter the elements”); a[i][j] = in.nextInt(); } } for(j=0;j<n;j++) { for(j=0;j<n;j++) { System.out.print(a[i][j]+ “ “); } System.out.println(); } System.out.println(“The matrix is”); 40 | P a g e for(i=0;i<n;i++) { for(j=n-1;j<=0;j--) { System.out.print(a[i][j]+ “ “); } System.out.println(); } } } } Program- 15 41 | P a g e Algorithm : 1.Start 2.Accept the values of m and n 3.if(n=1) Return m Else return(m*function(m,n-1)) 4.Stop 42 | P a g e Question: Write a program in java to demonstrate the use of recursion in finding power. Eg:a^b _________________________________________________________________________ _______________ Answer: import java.util.*; class PowerRecursion { public static double power(double n,double p) { if (p==0) return(1); else { return(n*power(n,p-1)); } } public static void main() { Scanner sc=new Scanner(System.in); double num,po,res; System.out.println("Enter the base value"); num=sc.nextDouble(); System.out.println("Enter power"); po=sc.nextDouble(); res=power(num,po); System.out.println("Value is "+res); } } Program -16: 43 | P a g e Algorithm: 1.Start 2.accept String str 3.str.length(); 4.Displaying all possible combinations Repeat next step until i=1 Repeat next step until j=1 Repeat next step until k=1 Repeat through the following 4 steps until(ij)&(j==k)&(kli) Dispay(char)str.charAt(i) Dispay(char)str.charAt(j) Dispay(char)str.charAt(k) 5.Stop. Question: 44 | P a g e Write a program in java to accept any three letter word and print all the probable three letter combinations. No letter should be repeated within the output. _________________________________________________________________________ _______________ Answer: import java.util.*; public class Combinations { public static void main() { int i, j, k, l; String s; Scanner sc = new Scanner(System.in); System.out.println("Enter a 3 letter word"); s = sc.nextLine(); l = s.length(); System.out.println("the combinations are"); for (i = 0; i < l; i++) { for (j = 0; j < l; j++) { for (k = 0; k < l; k++) { if (i != j && j != k && k != i) System.out.println(s.charAt(i) + "" + s.charAt(j) + "" + s.charAt(k)); } } } } } PROGRAM-17 Algorithm: 45 | P a g e 1. Start 2. Accept the value of n and base 3. if(n<0) 4. else. Call function(n,base) 5. Repeat until n>=base Call function(n/base,base) 6. Stop Question: Write a program in java with a recursive method for converting a decimal number into any other base. Sample Input: 46 | P a g e Enter a decimal number: 25 Sample Output: Binary equivalent: 11001 _________________________________________________________________________ ______________ Answer: import java.util.*; public class DecimalToBinary { public static int rec(int n,String s) { if(n==0) return (Integer.parseInt(s)); else s=Integer.toString(n%2)+s; return(rec(n/2,s)); } public static void main() { Scanner sc=new Scanner(System.in); System.out.println("Enter a decimal number"); int num=sc.nextInt(); int res=rec(num,""); System.out.println("Binary equivalent:"+res); } } PROGRAM-18 Algorithm 18 Insertion 1. [Create the first list of the structure] 47 | P a g e Accept(temp-data) temp.link=NULL 2. temp.link-start 3. Start-temp temp = NULL Deletion 1. ptr-start ptr1=start 2. count=0 3. Repeat while(count<n) ptr1=ptr ptr ptr.link Count=1 EndWhile 1. ptr1-link=ptr.link ptr.link=NULL 2. ptr=NULL ptr1=NULL Question: Write a program to create a class to operate the following functions on all linked list. -Create a linked list - Insert a list - Delete a list 48 | P a g e _________________________________________________________________________ Answer: Import java.util.*; import java.io.*; class node1 { int data; node1 link; node1(); Data=0; link=null; } void create() throws IOException { int n; InputStreamReader read=new InputStream(System.in); Buffered Reader in=new BufferedReader(read); System.out.println("Enter the first data"); this.data=Integer.parseInt(in.readLine()); System.out.println("the number of the nodes to be created"); n=Integer.parseInt(in.readLine()); node1 = temp; node ptr=this; for(int i=1 ; i<n ; i++) temp=new node1(); System.out.println("Enter the next data”); temp.data=Integer.parseInt(in.readLine()); temp.link=null; ptr.link=null; 49 | P a g e temp=null; ptr ptr.link; } void insert(node1, start,int x) ( Node1 temp=new node1(); System.out.println("Enter element for the new list"); temp.data=x; temp.link=null; temp.link=start; start=temp; temp=null; } void delete (node1, start ,int n) { node1 ptr=start; node1 ptr1=ptr; int c=0; while(c<=0) ptr1=ptr: ptr= ptr.link: ptr1.link=ptr.link; ptr.link=null; ptr= ptr1=null; } void display() { Node1 ptr=this; while(ptr1!=null) { 50 | P a g e System.out.printlin (ptr.data); ptr=ptr.link; } } } public class xyz { public static void main(String args)throws IOException { int k,p,ch; InputStreamReader read-new InputStreamReader(System.in); BufferReader br=new BufferReader(read); node1 first=new node1(); node1 abc=new node1(); do { System.out.println("Main Menu"); System.out.println("1. Create a linked list structure"); System.out.println("2. Insert a list"); System.out.println("3. Delete a list"); System.out.println("4. Exit"); System.out.println("Enter your choice"); ch=Integer.parseInt(in.readLine()); switch(ch) { Case 1: first.create(); break: Case 2: 51 | P a g e System.out.println("Enter the data for the new node"); k=Integer.parseInt((in.readLine()); first.insert(first,k); break; Case 3: System.out.println("Enter the node number to be deleted"); k= Integer.parseInt(in.readLine(): first delete(first,k); break; while(ch!-3); } } } } PROGRAM 19 : Algorithm : Creation 1.(Create the first list of the structure) 2.ptr=start 3.Accept("No. 01 nodes",n) Count=0 4.Repeat through steps 5 & 6 while(c<n) 5.Accept(temp , data) 52 | P a g e 6.temp.link=NULL ptr.link=NULL Ptr=ptr.link C=C+1 Temp=NULL 7.ptr-NULL Question: Write a class to create a linked list and display it. ______________________________________________________________________________ Answer: import java.io.*; class Node { int data; Node link; 53 | P a g e Node() { data = 0; link = null; } } public class LinkedListExample { Node first; void create() throws IOException { int n; InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); System.out.println("Enter the first data"); first = new Node(); first.data = Integer.parseInt(in.readLine()); System.out.println("Enter the number of nodes to be created"); n = Integer.parseInt(in.readLine()); Node temp; Node ptr = first; for (int i = 1; i < n; i++) { temp = new Node(); System.out.println("Enter next Data"); temp.data = Integer.parseInt(in.readLine()); temp.link = null; ptr.link = temp; 54 | P a g e ptr = ptr.link; } } void display() { Node ptr = first; while (ptr != null) { System.out.println(ptr.data); ptr = ptr.link; } } } PROGRAM-20 Algorithm: 1. Start 2. Accept("Array Size",n) 3. Accept("Position of deletion") 4. Repeat through step 5 for(i=pos,pos+1,pos+2......n) 5. arr[i-1]=arr[i] n-6. Stop 55 | P a g e Question: Write a program to show deletion in an array. _________________________________________________________________________ _______________ Answer: import java.util"; public class Deletion ( public static void main(String args[]) ( int arr[]=new int [20]; Scanner in=new Scanner(System.in); System.out.println("Enter array size below 20"); 56 | P a g e int n=in.nextInt(); System.out.println("Enter the postion of the deletion"); int pos=in.nextInt(); System.out.println("Enter Array Elements"); for(int i=0; i<n ; i++) arr[i]-in.nextInt(); for(int j-pos;j<n; j++) arr[j-1]=arr[j]; System.out.println("Array elements after deletion"); for(int i=0;i<n;i++) System.out.println(arr[i]); } } PROGRAM 21: Agorithm: 1.Start 2.Accept(" Array size",n) 3.Repeat through step 4 to 10 for i=(0,1,2,3.....n) 4 min=a[i][0] c=0 S. Repeat through step 6 for j=(0,1,2,3 n) 6.if(a[i][j]<min) min=a[i][j] c=j 7.max=a[0][c] 8. Repeat through step 9 for k=(0,1,2,3.....n) 9.if a[k][c]>max 57 | P a g e max=a[k][c] 10.if (max==min) Display("Saddle point is"+max) 11.Stop. 58 | P a g e Question: Write a program in java to find the saddle point of a matrix. Sample output: Enter the Size of the array 03 Enter the elements 123456789 Original array 1 2 3 456 789 Saddle point of the matrix=7 _________________________________________________________________________________ Answer: import java.util.*; public class SaddlePointofMatrix { public static void main(String args[]) { int max,min,c=0,f=0; Scanner in=new Scanner(System.in); System.out.println("Enter the size of the array"); int n=in.nextInt(); int a[][]=new int[n][n]; max=a[0][0]; min=a[0][0]; System.out.println("Enter elements"); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { a[i][j]=in.nextInt(); } 59 | P a g e } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { System.out.print(a[i]+"\t"); } System.out.println(); } for(int i=0;i<n;i++) { min=a[i][0]; c=0; for(int j=0;j<n; j++) { if(a[i][j]<min) min=a[i][j]; c=j; } } max=a[0][c]; for(int k=0;k<n;k++) { if(a[k][c]>max) max=a[k][c]; if(max==min) { System.out.println("Saddle point of the matrix :"+max); f=1; } } 60 | P a g e if (f==0) { System.out.println("No saddle point in this matrix"); } } } PROGRAM-22 Algorithm: 1.Start 61 | P a g e 2. Accept(“Size of the array”,n) 3. a[][]=int[n][n] k=1; row1=column1=0 row2=column2=n-1 4.repeat through 5 to 9,until(k<=n*n) 5.repeat for(i=row1;i<row2;i++) a[i][column1]=k k++ 6.Repeat for(j=column1+1;j<column2;j++) a[row2][j]=k k++ 7.Repeat for(i=row2-1;i<row1;i--) a[i][column2]=k k++ 8.Repeat for(j=column2-1;j<column1+1;j--) a[row1][j]=k k++ 9.row1++ column1++ row2— column2— 10.stop. Question: Write a program to fill an array in clockwise out>in fashion 1 62 | P a g e 2 3 8 9 4 7 6 5 Sample Input: 3 Sample Output: 1 2 3 8 9 4 7 6 5 _________________________________________________________________________ _______________ Answer: import java.io.* public class CircularMatrixClockOutIn { public static void main(String args[])throws IOException { InputStreamReader read=new InputStreamReader(System.in); BufferedReader in=new BufferedReader(read); c1=0,c2=n-1,r1=0;r2=n-1; int n,r1,r2,c1,c2,I,j,k=1; System.out.println(“Enter the size of the matrix”); n=integer.parseInt(in.readLine()); int a[][]=new int[n][n]; do { for(i=c1;i<=c2;i++) { a[r1][i]=k;k++; } for(j=r1+1;j<=r2;j++) 63 | P a g e { a[j][c2]=k;k++; } for(i=c2-1;i>=c1;i--) { a[r2][i]=k;k++; } for(j=r2-1;j<=r1+1;j--) { a[j][c1]=k;k++; } c1++,c2--,r1++,r2--; } while(k<=n*n); System.out.println(“Matrix”); for(i=0;i<n;i++) { for(j=0;j<n;j++) { System.out.print(a[i][j]+”\t”); } System.out.println(); } } } PROGRAM-23 Algorithm: Insertion (Add job) 01. NewPtr=new node 02. NewPtr.data=Item 64 | P a g e NewPtr.link=NULL 03. if(rear=NULL)then front=NewPtr rear=NewPtr 04. else rear.link=NewPtr rear=NewPtr Deletion (Remove job) 01. if(front=NULL)then Print("Queue Empty") 02. else Item=front.data 03. if(front-rear) then front = NULL rear=NULL 04. else front-front++ Question: NIC institute's resource manager has decided to network the computer resources like printer, storage media,etc. So that minimum resources and maximum sharing could be availed. Accordingly printers are linked to a centralized system and the printing jobs are done on a 'first come first served' basis only. This is like the first person's printing job will get done first and the next person's job will be done as the next job in the list and so on. In order to avoid collision, the restriction is that no more that 20 printing jobs can be added. 65 | P a g e Specify the class Printjob giving details of the constructor and the function void addJob(), void createJob() and void removeJob() only. You don't need to write the main function. _________________________________________________________________________ ______________ Answer: public class PrintJob { private int job[]; private int front; private int rear; private int newjob ,capacity; public PrintJob() { capacity=20; front=-1 rear=-1; createjob(); } public void createjob() { job=new int[capacity]; } public void addjob(int j) ( if(rear=capacity-1) ( System.out.println("Printjob is full, cannot add any more"); } else if(rear==-1) {front=0; 66 | P a g e rear=0; } else rear++; job[rear] =j; } } public void removejob() { if(rear==-1) System.out.println("Printjob is empty"); else if(rear==front) ( System.out.println(job[front]); rear=-1 front=-1; } else { System.out.println(job[front++]); } } } } PROGRAM-24 Algorithm : Insertion 01. if(r=n) Display(“Queue overflows”); 67 | P a g e 02. if(f=NULL and r=NULL) f=0 and r=0 03. else r=r+1 04. q[r]=ITEM Deletion 01. if(f=NULL and r=NULL) Display(“Queue underflows”) 02. Return val=q[f] 03.Display(“Element deleted is”,val) 04. if(f=r) f=NULL and r=NULL 05. else f=f+1 Question: Write a program to implement insertion and deletion in a queue ______________________________________________________________________ _____________ Answer: public class queue { 68 | P a g e int q[]; int f,r,size,val; queue(int n) { q=new int[n]; } void insertqueue(int item) { if(r==(size-1)) { System.out.println(“Queue overflows”); } else { if(f==-1&&r==-1) { f=0; r=0; } else r=r+1; q[r]=item; } } void deletequeue() { if(f==-1&&r==-1) System.out.println(“Queue underflows”); else { val=q[f] 69 | P a g e if(f==r) { f=-1; r=-1; } else f=f+1; } } void display() { System.out.println(“Elements of the queue”); for(int j=f;j<=r;j++) System.out.println(q[j]); } } PROGRAM-25 Algorithm : Pushdata() 01. If(sp==(n-1)) Display(“Stack Overflows”) 02. else sp++ 70 | P a g e s[sp]=item Popdata() 01. if(sp==-1) Display (“Stack Overflows”) 02. Else v=s[sp] Display(“Popped out elements is ”+ v) sp-- Question: Write a program to perform push and pop operations in a stack. ______________________________________________________________________ _____________ Answer: Class Stackop { int s[]=new int[20]; 71 | P a g e int sp,n; Stackop(int nn) { for(int i=0;i<20;i++) s[i]=0; sp=-1; n=nn; } void pushdata(int item) { if(sp==(n-1)) System.out.println(“Stack Overflows”); else { sp++; s[sp]=item; } } void popdata() { int v; if(sp==-1) System.out.println(“Stack underflows”); else { v=s[sp]; System.out.println(“Popped out element is”+v); sp--; } } 72 | P a g e void display() { if(sp==-1) System.out.println(“Stack empty”); else { System.out.println(“SP ”+ s[sp]); System.out.println(); for(int i=sp-1;i>=0;i--) { System.out.println(s[i]); System.out.println(); } } } } 73 | P a g e