Sie sind auf Seite 1von 89

# Page |1 QUESTION 1 : Program to accept the amount from the user and display the break-up in descending Order

of denominations. (i.e. preference should be given to the highest denomination available) along with the total number of notes. The available denomination in the Bank are of rupees 1000, 500, 100, 50,20,10,5,2 and 1. Also print the amount in words according to the digits.

import java.io.*; class Bank { int rev=0,amount,dummy; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public void intake() throws IOException { System.out.println("Enter the Amount:"); amount=Integer.parseInt(br.readLine()); if(amount >99999) { System.out.println("Invalid Amount..."); return; } dummy=amount; while(dummy >0) { rev=rev*10+dummy%10; dummy=dummy/10; } System.out.print("Amount in words :"); while(rev >0)

Page |2 { switch(rev%10) { case 0: System.out.print(" ZERO"); break; case 1: System.out.print(" ONE"); break; case 2: System.out.print(" TWO"); break; case 3: System.out.print(" THREE"); break; case 4: System.out.print(" FOUR"); break; case 5: System.out.print(" FIVE"); break; case 6: System.out.print(" SIX"); break; case 7: System.out.print(" SEVEN"); break;

Page |3 case 8: System.out.print(" EIGHT"); break; case 9: System.out.print(" NINE"); break; } rev=rev/10; } //SPLITTING INTO SMALLER DENOMINATIONS System.out.println("\nDENOMINATORS:\n"); rev=amount/1000; if(rev!=0) System.out.println("1000 X " + rev + " = " + rev*1000); amount=amount%1000; rev=amount/500; if(rev!=0) System.out.println("500 X " + rev + " = " + rev*500); amount=amount%500; rev=amount/100; if(rev!=0) System.out.println("100 X " + rev + " = " + rev*100); amount=amount%100; rev=amount/50; if(rev!=0) System.out.println("50 X " + rev + " = " + rev*50); amount=amount%50;

Page |4 rev=amount/20; if(rev!=0) System.out.println("20 X " + rev + " = " + rev*20); amount=amount%20; rev=amount/10; if(rev!=0) System.out.println("10 X " + rev + " = " + rev*10); amount=amount%10; rev=amount/5; if(rev!=0) System.out.println("5 X " + rev + " = " + rev*5); amount=amount%5; rev=amount/2; if(rev!=0) System.out.println("2 X " + rev + " = " + rev*2); amount=amount%2; rev=amount/1; if(rev!=0) System.out.println("1 X " + rev + " = " + rev*1); } }

Page |5 QUESTION 2 Program to accept number of telephone calls made and other details and calculates total amount and prints all relevant details. Amount calculation is as per the following table:

Number of calls

Cost

## < 100 101-200 201-300 > 300

Rs. 500 Rs. 500 + Rs. 1 per extra calls over 100 Rs. 600 + Rs. 1.2 per extra calls over 200 Rs. 720 + Rs. 1.5 per extra calls over 300

## Telcall(String ph,String na,int nc) { phno=ph; name=na; n=nc; amt=0.00; }

void compute()

Page |6 { //CALCULATING BILL BASED ON THE GIVEN SLAB SYSTEM if(n<=100) amt=500.0; else if(n>100&&n<=200) amt=500+n-100; else if(n>200&&n<=300) amt=500+100+(n-300)*1.2; else amt=500+100+120+(n-300)*1.5; }

## public static void main() { Telcall t1=new Telcall("2706895","Balu",330); t1.compute(); t1.dispdata(); } }

Page |7 QUESTION 3 Program to add two given times and display it in the hour:minute:second format

import java.io.*; class Time_Calculate { int hrs,min,sec; Time_Calculate(int h,int m,int s) { hrs=h; min=m; sec=s; } static void add(Time_Calculate t1,Time_Calculate t2) { int toth,totm,tots; //ADDING TWO TIME OBJECTS tots=t1.sec+t2.sec; totm=t1.min+t2.min; toth=t1.hrs+t2.hrs; if(tots>=60) { totm+=(int)(tots/60); tots=tots%60; } if(totm>=60)

Page |8 { toth+=(int)(totm/60); totm=totm%60; } System.out.println("TIME 1 : "+t1.hrs+" hrs "+t1.min+" min "+t1.sec+" sec"); System.out.println("TIME 2 : "+t2.hrs+" hrs "+t2.min+" min "+t2.sec+" sec"); System.out.println("\n\nFINAL TIME : "+toth+" hrs "+totm+" min "+tots+" sec"); } public static void main() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter first time in order -> hr,min,sec"); int h1=Integer.parseInt(br.readLine()); int m1=Integer.parseInt(br.readLine()); int s1=Integer.parseInt(br.readLine()); System.out.println("Enter second time in order -> hr,min,sec"); int h2=Integer.parseInt(br.readLine()); int m2=Integer.parseInt(br.readLine()); int s2=Integer.parseInt(br.readLine()); Time_Calculate t1=new Time_Calculate(h1,m1,s1); Time_Calculate t2=new Time_Calculate(h2,m2,s2); add(t1,t2); } }

Page |9 QUESTION 4 : A String array of size n where n is greater than 1 and less than 10, stores single sentences (each sentence ends with a full stop) in each row of the array.Write a program to accept the size of the array. Display an appropriate message if the size is not satisfying the given condition. Define a string array of the inputted size and fill it with sentences row-wise. Change the sentence of the odd rows with an encryption of two characters ahead of the original character. Also change the sentence of the even rows by storing the sentence in reverse order. Display the encrypted sentences

import java.io.*; class Encryption { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str[],s; int i,n; public void takeString() throws Exception { System.out.println("\nHow many sentences:"); n=Integer.parseInt(br.readLine()); if(n<1 || n>9) { System.out.println("\nINVALID ENTRY:"); return; } str=new String[n]; for(i=0;i< n;i++) { System.out.println("\nEnter sentence ( ends with full stop):"); s=br.readLine().trim(); if(s.charAt(s.length()-1)!='.')

P a g e | 10 { System.out.println("\nFull stop at the end is mandatory:"); i--; continue; } str[i]=s; } for(i=0;i< n;i++) { s=str[i]; if(i%2==0) encrypt(s); else reverse(s); } } public void encrypt(String s) { int i,len; char ch; String st=""; len=s.length(); for(i=0;i< len;i++) { ch=s.charAt(i); if((ch >=65 && ch< =90)||(ch >=97 && ch< =122)) {

P a g e | 11 ch=(char)(ch+2); if(ch >90 && ch< 97) { ch=(char)((64+ch-90)); } else if(ch >122) { ch=(char)((96+ch-122)); } } st=st+ch; } System.out.println(st); }

public void reverse(String s) { int i; String s1; s=s.trim(); s=s.substring(0,s.length()-1); while(true) { i=s.lastIndexOf(" "); if(i<0) break; s1=s.substring(i).trim();

P a g e | 12 System.out.print(s1+ " "); s=s.substring(0,i).trim(); } System.out.println(s+"."); } public static void main(String args[]) throws Exception { Encryption e1=new Encryption (); E1.takeString(); } }

P a g e | 13 QUESTION 5 : Write a program to input a string and find the total number of words and the frequency of each word.

import java.io.*; class Sentence { int i,j,c; String sentence,s1; String word[]=new String[20]; int x=0; BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

public void disp()throws Exception { System.out.println("Enter the sentence:"); sentence=br.readLine(); while(true) { i=sentence.indexOf(' '); if(i<0) break; word[x++]=sentence.substring(0,i); sentence=sentence.substring(i+1); } word[x++]=sentence;

P a g e | 14 for(i=0;i< x-1;i++) { for(j=i+1;j< x;j++) { if(word[i].compareTo(word[j])>0) { s1=word[i]; word[i]=word[j]; word[j]=s1; } } } s1=word[0]; c=1; for(i=1;i< x;i++) { if(s1.equals(word[i])) c++; else { System.out.println(s1+ " frequence="+c); c=1; s1=word[i]; } } System.out.println(s1+ " frequence="+c); }

P a g e | 15

public static void main(String args[])throws Exception { Sentence ob=new Sentence(); ob.disp(); } }

P a g e | 16 QUESTION 6 : A smith number is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding 1). Check if a given number is a smith number or not.

## import java.io.*; class SmithNum {

public void takeNumber() throws Exception { //ACCEPTING THE NUMBER System.out.println("Enter the number:"); n=Integer.parseInt(br.readLine()); no=n; sum=sumOfDigits(n); for(i=1;i< =n;i++) { if(n%i==0) { b=prime(i); if(b) { //CHECKING IF THE NUMBER IS PRIME AND STORING TO A BOOLEAN VARIABLE

P a g e | 17 j=i; System.out.print(" "+j); while(j >0) { sumfactors=sumfactors+j%10; j=j/10; } n=n/i; i--; } } } if(sum==sumfactors) System.out.println(no+ " is a smith number."); else System.out.println(no+ " is not a smith number."); } private int sumOfDigits(int n) { int s=0; while(n >0) { s=s+n%10; n=n/10; } return s; }

P a g e | 18

private boolean prime(int n) { int i; for(i=2;i< n;i++) { if(n%i==0) break; } if(i==n) return true; else return false; }

public static void main()throws Exception { SmithNum s1=new SmithNum (); s1.takeNumber(); } }

P a g e | 19 QUESTION 7: Write a program to convert a decimal number to binary octal and hexadecimal systems using Recursion Technique.

import java.io.*; class RecursiveConvert { static void bin(int n) { if(n==0) return ; else { bin(n/2); System.out.print(n%2); } } static void oct(int n) { if(n==0) return ; else { oct(n/8); System.out.print(n%8); } }

P a g e | 20 static void hex(int n) { // a[] IS AN ARRAY TO STORE THE EQUIVALENT HEXADECIMALS OF 1 TO 15 String a[]={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};

## if(n==0) return ; else { hex(n/16); System.out.print(a[n%16]); } }

public static void main() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the Number"); int n=Integer.parseInt(br.readLine()); System.out.println("Number in Binary system : "); bin(n); System.out.println("\nNumber in Octal system : "); oct(n); System.out.println("\nNumber in Hexadecimal system : "); hex(n); } }

P a g e | 21 QUESTION 8 : Program to input the details of a worker and calculate overtime wage and print net salary

class Worker { String name; double basic; Worker(String n,double b) { name=n; basic=b; } void display() { System.out.println("Name : "+name); System.out.println("Basic salary : Rs. "+basic); } } class Wages extends Worker { int hrs; double rate,wage; Wages(String nm,double bas,int hr,double rt) { super(nm,bas); hrs=hr; rate=rt; //USE OF INHERITANCE

P a g e | 22 } double overtime() { return (hrs*rate); } void display() { super.display(); wage=overtime()+basic; System.out.println("Hours of work : "+hrs); System.out.println("Rate applicable : Rs. "+rate+"/hr"); System.out.println("\nTOTAL WAGE : Rs. "+wage); } } public class Worker_Inherit { public static void main() { Wages w=new Wages("Balu",5000.0,8,20.5); w.display(); } }

P a g e | 23 QUESTION 9 : Program to print all prime palindrome numbers in the given limit.

import java.io.*; class Prime_Palindrome { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int m,n; int var[]; Prime_Palindrome(int a,int b) { var=new int[3000]; if(a>3000) { System.out.println("OUT OF RANGE"); m=0; } else m=a; if(b>3000) { System.out.println("OUT OF RANGE"); n=0; } else n=b; }

P a g e | 24 boolean palindrome(int n) { int num=n,c=0; double sum=0.0,temp=n; while(num>0) { num=num/10; c++; } int r; num=n; while(num>0) { //EXTRACTING THE DIGITS AND REVERSING THE NUMBER r=num%10; sum=sum+(r*Math.pow(10,c-1)); num=num/10; c--; } if(sum==temp) return true; else return false; } boolean isPrime(int n) { int i,f=0;

P a g e | 25 for(i=1;i<=n;i++) { if(n%i==0) f++; } if(f==2) return true; else return false; } void calculate() { if(m!=0&&n!=0) { int i,c=0; for(i=m;i<=n;i++) { if((palindrome(i)==true)&&(isPrime(i)==true)) { var[c]=i; c++; } } System.out.println("THE NO. OF PRIME PALINDROME INTEGERS : "+c); System.out.println("\nTHE PRIME PALINDROME INTEGERS :\n"); for(i=0;i<c;i++) {

P a g e | 26 System.out.print(var[i]+","); } } } public static void main() throws IOException { System.out.println("Enter lower limit(should be greater than 0)"); int x=Integer.parseInt(br.readLine()); System.out.println("Enter upper limit"); int y=Integer.parseInt(br.readLine()); Prime_Palindrome pp=new Prime_Palindrome(x,y); pp.calculate(); } }

P a g e | 27 QUESTION 10 : Write a program to calculate 3D and 2D distance between two points using distance formula.

class D2point { double x,y; D2point() { x=y=0.0; } D2point(double x1,double y1) { x=x1; y=y1; } double distance2d(D2point b) { double r=Math.sqrt(Math.pow(b.x-this.x,2)+Math.pow(b.y-this.y,2)); return r; } } class D3point extends D2point { //USING INHERITANCE TO GET THE x AND y COORDINATES double z; D3point() {

P a g e | 28 z=0.0; } D3point(double x1,double y1,double z1) { super(x1,y1); z=z1; } double distance3d(D3point b) { double r=Math.sqrt(Math.pow(b.x-this.x,2)+Math.pow(b.y-this.y,2)+Math.pow(b.z-this.z,2)); return r; } } public class Distance_Calculate { public static void main() { System.out.println("Enter the coordinates of first point"); int a1=Integer.parseInt(br.readLine()); int a2=Integer.parseInt(br.readLine()); int a3=Integer.parseInt(br.readLine()); System.out.println("Enter the coordinates of second point"); int b1=Integer.parseInt(br.readLine()); int b2=Integer.parseInt(br.readLine()); int b3=Integer.parseInt(br.readLine()); D3point b=new D3point(a1,a2,a3); D3point o=new D3point(b1,b2,b3);

P a g e | 29 double r=o.distance3d(b); System.out.println("3d distance between "+a1+","+a2+","+a3 "and"+b1+","+b2+","+b3+" = "+r); r=o.distance2d(b); System.out.println("2d distance between "+a1+","+a2+","+a3 "and"+b1+","+b2+","+b3+" = "+r); } }

P a g e | 30 QUESTION 11 : A unique-digit integer is a positive integer (without leading zeros) with no duplicates digits. For example 7, 135, 214 are all unique-digit integers whereas 33,3121, 300 are not. Program to print the unique numbers within given limits and also print the frequency.

import java.io.*; class Unique_Number { int k,x,i,j,m,n,c,arr[],index; BufferedReader br; public static void main(String args[])throws IOException { Digit ob=new Digit(); ob.take(); ob.show(); } Digit() { br=new BufferedReader(new InputStreamReader(System.in)); arr=new int[1000]; c=0; index=0; } public void take()throws IOException { while(true) { System.out.println("Enter the Lower range:");

P a g e | 31 m=Integer.parseInt(br.readLine().trim()); System.out.println("Enter the Upper range:"); n=Integer.parseInt(br.readLine().trim()); if(m< n) break; } } void show () { System.out.println("The unique digit integers are:"); //CHECKING FOR UNIQUE DIGIT INTEGERS WITHIN THE RANGE OF m AND n for(i=m;i< =n;i++) { //CHECKING IF THE DIGITS REPEAT index=0; for(j=i;j> 0;j=j/10) { x=j%10; for(k=0;k< index;k++) { if (arr[k]==x) break; } if(k< index) break; else arr[index++]=x;

## System.out.println("\nFequency of unique digit integers is :"+ c); } }

P a g e | 33 QUESTION 12 :

Program to calculate the Kaprekar Numbers within a certain limit: A given number 'n' with d digits is squared and if the last 'd' digits is equal to the first remaining digits then the number 'n' is a Kaprekar number

import java.io.*; class Kaprekar { int i,p,q,c=0; int num; public static void main() throws IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the Lower Range:"); p=Integer.parseInt(br.readLine()); System.out.println("Enter the Upper Range:"); q=Integer.parseInt(br.readLine()); if(p >=q) { System.out.println("Wrong Entry..."); return; } System.out.println("THE KAPREKAR NUMBERS ARE:"); for(i=p;i< =q;i++) { show(i);

P a g e | 34 } System.out.println("FREQUENCY OF KAPREKAR NUMBERS IS:"+c); } public void show(int x) { int digit,rev=0;int no;num=x*x;digit=0;no=x; while(no >0) { digit++; no=no/10; } no=num; while(digit > 0) { rev=rev*10+no%10; no=no/10; digit--; }

// rev HOLDS THE RIGHT PART AND no HOLDS THE LEFT PART

## rev=reverse(rev); if((rev+no)==x) { System.out.print(" "+x); c++; }

P a g e | 35 } private int reverse(int n) { int r=0; while(n > 0) { r=r*10+n%10; n=n/10; } return r; } }

P a g e | 36 QUESTION 13 : Write a program to accept details of a book, calculates and prints fine and total amount as : Total amount = (2 % of price x no. of days) + fine

import java.io.*; class Library { String name; String author; double price; Library(String nm,String auth,double pr) { name=nm; author=auth; price=pr; } void show() { System.out.println("Name of Book : "+name); System.out.println("Author of Book : "+author); System.out.println("Price of Book : "+price); } } class Compute extends Library {//USING INHERITANCE TO GET DETAILS FROM CLASS LIBRARY int d; double f,p1;

P a g e | 37 Compute(String n,String a,double p,int days) { super(n,a,p); d=days; p1=p; } void fine() { int excess=d-7; if(excess<1) f=0.0; else if(excess<=5) f=excess*2.0; else if(excess<=10) f=excess*3.0; else f=excess*5.0; } void display() { super.show(); System.out.println("No. of days borrowed : "+d); System.out.println("Fine on the book double total=((0.02*p1)*d)+f; System.out.println("\nTOTAL AMOUNT : Rs. "+total); } } : Rs. "+f);

P a g e | 38

public class Library_Inherit { public static void main() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the name of book"); String nam=br.readLine(); System.out.println("Enter the name of author"); String aut=br.readLine(); System.out.println("Enter the price of book"); double pri=Double.parseDouble(br.readLine()); System.out.println("Enter the number of days borrowed"); int dayno=Integer.parseInt(br.readLine()); Compute com=new Compute(nam,aut,pri,dayno); com.fine(); com.display(); } }

P a g e | 39 QUESTION 14 Program to interact with customer and check whether requested book is present and if so updates stock and print Net Price.

## Stock() { title="A"; author="B"; noc=50; price=10; amt=0; }

void check(String t,String a,int n) { int flag1=0,flag2=0; //CHECKING IF THE TITLE AND AUTHOR MATCH if(t.equals(title)&&a.equals(author)) flag1=1; if(noc>=n) flag2=1; if(flag1==1&&flag2==1)

## public static void main() { Stock s1=new Stock(); s1.check("A","B",25); s1.disp(); } }

P a g e | 41 QUESTION 15: Program to input a word and perform the following operations on it: a) If word begins with a vowel, print the word followed by 'Y' b) If word contains but does not begin with a vowel, print the portion of the word after the first vowel followed by portion before the vowel along with 'C' c) If word does not contain vowel, print the word followed by 'N'

import java.io.*; class Rearrange { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String txt,cxt; int len; Arrange() { txt=null; cxt=null; len=0; } void readWord() throws IOException { System.out.println("Enter the word"); txt=(br.readLine()).toUpperCase(); } void convert() { len=txt.length(); int i,flag=1; char ch=txt.charAt(0); if((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')) { cxt=txt+"Y"; flag=0; } else

P a g e | 42 { for(i=1;i<len;i++) { ch=txt.charAt(i); if((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')) { cxt=txt.substring(i)+txt.substring(0,i)+"C"; flag=0; break; } } } if(flag==1) cxt=txt+"N"; } void display() { System.out.println("ORIGINAL WORD :"); System.out.println(txt); System.out.println("CHANGED WORD :"); System.out.println(cxt); } public static void main() throws IOException { Rearrange a1=new Rearrange(); a1.readWord(); a1.convert(); a1.display(); } }

P a g e | 43 QUESTION 16 : Write a program to collect relevant details from the user and find the PF and Grativity.

class Personal { String name; int pan,accountno; double basic_pay; Personal(String s,int pno,int ano,double bas) { name=s; pan=pno; accountno=ano; basic_pay=bas; } void display() { System.out.println("EMPLOYEE NAME : "+name); System.out.println("PERSONAL ACCOUNT NO(PAN No.) : "+pan); System.out.println("ACCOUNT NO : "+accountno); System.out.println("BASIC SALARY : Rs. "+basic_pay); } } class Retire extends Personal {//USING INHERITANCE TO GET DETAILS FROM CLASS Personal int yrs; double pf,grat;

P a g e | 44 Retire(String nam,int panno,int acc,double b,int y) { super(nam,panno,acc,b); yrs=y; pf=0.0; grat=0.0; } void provident() { pf=(0.02*basic_pay)*yrs; } void grativity() { if(yrs>=10) grat=12.0*basic_pay; else grat=0.0; } void display() { super.display(); System.out.println("No. of years of employment : "+yrs+" yrs"); System.out.println("PF : Rs. "+pf); System.out.println("GRATIVITY : Rs. "+grat); } } public class Retirement_Inherit

P a g e | 45 { public static void main() { Retire ret=new Retire("Balu",1234,365,50000.0,17); ret.provident(); ret.grativity(); ret.display(); } }

P a g e | 46 QUESTION 17 : Write a program to merge two arrays using objects and "this" keyword.

import java.io.*; class Combine { int com[],size; static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Combine(int nn) { size=nn; com=new int[size]; } void inputarray() throws IOException { System.out.println("Enter the array elements"); for(int i=0;i<size;i++) {

## com[i]=Integer.parseInt(br.readLine()); } } void mix(Combine A,Combine B) { int i=0,j=0; for(i=0;i<A.size;i++) {

P a g e | 47 this.com[i]=A.com[i]; } for(;i<(A.size+B.size);i++,j++) { this.com[i]=B.com[j]; } } void sort() { int temp=0,i,j,small=0,pos=0; for(i=0;i<this.size;i++) { small=this.com[i]; pos=i; //SORTING THE COMBINED ARRAY for(j=i+1;j<this.size;j++) { if(this.com[j]<small) { small=this.com[j]; pos=j; } } temp=this.com[i]; this.com[i]=this.com[pos]; this.com[pos]=temp; }

P a g e | 48 } void display() { System.out.println("COMBINED AND SORTED ARRAY : "); for(int i=0;i<this.size;i++) { System.out.print(this.com[i]+" "); } } public static void main() throws IOException { System.out.println("Enter the size of 1st array"); int a=Integer.parseInt(br.readLine()); Combine A=new Combine(a); System.out.println("Enter the size of 2nd array"); int b=Integer.parseInt(br.readLine()); Combine B=new Combine(b); Combine C=new Combine((a+b)); System.out.println("Enter the elements of 1st array"); A.inputarray(); System.out.println("Enter the elements of 2nd array"); B.inputarray(); C.mix(A,B); C.sort(); C.display(); } }

P a g e | 49 QUESTION 18: Program to print possible ways of summation. Eg., 15 = 1+2+3+4+5 = 4+5+6 = 7+8

## P a g e | 50 for(k=pos1;k<=pos2;k++) System.out.print(k+" "); }

} } }

P a g e | 51 QUESTION 19: Program to accept the order and elements of a matrix and print biggest and smallest numbers along with locations and print matrix arranged in ascending order

import java.io.*; class Array_Storage { static BufferedReader b=new BufferedReader(new InputStreamReader(System.in)); int A[][]; int m,n; Array_Storage(int a ,int b) { m=a; n=b; A=new int[m][n]; } void readValues() throws IOException { System.out.println("Enter "+(m*n)+" integers"); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { A[i][j]=Integer.parseInt(b.readLine()); } } } void displayValues() {

P a g e | 52 System.out.println("ORIGINAL MATRIX"); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { System.out.print(A[i][j]+" "); } System.out.println(); } } void compute() { int arr[]=new int[(m*n)]; int finalA[][]=new int[m][n]; int i,j,c=0,big=0,small=A[0][0],br=0,bc=0,sr=0,sc=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { arr[c]=A[i][j]; c++; if(A[i][j]<small) { small=A[i][j]; sr=i; sc=j; } if(A[i][j]>big) { big=A[i][j];

P a g e | 53 br=i; bc=j; } } } System.out.println("LARGEST NUMBER : "+big); System.out.println("\n System.out.println(" ROW = "+(br+1)); COLUMN = "+(bc+1));

System.out.println("SMALLEST NUMBER : "+small); System.out.println("\n System.out.println(" int temp=0; for(i=0;i<c;i++) { for(j=0;j<(c-i)-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } c=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { finalA[i][j]=arr[c]; ROW = "+(sr+1)); COLUMN = "+(sc+1));

P a g e | 54 c++; } } System.out.println("REARRANGED MATRIX : "); for(i=0;i<m;i++) { for(j=0;j<n;j++) { System.out.print(finalA[i][j]+" "); } System.out.println(); } } public static void main() throws IOException { int x,y; System.out.println("Enter the order of matrix"); x=Integer.parseInt(b.readLine()); y=Integer.parseInt(b.readLine()); if((x>2)&&(y>2)&&(x<20)&&(y<20)) { Array_Storage as=new Array_Storage(x,y); as.readValues(); as.displayValues(); as.compute(); } else System.out.println("!!Invalid size!! : Array size should be between (2,2) to (20,20)"); } }

P a g e | 55 QUESTION 20 :

Program to : 1) Check whether the inputted matrix is an unit matrix 2) Check whether the matrix is a symmetric matrix 3) Check whether a matrix (B) is equal to the transpose of another matrix (C)

import java.io.*; class Trans_Array { int a[][]=new int[3][3]; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); void input() throws IOException { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { a[i][j]=Integer.parseInt(br.readLine()); } } } void check(Trans_Array A) { int i,j,flag=0; for(i=0;i<3;i++)

P a g e | 56 { for(j=0;j<3;j++) { if(i==j) {//CHECKING IF IT IS A UNIT MATRIX if(A.a[i][j]!=1) { flag=1; break; } } else { if(A.a[i][j]!=0) { flag=1; break; } } } } if(flag==1) System.out.println("\nARRAY 'A' IS NOT A UNIT MATRIX"); else System.out.println("\nARRAY 'A' IS A UNIT MATRIX"); } void trans(Trans_Array A)

P a g e | 57 { int i,j,flag=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) {//CHECKING IF THE MATRIX IS A TRANSPOSE OF THE OTHER if(A.a[i][j]!=A.a[j][i]) { flag=1; break; } else flag=0; }

} if(flag==1) System.out.println("\nArray 'A' is not equal to the transpose of 'A'"); else System.out.println("\nArray 'A' is equal to the transpose of 'A'"); } void transCheck(Trans_Array A) { int i,j,flag=0; for(i=0;i<3;i++) { for(j=0;j<3;j++)

P a g e | 58 { if(A.a[i][j]!=this.a[j][i]) { flag=1; break; } else flag=0; } } if(flag==1) System.out.println("\nArray 'B' is not equal to the transpose of Array 'C'"); else System.out.println("\nArray 'B' is equal to the transpose of Array 'C'"); } public static void main() throws IOException { Trans_Array A=new Trans_Array(); Trans_Array B=new Trans_Array(); Trans_Array C=new Trans_Array(); System.out.println("Enter the elements of array A"); A.input(); System.out.println("Enter the elements of array B"); B.input(); System.out.println("Enter the elements of array C"); C.input(); A.check(A);

P a g e | 59 A.trans(A); B.transCheck(C); } }

P a g e | 60 QUESTION 21 Write a program to simulate the functioning of a ticket vending machine and print the amount collected and number of people visiting the fair (Price for one ticket = Rs. 2.50)

import java.io.*; class Ticket_Booth { int noofpeople; double amount; static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); void initial() { noofpeople=0; amount=0.0; } void notSold() { noofpeople+=1; } void sold() { noofpeople+=1; amount+=2.50; } void displayTotal() { System.out.println("\nTotal no. of people who have visited the booth : "+noofpeople);

P a g e | 61 } void displayTicket() { int no=(int)(amount/2.50); System.out.println("Total no. of tickets sold : "+no); System.out.println("Total amount collected : Rs. "+amount); } public static void main() throws IOException { Ticket_Booth tb=new Ticket_Booth(); int ch=0,c=0; while(c!=1) { System.out.println("Do you want to purchase ticket ?"); System.out.println("Enter 1 - Yes , 0 - No"); ch=Integer.parseInt(br.readLine()); if(ch==1) { System.out.println("Ticket Purchased!"); tb.sold(); } else { System.out.println("Ticket not Purchased"); tb.notSold(); } System.out.println("Do you want to continue ?");

## P a g e | 62 System.out.println("Enter 1 - Yes, 0 - No"); ch=Integer.parseInt(br.readLine()); if(ch==0) c=1; } tb.displayTotal(); tb.displayTicket(); } }

P a g e | 63 QUESTION 22 : Write a program to print the rank list along with relevant details of the students.

import java.io.*; class Record { String name[]; int rnk[],pos[],n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Record() throws IOException { System.out.println(" Enter the number of students"); n = Integer.parseInt(br.readLine()); name = new String[n]; rnk = new int[n]; pos = new int[n]; } void readValues() throws IOException { System.out.println("Enter the name & rank of " +n+" students"); for(int i=0;i<n ;i++) { name [i] = br.readLine(); rnk [i] = Integer.parseInt(br.readLine()); } } void display()

P a g e | 64 { System.out.println("Sl.no."+"\t"+"NAME"+"\t"+"RANK"); for(int i=0;i<n;i++) { System.out.println((i+1)+"\t"+name[i]+"\t"+rnk[i]); } } } class Rank extends Record { int index; Rank()throws IOException { index=0; } void highest() throws IOException { super.readValues(); for(int i=0;i<n-1;i++) { if(rnk[i+1]<rnk[i]) index=i+1; } } void arrange() throws IOException { super.readValues();

P a g e | 65 int low,tmp; for(int i=0;i<n;i++) { low = i; for(int j=0;j<n-i-1;j++) { if (rnk[j] < rnk[low]) low = j; } pos[i] = low; tmp = rnk[low]; rnk[low] = rnk[i]; rnk[i] = rnk[low]; } } void display() { System.out.println("\nHIGHEST RANK :"+name[index]+" ,Rank: "+rnk[index]); System.out.println(); System.out.println("RANK LIST :"); System.out.println(); super.display(); } } public class Student_Record { public static void main() throws IOException

## P a g e | 66 { Rank rk=new Rank(); rk.highest(); rk.arrange(); rk.display(); } }

P a g e | 67 QUESTION 23 : Program to print twin primes and calculate Brun's constant within an inputted limit. import java.io.*; class Brun { int n; double sum; static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Brun(int limit) { n=limit; sum=0.0; } boolean isPrime(int no) { int i,f=0; boolean b=false; for(i=1;i<=no;i++) { if(no%i==0) f=f+1; } if(f==2) b=true; return b; } void twinPrime()

P a g e | 68 { int i; System.out.println("TWIN PRIMES :"); for(i=3;i<=(n-2);i++) { if((isPrime(i)==true)&&(isPrime(i+2)==true)) System.out.println(i+" "+(i+2)); } } void compute() { int i; for(i=3;i<=(n-2);i++) {//CHECKING FOR TWIN PRIME if((isPrime(i)==true)&&(isPrime(i+2)==true)) sum=sum+(double)((1.0/i)+(1.0/(i+2))); } System.out.println("BRUN'S CONSTANT : "+sum); } public static void main() throws IOException { System.out.println("Enter the limit"); Brun b=new Brun(Integer.parseInt(br.readLine())); b.twinPrime(); b.compute(); } }

P a g e | 69 QUESTION 24 : Program to accept date in string format dd/mm/yyyy and accept name of the day on the 1st of January that year and calculates the name of the day of the inputted date.

import java.io.*; class Date_Calculate { int arr[]={31,28,31,30,31,30,31,31,30,31,30,31}; BufferedReader br; String str1,day,day1; int x,i,dayno,mon,yr,leap1; public static void main() throws IOException { Date_Calculate dc=new Date_Calculate(); dc.take(); dc.show(); } Date_Calculate() { br=new BufferedReader(new InputStreamReader(System.in)); } public void take() throws IOException { System.out.println("Enter the date in dd/mm/yyyy format"); day=br.readLine().trim(); day1=day; i=day.indexOf("/"); dayno=Integer.parseInt(day.substring(0,i));

P a g e | 70 day=day.substring(i+1); i=day.indexOf("/"); mon=Integer.parseInt(day.substring(0,i)); day=day.substring(i+1); yr=Integer.parseInt(day); leap1=0; if(mon>2) leap1=leap(yr); System.out.println("Enter the name of the day on 1st January of that year"); str1=br.readLine().trim(); } int leap(int p) { if(p%100==0 && p%400==0) return 1; else if(p%100!=0 && p%4==0) return 1; else return 0; } void show() { if(str1.equalsIgnoreCase("Sunday")) x=1; else if(str1.equalsIgnoreCase("Monday")) x=2; else if(str1.equalsIgnoreCase("Tuesday")) x=3;

P a g e | 71 else if(str1.equalsIgnoreCase("Wednesday")) x=4; else if(str1.equalsIgnoreCase("Thursday")) x=5; else if(str1.equalsIgnoreCase("Friday")) x=6; else if(str1.equalsIgnoreCase("Saturday")) x=7; else System.out.println("INVALID DAY NAME!"); for(i=0;i<mon-1;i++) dayno=dayno+arr[i]; dayno=dayno+leap1; for(i=0;i<dayno-1;i++) { x++; if(x==8) x=1; } System.out.print(day1+" : "); switch(x) { case 1: System.out.print("Sunday"); break; case 2: System.out.print("Monday");

P a g e | 72 break; case 3: System.out.print("Tuesday"); break; case 4: System.out.print("Wednesday"); break; case 5: System.out.print("Thursday"); break; case 6: System.out.print("Friday"); break; case 7: System.out.print("Saturday"); break; } } }

P a g e | 73 QUESTION 25 : Program to perform binary search for an item on an array both inputted from the user using Recursion technique.

P a g e | 74

## else { return (m); }

P a g e | 75 } public static void main() throws IOException { System.out.println("Enter the size of array"); size=Integer.parseInt(br.readLine()); BinarySearch1 b=new BinarySearch1(size); b.readData();

int p=b.binarysearch(x,l,u,); if(p==-1) System.out.println("Not Found"); else System.out.println("Found At Position : " +(p+1));

} }

P a g e | 76 QUESTION 26 : Program to multiply two matrices provided number of rows in first match number of columns in the second. Limit the maximum number of rows and columns to 10 in each case. import java.io.*; class Arr { int m1[][]=new int[10][10]; int m2[][]=new int[10][10]; int mult[][]=new int[10][10]; int i,j,k,r1,c1,r2,c2; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public void show() throws IOException { System.out.println("Enter number of rows of first matrix (less than 10)\n"); r1=Integer.parseInt(br.readLine()); System.out.println("Enter number of columns of first matrix (less than 10)\n"); c1=Integer.parseInt(br.readLine()); System.out.println("Enter number of rows of second matrix (less than 10)\n"); r2=Integer.parseInt(br.readLine()); System.out.println("Enter number of columns of second matrix (less than 10)\n"); c2=Integer.parseInt(br.readLine()); System.out.println("Value for 1st matrix:"); if(r2==c1) {

P a g e | 77 {

## m1[i][j]=Integer.parseInt(br.readLine()); } } System.out.println("Value for 2nd matrix:");

for(i=0;i< r2;i++) { for(j=0;j< c2;j++) { m2[i][j]=Integer.parseInt(br.readLine()); } } System.out.println("\n1st Matrix\n"); for(i=0;i< r1;i++) { for(j=0;j< c1;j++) System.out.print(" "+m1[i][j]); System.out.println(); } System.out.println("\n2nd Matrix\n"); for(i=0;i< r2;i++) { for(j=0;j< c1;j++) System.out.print(" "+m2[i][j]); System.out.println();

P a g e | 78 } for(i=0;i< r1;i++) { for(j=0;j< c2;j++) { mult[i][j]=0; for(k=0;k< r1;k++) {//MULTIPLYING BOTH MATRICES mult[i][j]+=m1[i][k]*m2[k][j]; } } } System.out.println("\nFinal Matrix\n"); for(i=0;i< r1;i++) { for(j=0;j< c2;j++) System.out.print(" "+mult[i][j]); System.out.println(); } } } }

P a g e | 79 QUESTION 27 : Program to remove duplicate characters from a string and print final string

import java.io.*; public class Remove_Duplicate_Characters { public static void main() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter string"); String s=br.readLine(); char ch; String s1,sf=""; int l=s.length(); for(int i=0;i<l;i++) { for(int j=i+1;j<l;j++) { String s2=s.substring(0,j); String s3=s.substring(j); if(s.charAt(j)==s.charAt(i))

## // replacing duplicate characters with '*'

s3=s3.replace(s.charAt(j),'*'); s=s2+s3; }

P a g e | 80 } for(int i=0;i<l;i++) { // REMOVING '*' AND JOINING THE REST OF THE CHARACTERS if(s.charAt(i)=='*') sf=sf+""; else sf=sf+s.charAt(i); } System.out.println("The new string is - "+sf); } }

P a g e | 81 QUESTION 28 : Program to accept a sentence and print the number of words and sentence arranged according to alphabetical order of the first letter of each word.

import java.io.*; import java.util.*; class Sentence_Operations { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str; String words[]; StringTokenizer stk; int i,j,c,x; public void take()throws Exception { int flag; while(true) { flag=0; System.out.println("Enter the sentence:"); str=br.readLine(); for(i=0;i< str.length();i++) {//ARRANGING IN ORDER if(Character.isLowerCase(str.charAt(i))) { flag=1; break;

P a g e | 82 } } if (flag==0) break; } stk=new StringTokenizer(str," .,?!"); c=stk.countTokens(); x=0; words=new String[c]; while(stk.hasMoreElements()) { words[x++]=stk.nextToken(); } System.out.println("Length="+c); for(i=0;i< x-1;i++) { for(j=i+1;j< x;j++) { if(words[i].compareTo(words[j])>0) { str=words[i]; words[i]=words[j]; words[j]=str; } } } System.out.println("\nRearranged Sentence:\n");

P a g e | 83 for(i=0;i< c;i++) System.out.print(words[i]+" "); } public static void main(String args[]) throws Exception { Sentence_Operations ob=new Sentence_Operations(); ob.take(); } }

P a g e | 84 QUESTION 29 :

Program to print the Tribonacci series. Tribonacci series is like Fibonacci series with slight difference. In this series the latest three numbers are added and the result is displayed.

import java.io.*; class Tribonacci { int i,p,m,c,next,n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public void disp()throws Exception { p=0; m=1; c=1; System.out.println("Enter the number of elements in the series:"); n=Integer.parseInt(br.readLine()); System.out.print("The series is="+ p+" "+m+" "+c+ " "); for(i=0;i< n-3;i++) { next=p+m+c; System.out.print(next+ " "); p=m; m=c; c=next; } } public static void main()throws IOException

## P a g e | 85 { Tribonacci t1=new Tribonacci (); t1.disp(); } }

P a g e | 86 QUESTION 30: Write a program to print the inputted number in words with proper conjunctions. The Limit is 9999.

import java.io.*;

class NumToWords_till_9999 {

static int r,d1=0,d2=0,d3=0,d4=0; static int i,j; static int n=0; static String ones[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; static String tens[]={"","Ten","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"}; static String hundreds[]={"","One Hundred","Two Hundred","Three Hundred","Four Hundred","Five Hundred","Six Hundred ","Seven Hundred","Eight Hundred","Nine Hundred"}; static String thousands[]={"","One Thousand","Two Thousand","Three Thousand","Four Thousand","Five Thousand","Six Thousand","Seven Thousand","Eight Thousand","Nine Thousand"};

digit(); if(n==0)

P a g e | 87 System.out.print("Zero"); else if(n<=9) ones(); else if(n>=10&&n<=99) tens(); else if(n>=100&&n<=999) hundreds(); else if(n>=1000&&n<=9999) thousands() }

public static void digit() { //EXTRACTING DIGITS d1=n%10; d2=(n%100)/10; d3=(n%1000)/100; d4=(n%10000)/1000; } public static void ones() { System.out.print(" "+ones[d1]+" "); }

## public static void tens() { if(d1!=0&&d4!=0&&d3!=0) System.out.print(" And ");

P a g e | 88

switch(d1+d2*10) { case 10 : System.out.print("Ten"); break; case 11 : System.out.print("Eleven"); break; case 12 : System.out.print("Twelve"); break; case 13 : System.out.print("Thirteen"); break; case 14 : System.out.print("Fourteen"); break; case 15 : System.out.print("Fifteen"); break; case 16 : System.out.print("Sixteen"); break; case 17 : System.out.print("Seventeen"); break; case 18 : System.out.print("Eightteen"); break; case 19 : System.out.print("Nineteen"); break; default : System.out.print(tens[d2]); ones(); break; }

P a g e | 89