2000.Q.No.1 Happy-Happy Refer Your Record //Q2.2000. To Print The Elapsed Days Between 2 Dates

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 31

//2000.Q.no.

1 HAPPY-HAPPY REFER YOUR RECORD


//Q2.2000. TO PRINT THE ELAPSED DAYS BETWEEN 2 DATES
import java.io.*;
class Q2_2000
{ public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int d1,d2,m1,m2,y1,y2,ed=0,i,j;
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
System.out.println("FIRST DATE :DAY ");
d1=Integer.parseInt(b.readLine());
System.out.print("MONTH :");
m1=Integer.parseInt(b.readLine());
System.out.print("YEAR :");
y1=Integer.parseInt(b.readLine());
System.out.println("SECOND DATE :DAY ");
d2=Integer.parseInt(b.readLine());
System.out.print("MONTH :");
m2=Integer.parseInt(b.readLine());
System.out.print("YEAR :");
y2=Integer.parseInt(b.readLine());
//if month && year are same
if((m1==m2) &&(y1==y2))
System.out.print("ELAPSED DAYS BETWEEN 2 DATES ARE "+(Math.abs(d2-d1)-1));
else if(y1==y2)//if years are same
{
if((y1%400==0)||((y1%4==0)&&(y1%100!=0)))m[1]=29; //leap year checking
ed=m[m1-1]-d1;
for(j=m1;j<m2-1;j++)//loop to add the days of each month
ed=ed+m[j];
ed=ed+d2;
System.out.print("ELAPSED DAYS BETWEEN 2 DATES ARE "+(ed-1));
}
else
{
boolean flag=false;
ed=ed+d1;
if((y1%400==0)||((y1%4==0)&&(y1%100!=0))){flag=true;m[1]=29;}//leap year
for(i=0;i<m1-1;i++)//loop to add the total days of each month in year y1
{
ed=ed+m[i];
}
if(flag==true)ed=366-ed;else ed=365-ed;
for(i=y1+1;i<y2;i++)// loop to add the total days of each year between y1 & y2
{
int year=i;
if((year%400==0)||((year%4==0)&&(year%100!=0))){ed=ed+366;}else ed=ed+365;
}
for(i=0;i<m2-1;i++)//loop to add the total days of each month in year y1
ed=ed+m[i];
ed=ed+d2;
System.out.print("ELAPSED DAYS BETWEEN 2 DATES ARE "+(ed-1));
}
}//main
}//class
//Q1.2001 Program to generate and print lucky numbers less than N where N<=50
import java.io.*;
class Q1_2001
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int N,i;
System.out.print("SAMPLE INPUT N= ");
N=Integer.parseInt(b.readLine());
int a[];
a=new int[N];
System.out.println("Enter array elements ");
for(i=0;i<N;i++)
a[i]=Integer.parseInt(b.readLine());
System.out.print("OUTPUT : ");
System.out.print("THE LUCKY NUMBERS LESS THAN " +N+ " ARE: \n");
int f=2,c=0,j;
//loop to print the lucky numbers
for(i=0;i<N;i++)
{
//loop to remove the second,third,fourth numbers and so on
for(j=1;j<=N;j++)
{
if(j%f!=0)
{a[c]=a[j-1];c++;}
}
f=f+1;
N=c;c=0;
}
//loop to print the lucky numbers
for(i=0;i<N;i++)
System.out.println(a[i]);
}//main
}//class
//2001 Q.no.2 to calculate and print the corresponding day of the year
import java.io.*;
class Q2_2001
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int mon[]={31,28,31,30,31,30,31,31,30,31,30,31};
int m,d,y,cd=0,i;
System.out.print("INPUT: month number ");
m=Integer.parseInt(b.readLine());
System.out.print("day ");
d=Integer.parseInt(b.readLine());
System.out.print("year ");
y=Integer.parseInt(b.readLine());
//To check y is a leap year or not
if((y%400==0)||(y%4==0&&y%100!=0))
mon[1]=29;
//loop to calculate the corresponding day of the year
for(i=0;i<m-1;i++)
{
cd=cd+mon[i];
}
cd=cd+d;
System.out.print("\nOUTPUT:");
System.out.print("\nCORRESPONDING DAY OF THE YEAR IS:"+cd+"\n(");
//loop to print the output in the given format
for(i=0;i<m-1;i++)
{
System.out.print(mon[i]+"+");
}

System.out.print(d+"= "+cd+")");
}//main
}//end

//2001 Q.no.3 A[] of N integers B[] of M integers merge them in array C[] using sorting while
//merging Refer your record
// 2002 Q.no.1 To read N and print out the twin prime pair that has
//smallest distance from N
import java.io.*;
public class Q1_2002
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int N,i;
int prime1[],prime2[],prime3[];
prime1=new int[2];
prime2=new int[2];
prime3=new int[2];
System.out.print("Input:\n");
System.out.print("Give the number:");
N=Integer.parseInt(b.readLine());
System.out.print("Output:\n");
System.out.print("Number read in is:"+N);
//loop to find out the first twin prime pair <=N
for(i=N;;i--)
{
int f=prime(i);
if(f==1)
{
int m=prime(i-2);
if(m==1)
{
prime1[0]=i;
prime1[1]=i-2;
break;
}
}
}//loop end
//loop to find out the first twin prime pair >=N
for(i=N;;i++)
{
int f=prime(i);
if(f==1)
{
int m=prime(i+2);
if(m==1)
{
prime2[0]=i;
prime2[1]=i+2;
break;
}
}
}//loop end
//statements to find out N+1 and N-1 is the twin prime pair
int m=prime(N+1);
int n=prime(N-1);
if(m==1&&n==1)
{
prime3[0]=N-1;
prime3[1]=N+1;
}
int dis1=Math.abs(N-prime1[0]);
int dis2=Math.abs(N-prime2[0]);
int dis3=Math.abs(N-prime3[0]);
//to print the desired output
if(dis1<dis2&&dis1<dis3)
System.out.print("\np1="+prime1[0]+"\tp2="+prime1[1]);
else if(dis2<dis3)
System.out.print("\np1="+prime2[0]+"\tp2="+prime2[1]);
else
System.out.print("\np1="+prime3[0]+"\tp2="+prime3[1]);
}//main
//Function to check x is prime or not
public static int prime(int x)
{ int c=0;
for(int i=1;i<=x;i++)
{ if(x%i==0)c++;}
if(c==2) return 1;
else return 0;
}

}//class
//2002 Q.no.2 To rearrange the integers in given array DATA
import java.io.*;
class Q2_2002
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int data[],n,c=1,sec,fir,j=0;
int lar,pos=0,i,k;
data=new int[50];
System.out.print("Input:\n");
System.out.print("Give the number of integers:");
n=Integer.parseInt(b.readLine());
//loop to enter the integers and store the in data[]
for(i=0;i<n;i++)
{
System.out.print("Give integer "+(i+1)+":");
data[i]=Integer.parseInt(b.readLine());
}
System.out.print("Output:\n");
System.out.print("Original array\n");
//loop to print the original array
for(i=0;i<n;i++)
System.out.print(data[i]+"\t");
System.out.print("\nRearranged array\n");

if(n%2==0) {fir=n/2-2;sec=n/2;j=1;}
else
{fir=n/2-1;sec=n/2+1;}
lar=data[0];
//loop to find the largest number and its index
for(i=1;i<n;i++)
{ if(data[i]>lar)
{lar=data[i];
pos=i;
}
}
//statements to swap the largest number with the middle element of data[]
int t=data[n/2-j];
data[n/2-j]=lar;
data[pos]=t;
//loop to rearrange the numbers as given
while(fir>=0&&sec<n)
{ lar=data[0];pos=0;
//loop to find largest no and index in the first half of data[]
for(k=1;k<=fir;k++)
{
if(data[k]>lar)
{
lar=data[k];pos=k;
}
}
//loop to find largest no and index in the second half of data[]
for(k=sec;k<n;k++)
{
if(data[k]>lar)
{
lar=data[k];
pos=k;
}
}
//statements to exchange the elements
if(c%2==1)
{
t=data[sec];
data[sec]=lar;
data[pos]=t;
sec++;c++;
}
else
{
t=data[fir];
data[fir]=lar;
data[pos]=t;
fir--;c++;
}
}//end of while
//loop to print the rearranged array
for(i=0;i<n;i++)
System.out.print(data[i]+"\t");
}//main
}//class
//2002 Q.NO.3 LINKED LIST
/*2003 Q.no.1 To input a coded text and decodes it
SAMPLE DATA:
INPUT:
CODED TEXT: UHINBYLKKQCHHYLKK
SHIFT : 7
OUTPUT:
DECODED TEXT: ANOTHER WINNER*/
import java.io.*;
class Q1_2003
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String s="";
System.out.print("SAMPLE DATA:\n");
System.out.print("INPUT:\n");
System.out.print("CODED TEXT: ");
s=b.readLine();
int sv,i;
System.out.print("SHIFT : ");
sv=Integer.parseInt(b.readLine());
System.out.print("OUTPUT:\n");
if(sv>26)
System.out.print("INVALID SHIFT VALUE.");
else
{
System.out.print("DECODED TEXT: ");
int l=s.length();
sv=sv-1;
//Loop to obtain the desired output
for(i=0;i<l;i++)
{
int nv=s.charAt(i)+sv;//Statement to get the ASCII value of s[i] after //adding the shift value
char c=(char)nv;
if(c=='Q')System.out.print(" ");//Statement to print the blank space
else
{
if(nv<=90)
System.out.print(c);
else
{
//Statements to print the equivalent alphabets when the ASCII value beyond 90
nv=nv-90;
System.out.print((char)(nv+64));
}
}
}//i loop
}//else

}//main
}//class
//2003 Q.No.2 To convert time in numbers into words
import java.io.*;
class Q2_2003
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String h[]={"one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"};
String m[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen",
"seventeen","eighteen","nineteen","twenty"};
int hrs,sec;char com;
System.out.print("INPUT:\nTIME:");
hrs=Integer.parseInt(b.readLine());
sec=Integer.parseInt(b.readLine());
//TO VALIDATE THE INPUT
if(sec>=60||sec<0||hrs<1||hrs>12)
{
System.out.print("OUTPUT: INCORRECT INPUT\n");System.exit(0);
}
if(sec==0)
System.out.print(h[hrs-1]+" o' clock");
else if(sec<=30)//to print the message "past"
{
if(sec==15)
System.out.print("quarter past "+h[hrs-1]);
else if(sec==30)
System.out.print("half past "+h[hrs-1]);
else
{
if(sec<=20)
{ if(sec==10)
System.out.print("ten minutes past "+h[hrs-1]);
else if(sec==20)
System.out.print("twenty minutes past "+h[hrs-1]);
else
{ if(sec<10)
{System.out.print(h[sec-1]+" minute(s) past "+h[hrs-1]);
}
else
{
int r=sec%10;
System.out.print(m[r-1]+" minutes past "+h[hrs-1]);
}
}
}
else
{
int r=sec%10;
System.out.print("twenty "+h[r-1]+" minutes past "+h[hrs-1]);
}
}
}
else
{ //To print the message "minutes to"
int r=60-sec;
if(r==15)
{ if(hrs==12) System.out.print("quarter to one ");
else System.out.print("quarter to "+h[hrs]);
}
else if(r>20)
System.out.print("twenty "+h[(r%10)-1]+" minutes to "+h[hrs]);
else if(r>=11&&r<=20)
System.out.print(m[r-11]+" minutes to "+h[hrs]);
else System.out.print(h[r-1]+" minute(s) to "+h[hrs]);
}
}//main
}//class

//2003 Q.No.3 To find the saddle point and sort the elements along principal //diagonal in
ascending order using insertion sort technique
import java.io.*;
public class Q3_2003
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int N;
int A[][],rowmin[],colmax[];
int i,dia[],m=0,j,flag=0,t,ptr;
System.out.print("INPUT : N= ");
N=Integer.parseInt(b.readLine());;
A=new int[N][N];
rowmin=new int[N];
colmax=new int[N];
dia=new int[N];
System.out.print("MATRIX A[ ] [ ] = ");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
A[i][j]=Integer.parseInt(b.readLine());
}
System.out.print("\nOUTPUT :\n\n");
//To output the matrix elements as given
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{System.out.print(A[i][j]+"\t");
}
System.out.print("\n");
}
//To sort the principal diagonal and find saddle point
int c=0;
//Loop to find minimum no of each row, maximum no each column and store
//them in rowmin[],colmax[]
for(i=0;i<N;i++)
{
int min=A[i][0];
int max=A[0][i];
//Loop to store the principal diagonal elements in dia[]
for(j=0;j<N;j++)
{
if(i==j){dia[m]=A[i][j]; m++; }//end of if
if(A[i][j]<min)
{
min=A[i][j];
}
if(A[j][i]>max)
{
max=A[j][i];
}
}//end of for ‘j’ loop
rowmin[c]=min;
colmax[c]=max;
c++;
}//end of ‘I’ loop
//Loop to find the saddle point
for(i=0;i<c;i++)
{
for(j=0;j<c;j++)
{
if(rowmin[j]==colmax[j])
{
flag=1;
System.out.print("SADDLEPOINT = "+rowmin[j]);
}
}//j loop
if(flag==1)break;
}// I loop
if(flag==0)
System.out.print("\nNO SADDLE POINT\n");
System.out.print("\n\nMATRIX AFTER SORTING THE PRINCIPAL DIAGONAL\n");
//A nested loop to sort the diagonal elements using Insertion sort
for(i=1;i<N;i++)
{
t=dia[i];
ptr=i-1;
while(t<dia[ptr]&&ptr>=0)
{
dia[ptr+1]=dia[ptr];
ptr--;
}
dia[ptr+1]=t;
}
//storing back
m=0;
for(i=0;i<N;i++)
{
A[i][i]=dia[m];
m++;
}
//Loop to print the resultant matrix
for(i=0;i<N;i++)
{
System.out.print("");
for(j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.print("\n");
}// end of ‘I’ loop
}//main
}//class
//2004 Q.no.1 To input X and Y and calculate the smallest base for X and smallest base //for Y
//To input two integers X and Y and calculate the smallest base for X
//and smallest base for Y(likely different from X) so that X and Y
//represent the same value
/*SAMPLE DATA:
INPUT:
X=12
Y=5 OUTPUT: 12(base 3)=5(base 2)
SAMPLE DATA:
INPUT: X=10 Y=A
OUTPUT: 10(base 2)=A(base 2)*/
import java.io.*;
class Q1_2004
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String a,b;
System.out.print("SAMPLE DATA:\n");
System.out.print("INPUT:\n");
System.out.print("X=");
a=br.readLine();
System.out.print("Y=");
b=br.readLine();
System.out.print("OUTPUT:\n");
int X,Y;
//Statements to convert the alphabet inputs into integers for eg 'A' to 10...
if(Character.isLetter(a.charAt(0))) X=a.charAt(0)-55; else X=Integer.parseInt(a);
if(Character.isLetter(b.charAt(0)))Y=b.charAt(0)-55; else Y=Integer.parseInt(b);
int i,x1=X,y1=Y;int flag=0;
for(i=2;i<=20;i++) //Loop to find the smallest base
{ double s1=0; int c1=0;
//loop to find the sum for each base
while(x1>0)
{
int r=x1%10;
s1=s1+r*Math.pow(i,c1);
c1++; x1=x1/10;
}
//loop to find the base so that X=Y
for(int j=2;j<=20;j++)
{ double s2=0; int c2=0;
while(y1>0)
{
int r=y1%10;
s2=s2+r*Math.pow(j,c2);
c2++; y1=y1/10;
}
if(s2==s1) //Statements to print the desired output
{
if(Character.isLetter(a.charAt(0)))
System.out.print(a.charAt(0)+"(base "+i+")="+Y+"(base "+j+")");
else if(Character.isLetter(b.charAt(0)))
System.out.print(X+"(base "+i+")="+b.charAt(0)+"(base "+j+")");
else System.out.print(X+"(base "+i+")="+Y+"(base "+j+")");
flag=1;break;
}
y1=Y;}
if(flag==1) break; else{x1=X;}
}//i loop
if(flag==0)
System.out.print(X+"is not equal to "+Y+"in any base between 2 to 20");
}//main
}//class
//2004 Q.no.2 To read a coded message and decodes it
//Encoded message 2312179862310199501872379231018117927
//The decoded message will be Have a Nice Day
import java.io.*;
class Q2_2004
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1="",s=""; int i;
System.out.print("SAMPLE DATA:\n");
System.out.print("INPUT:\n");
System.out.print("Encoded message:\n");
s1=br.readLine();
//To reverse the original string
int l=s1.length();
for(i=l-1;i>=0;i--)
{
s=s+s1.charAt(i);
}
//Loop to get the desired output
for(i=0;i<l;i++)
{
//Statements to form a 2 digit number
int a=s.charAt(i)-'0';
int b=s.charAt(i+1)-'0';
int td=a*10+b;
char f=(char)td;
i=i+1;
if(td==32)System.out.print(" ");//Statement to print the blank space blank space ascii value is 32
else if(!Character.isLetter(f))//Statements to extract the 3rd digit if char(td) does
//not represent an alphabet
{
i=i+1;
int d=s.charAt(i)-'0';
int nd=td*10+d;
System.out.print((char)nd);
}
else
{ System.out.print((char)td);//Statement to print the alphabet of the two digit number
}
}//i loop
}//main
}//class

//2004 Q.no.3 To find the duration for which each user has logged.
//Output the record of the user who logged for the longest duration
import java.io.*;
class Q3_2004
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int userid[],N,loginhrs[],loginmin[],logouthrs[];
int logoutmin[],loginday[],loginmon[],i;
int logoutday[],logoutmon[],totalmin[],durhrs[],durmin[];
userid=new int[100];
loginhrs=new int[100]; loginmin=new int[100]; logouthrs=new int[100]; logoutmin=new int[100];
loginday=new int[100]; loginmon=new int[100]; logoutday=new int[100];
logoutmon=new int[100]; totalmin=new int[100]; durhrs=new int[100]; durmin=new int[100];
System.out.print("INPUT:\n");
System.out.print("Number of users: ");
N=Integer.parseInt(b.readLine());
//loop to enter the input as given
for(i=0;i<N;i++)
{
System.out.println("USER IDENTIFICATION ");
userid[i]=Integer.parseInt(b.readLine());
System.out.println("LOGIN TIME ");
loginhrs[i]=Integer.parseInt(b.readLine());
loginmin[i]=Integer.parseInt(b.readLine());
System.out.println("DATE");
loginday[i]=Integer.parseInt(b.readLine());
loginmon[i]=Integer.parseInt(b.readLine());
System.out.println("LOGOUT TIME");
logouthrs[i]=Integer.parseInt(b.readLine());
logoutmin[i]=Integer.parseInt(b.readLine());
System.out.println("DATE\n");
logoutday[i]=Integer.parseInt(b.readLine());
logoutmon[i]=Integer.parseInt(b.readLine());
}
//loop to find the duration for which each user logged
for(i=0;i<N;i++)
{
if(loginday[i]==logoutday[i])
{
totalmin[i]=logouthrs[i]*60+logoutmin[i]-(loginhrs[i]*60+loginmin[i]);
durhrs[i]=totalmin[i]/60;
durmin[i]=totalmin[i]%60;
}
else
{
totalmin[i]=1440-(loginhrs[i]*60+loginmin[i]);
for(int j=loginday[i]+1;j<logoutday[i];j++)
{
totalmin[i]=totalmin[i]+1440;
}
totalmin[i]=totalmin[i]+logouthrs[i]*60+logoutmin[i];
durhrs[i]=totalmin[i]/60;
durmin[i]=totalmin[i]%60;
}
}//i loop
int large=totalmin[0];
int pos=0;
//loop to find the index of the user who logged for the longest duration
for(i=1;i<N;i++)
{
if(totalmin[i]>large)
{
large=totalmin[i];
pos=i;
}
}//i loop
System.out.print("OUTPUT:\n");
System.out.print("USER IDENTIFICATION LOGIN TIME&DATE LOGOUT TIME &DATE\t");
System.out.print("DURATION HOURS:MINS\n");
//loop to output all records along with the duration in hrs:min
for(i=0;i<N;i++)
{
System.out.print("\t"+userid[i]+"\t"+loginhrs[i]+":"+loginmin[i]+"\t");
System.out.print(loginday[i]+"-"+loginmon[i]+"\t\t"+logouthrs[i]+":"+logoutmin[i]);
System.out.print("\t"+logoutday[i]+"-"+logoutmon[i]+"\t\t"+durhrs[i]+":");
System.out.print(durmin[i]+"\n");
}

System.out.print("THE USER WHO LOGGED IN FOR THE LONGEST DURATION\n");


System.out.print(userid[pos]+"\t\t"+loginhrs[pos]+":"+loginmin[pos]+"\t"+logouthrs[pos]);
System.out.print(":"+logoutmin[pos]+"\t"+logoutday[pos]+"-"+logoutmon[pos]);
System.out.print("\t\t"+durhrs[pos]+":"+durmin[pos]);
}//main
}//class
//2005 Q.no.1 printing the words of a sentence in descending order of their length
//Refer your record
//2005 Q.no.2 to read a square matrix and check it is a) wondrous square b) also output //the
prime numbers in ascending order along with row and column index
import java.io.*;
class Q2_2005
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int A[][],N;
int i,rs[],cs[],c=0,pri[],rowi[],coli[],no,f=0;
System.out.print("SAMPLE DATA:\n");
System.out.print("INPUT\n");
System.out.print("N=");
N=Integer.parseInt(b.readLine());
A=new int[N][N]; rs=new int[N];
cs=new int[N]; pri=new int[N*N];
rowi=new int[N*N]; coli=new int[N*N];
//Loop to enter the matrix elements
for(i=0;i<N;i++)
{
for(int j=0;j<N;j++)
A[i][j]=Integer.parseInt(b.readLine());
}
System.out.print("OUTPUT:\n");
//Nested Loop to store Rowsum,Column sum and prime numbers from A[][]
for(i=0;i<N;i++)
{
rs[i]=0;cs[i]=0;
for(int j=0;j<N;j++)
{
no=A[i][j]; f=0;
rs[i]+=A[i][j]; cs[i]+=A[j][i];
for(int k=1;k<=no;k++)//loop to check A[i][j] is a prime number
{
if(no%k==0)f++;
}
if(f==2)
{pri[c]=no;rowi[c]=i;coli[c]=j;
c++;}
}
}
int flag=0;
double sum=0.5*N*(N*N+1);
//Loop to check A[][] is a wondrous square
for(i=0;i<N;i++)
{
if(rs[i]!=sum || cs[i]!=sum)
{flag=1;break;}
}
if(flag==0)
System.out.print("YES IT REPRESENTS A WONDROUS SQUARE\n");
else
System.out.print("NOT A WONDROUS SQUARE\n");
System.out.print("PRIME ROW INDEX COLUMN INDEX\n");
//Loop to arrange the prime numbers in ascending order
for(i=0;i<c-1;i++)
{
for(int j=0;j<c-i-1;j++)
{
if(pri[j]>pri[j+1])
{
int t1,t2,t3;
t1=pri[j]; pri[j]=pri[j+1]; pri[j+1]=t1;
t2=rowi[j]; rowi[j]=rowi[j+1]; rowi[j+1]=t2;
t3=coli[j]; coli[j]=coli[j+1]; coli[j+1]=t3;
}
}
}
//Loop to print the desired output
for(i=0;i<c;i++)
System.out.print(pri[i]+"\t\t"+rowi[i]+"\t\t"+coli[i]+"\n");
}//main
}//class

//Q3_2005 PROGRAM TO PRINT ALL POSSIBLE PERMUTATIONS OF A WORD


/*OUTPUT
------
Enter a word: 123
Possible permutations are :
213
231
321
312
132
123
Enter a word : top
Possible permutations are :
1:opt
2:pot
3:pto
4:tpo
5:otp
6:top
*/
import java.io.*;
public class Q3_2005
{
static int comb=1;
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String s; int i;
System.out.print("Enter a word : ");
s=b.readLine();
int len=s.length();
char c[]=new char[len];
for(i=0;i<len;i++)
c[i]=s.charAt(i);
System.out.println("Possible permutations are :");
permut(c,len);
}
//function to swap a[i] with a[j]
public static void swap(char a[],int i,int j)
{
char c;
c=a[i];
a[i]=a[j];
a[j]=c;
}
//function to generate the permutations of the array
public static void permut(char a[],int le)
{
if(le==1)
{
System.out.print(comb+":");
System.out.print(a);
comb++;
System.out.println();
return;
}
for(int i=0;i<le;i++)
{
swap(a,i,le-1);
permut(a,le-1);
swap(a,i,le-1);
}
}//permut
}//class
//2006 Q.no.1 to print the possible consecutive number combinations
//which when added give N
/*if N=15 then output will be
1 2 3 4 5
4 5 6
7 8*/
import java.io.*;
class Q1_2006
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int N,j;
System.out.print("INPUT\n");
System.out.print("\tN=");
N=Integer.parseInt(b.readLine());
System.out.print("\nOUTPUT\n");
//loop to find the combinations
for(int i=1;i<=N/2;i++)
{
int sum=0;
//loop to add j to sum as long as it is < N
for(j=i;;j++)
{
sum=sum+j;
if(sum>=N)
break;
}
//loop to print the combination
if(sum==N)
{
for(int k=i;k<=j;k++)
System.out.print("\t"+k);
System.out.print("\n");
}
}//’I’ loop
}
}
//2006 Question 2. Array A[] N names B[] M names Merge them into C[ ] using sorting while
merging Refer your record
//2006 Q.no.3 program to input a code and its length
// At the first instance of an error display "Invalid!" stating the
//appropriate reason. in case of no error, display "Valid!"
import java.io.*;
public class q3_2006
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String code;
int N;
System.out.print("N=");
N=Integer.parseInt(b.readLine());
if(N>6)
{System.out.print("Error! Length of string should not exceed 6 characters!");
}
else
{
code=b.readLine();
System.out.print("OUTPUT\n");
int l=code.length();
if(l>N)
System.out.print("Invalid! String length not the same as specified!");
else
{
int f=1;
//loop to check alternate letters, lower case letters
for(int i=0;i<l;i++)
{
if(Character.isLowerCase(code.charAt(i)))
{f=0;System.out.print("Invalid! Only upper case letters permitted!");
break;
}
//loop to check repetition of characters
for(int j=i+1;j<l&&i!=l-1;j++)
{
if(code.charAt(i)==code.charAt(j))
{f=0;System.out.print("Invalid! Repetition of characters not permitted!");
break;
}
}
int x=code.charAt(i)+1;
int y=code.charAt(i+1);
if(x==y)
{f=0;
System.out.print("Invalid! only alternate letters permitted!");
break;
}
}//end of for loop

if(f==1)
System.out.print("\nValid!");
} }
}
}

/*2007 Q.no.1 To accept the name of the day on 1st January


of the corresponding year and print the day for the given date.
Example: Day:10 Month:1 Year:2001
Day on 1st January: Thursday
Output: Day on 10th January is: Saturday
Example: Day:5 Month:7 Year:2001
Day on 1st January: Monday
Output: Day on 10th January is: Thursday
*/
import java.io.*;
public class Q1_2007
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int m[]={31,28,31,30,31,30,31,31,30,31,30,31};
String week[]={"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY",
"SATURDAY","SUNDAY"};
String day,d[]={"","",""};//to avoid printing the word null
int i=0; int date=0,mon=0,year=0,code=0,count=0;
System.out.print("INPUT\nDate: ");
day=b.readLine();
for(i=0;i<day.length();i++)
{
char c=day.charAt(i);
if(c!='/')
{d[count]=d[count]+c;}
else {count++;}
}
date=Integer.parseInt(d[0]);
mon=Integer.parseInt(d[1]);
year=Integer.parseInt(d[2]);
//validating DATE
if(date<1||date>31)
System.out.print("Invalid date........\n");
//checking for leap year
if((year%4==0&&year%100!=0)||(year%400==0))
m[1]=29;
System.out.print("\nDay on 1st January:");
day=b.readLine();
//if else if to assign the correct code for the day
if(day.compareTo("MONDAY")==0) code=1;
else if(day.compareTo("TUESDAY")==0) code=2;
else if(day.compareTo("WEDNESDAY")==0) code=3;
else if(day.compareTo("THURSDAY")==0) code=4;
else if(day.compareTo("FRIDAY")==0) code=5;
else if(day.compareTo("SATURDAY")==0) code=6;
else if(day.compareTo("SUNDAY")==0) code=7;
//To find the elapsed days
if(mon!=1)
{
for(int i=0;i<mon-1;i++)
{
total=total+m[i];
}
total=total+date-1;
}
else
{
total=date-1;
}
equvt=total%7;
//loop to find the day for the given date
for(int k=1;k<=equvt;k++)
{
if(code==7)
{ code=1;}
else
code++;
}
System.out.print("\nOUTPUT\n");
System.out.print("Day on "+date+"/"+mon+"/"+year+"\t:"+week[code-1]);
}//main
}//class

//2007 Q.no.2 To print the words in reverse order without any


//punctuation marks other than blanks
import java.io.*;
public class Q2_2007
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int N,i;
String sen[]={"","","",""},words[];
words=new String[20];
//loop to intialize words[i] to empty to avoid displaying the word 'null' in the output
for(i=0;i<20;i++)
words[i]="";
System.out.print("INPUT:\n");
N=Integer.parseInt(b.readLine());
//LOOP TO ENTER THE SENTENCES AND STORE THEM IN SEN1[]
for(i=0;i<N;i++)
{
sen[i]=b.readLine();
}
String newsen="";
//LOOP TO extract EACH SENTENCE & STORE THEM IN NEWSEN
for(i=N-1;i>=0;i--)
{
newsen=sen[i]+" "+newsen;
}
System.out.print(newsen);
int c=0,now=0;
int l=newsen.length();newsen=" "+newsen;
//LOOP TO EXTRACT EACH CHARACTER & STORE ONLY ALPHABETS IN WORDS[]
for(i=l-1;i>=0;i--)
{
if(Character.isLetter(newsen.charAt(i)))
{
words[now]=newsen.charAt(i)+words[now];
}
//TO CHECK IF A WORD IS REACHED
else if(newsen.charAt(i)==' ')
{
now++;
}
}
//TO PRINT THE DESIRED OUTPUT
System.out.println("OUTPUT:");
for(i=0;i<now;i++)
System.out.print(words[i]+" ");
}//end of main
}//end of class

//2007 Q.no.3 Program to determine the unique digit integers in the range
//between m and n (both inclusive) and output them
import java.io.*;
class Q3_2007
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int a[]={0,0,0,0,0,0,0,0,0,0};
int m,n;
System.out.print("INPUT\n");
System.out.print("m=");
m=Integer.parseInt(b.readLine());
System.out.print("n=");
n=Integer.parseInt(b.readLine());
System.out.print("\nOUTPUT:\n");
System.out.print("THE UNIQUE DIGIT INTEGERS ARE:-\n");
int freq=0;
//loop to print unique digit integers in the range m and n
for(int i=m;i<=n;i++)
{
int no=i,f=1;
//loop to increment each digit's counter
while(no>0)
{
int r=no%10;
a[r]++;
no=no/10;
}
//loop to check each digit's frequency
for(int j=0;j<10;j++)
{
if(a[j]>1)
{f=0;break;}
}
//condition to print unique digit integer
if(f==1)
{
System.out.print(i+",");
freq++;
}
//loop to initialize the counter
for(int j=0;j<10;j++)
{
a[j]=0;
}
}//I loop end
System.out.print("\nFREQUENCY OF UNIQUE-DIGIT INTEGERS IS:"+freq);
}
}

//Q1.2008 SMITH NUMBER REFER YOUR RECORD


/*2008.Q2. A sentence is terminated by either ".","?' or "!" followed by a space. Input a piece of
text consisting of maximum 10 sentences. WAP to obtain the length of the sentence and the
frequency of vowels in each sentence.
*/
import java.io.*;
class Q2_2008
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String s=b.readLine();
String a[]={"","","","","","","","","","",""};
//or for(int i=0;i<10;i++){a[i]="";} to avoid printing the word null
int now[],vowels[];
now=new int[20];
vowels=new int[20];
int k=0,w=0;
//Loop to extract the sentences
int l=s.length();
for(int i=0;i<l;i++)
{
if(s.charAt(i)=='.'||s.charAt(i)=='?'||s.charAt(i)=='!')
{
now[w]=now[w]+1;
a[k]=a[k]+s.charAt(i);w++;
//System.out.print(a[k]);
i++;//to avoid storing the leading blank space in a sentence
k++;
}
else
{
if(s.charAt(i)==' ')now[w]=now[w]+1;
if(s.charAt(i)=='A'||s.charAt(i)=='E'||s.charAt(i)=='I'||s.charAt(i)=='O'||
s.charAt(i)=='U')vowels[w]=vowels[w]+1;
a[k]=a[k]+s.charAt(i);

}
}
//Print the desired output
System.out.println("Sentence No of vowels No of words");
for(int i=0;i<k;i++)
{
System.out.println((i+1)+"\t"+vowels[i]+"\t"+now[i]);
}
System.out.println("Sentence No of words/vowels");
for(int i=0;i<k;i++)
{
System.out.print((i+1)+"\t\t");
for(int j=1;j<=3*vowels[i];j++)
System.out.print("V");

System.out.println();

for(int j=1;j<=3*now[i];j++)
System.out.print("W");

System.out.println();
}
}//end of main
}//end of class

//Q3.2008 LARGEST,SECOND LARGEST OF THE SQUARE MATRIX AND PRINTING THE


//DIAGONAL ELEMENTS IN ASCENDING ORDER
//REFER YOUR RECORD

/* Q1-2009 Design a program to accept a day number between 1 to 366, year(in 4 digits) from the
user to generate and display the corresponding date. Also accept 'N'(1<=N<=100) from the user
to compute and display the future date corresponding to 'N' days after
* the generated date.
* Example:
* INPUT
* DAY NUMBER:360
* YEAR:2008
* DATE AFTER N:10
* OUTPUT
* 25TH DECEMBER 2008
* DATE AFTER 10 DAYS: 4TH JANUARY 2009 */
import java.io.*;
public class day1
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int d[]={31,28,31,30,31,30,31,31,30,31,30,31};
String mon[]={"Jan","Feb","Mar","APr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
int day,y,N;
System.out.print("DAY NUMBER :");
day=Integer.parseInt(b.readLine());
System.out.print("YEAR ");
y=Integer.parseInt(b.readLine());
System.out.print("DATE AFTER(N) ");
N=Integer.parseInt(b.readLine());
boolean flag=false;
//to check the leap year
if(y%400==0 || (y%4==0 &&y%100!=0)){ d[1]=29;flag=true;}
System.out.println("OUTPUT");
int sum=0,i;
//if statement to print the suffix
for(i=0; ;i++)
{ if(sum+d[i]>day)break;
sum=sum+d[i];

}
int d1=day-sum;
System.out.print(d1);
//if statement to print the suffix
if(d1==1||d1==21||d1==31)
System.out.println("ST "+mon[i]+"\t"+y);
else if(d1==2||d1==22)
System.out.println("ND "+mon[i]+"\t"+y);
else if(d1==3||d1==23)
System.out.println("RD "+mon[i]+"\t"+y);
else
System.out.println("TH "+mon[i]+"\t"+y);
System.out.print("DATE AFTER "+N+" DAYS : ");
int daf=day+N;
//if date exceeds 1 year after N days
if(daf>366&&flag==true){day=daf-366;y=y+1;}
else if(daf>365&&flag==false){day=daf-365;y=y+1;}
else day=daf;
sum=0;
//loop to determine the corresponding date
for(i=0; ;i++)
{ if(sum+d[i]>day)break;
sum=sum+d[i];

}
d1=day-sum;
System.out.print(d1);
//if statement to print the suffix
if(d1==1||d1==21||d1==31)
System.out.println("ST "+mon[i]+"\t"+y);
else if(d1==2||d1==22)
System.out.println("ND "+mon[i]+"\t"+y);
else if(d1==3||d1==23)
System.out.println("RD "+mon[i]+"\t"+y);
else
System.out.println("TH "+mon[i]+"\t"+y);
}
}

/* 2009.Q2. Allow the user to input positive integers in the matrix of maximum size 20.
* 1.sort the elements of the outer row and column elements in ascending order
* 2.calculate the sum of the outer row and column elements............ */
import java.io.*;
class Q2_2009
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int A[][],OUT[],M,N,c=0,i,j,k,sum=0;
System.out.print("M=");
M=Integer.parseInt(b.readLine());
System.out.print("N=");
N=Integer.parseInt(b.readLine());
A=new int[M][N];
OUT=new int[M*N];
//Loop to enter the matrix elements
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
A[i][j]=Integer.parseInt(b.readLine());
}
//loops to extract the boundary elements from the matrix and storing them to OUT[ ]
if(M==N)
{
for(i=0;i<M;i++)
{
OUT[c]=A[0][i];c++;
}
for(k=1,j=N-1;k<N;k++)
{
OUT[c]=A[k][j];c++;
}
for(k=N-2,j=N-1;k>=0;k--)
{
OUT[c]=A[j][k];c++;
}
for(k=0,j=N-2;j>0;j--)
{
OUT[c]=A[j][k];c++;
}
}//if
//bubble sort the outer elements and adding the boundary elements
for(i=0;i<c-1;i++)
{
for(j=0;j<c-1;j++)
{
if(OUT[j]>OUT[j+1])
{
int t=OUT[j];
OUT[j]=OUT[j+1];
OUT[j+1]=t;
}
}
}//I loop
c=0;
//loops to store back the sorted boundary elements into A[][]
for(i=0;i<M;i++)
{
A[0][i]=OUT[c];c++;
}
for(k=1,j=N-1;k<N;k++)
{
A[k][j]=OUT[c];c++;
}
for(k=N-2,j=N-1;k>=0;k--)
{
A[j][k]=OUT[c];c++;
}
for(k=0,j=N-2;j>0;j--)
{
A[j][k]=OUT[c];c++;
}
System.out.println();
//loop to print the rearranged matrix
System.out.println("REARRANGED MATRIX");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
//loop to print the boundary elements of the matrix
System.out.println("BOUNDARY ELEMANTS");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if((i!=0&&j!=0)&&((i==j)||(i+j==(N-1)))&&(i!=N-1&&j!=N-1) )
{System.out.print("\t");
}
else
{sum=sum+A[i][j];System.out.print(A[i][j]+"\t");}
}
System.out.println();
}//i
System.out.print("SUM OF THE OUTER ROW AND COLUMN ELEMENTS = "+ sum);
}//main
}//class
//Q3.2009.//INPUT :The lines are printed in reverse order
//OUTPUT:In the are lines order printed reverse
import java.io.*;
class Q3_2009
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a sentence\n");
String s=b.readLine();
int i=0,count=0;
String words[]=new String[20];
//to avoid printing the word null
for(i=0;i<20;i++)
words[i]="";
s=s+" ";//for the last word
int l=s.length();
//loop to extract the words and store them in words[]
for(i=0;i<l;i++)
{ char c=s.charAt(i);
if(s.charAt(i)!=' ')
{words[count]=words[count]+c;}
else
{
count++;
}
}
//loop to arrange the words in ascending order of their length
for(i=0;i<count-1;i++)
{
for(int j=0;j<count-1;j++)
{
if(words[j].length()>words[j+1].length())
{
String t=words[j];
words[j]=words[j+1];
words[j+1]=t;
}
}
}
//loop to print the arranged words
for(i=0;i<count;i++)
{ for(int j=0;j<words[i].length();j++)
{
if(i==0&&j==0)
System.out.print(Character.toUpperCase(words[i].charAt(0)));//to print the first letter of first
word in upper case
else
System.out.print(Character.toLowerCase(words[i].charAt(j)));
}
System.out.print(" ");
}
}
}

// SPECIMEN QUESTION PAPER


Q1) Design a program which inputs a date in six digit format i.e. 141296. Test the validity of the date and print
the date in full form. If the date is invalid print “Invalid date”
Example: Input: 141296 Output:14 th December, 96 Valid date
Q2) WAP to fill in 2D array in a circular fashion with natural numbers from 1 to n 2 given ‘n’ as input.
Example: if n=4, n2=16, then the array will be
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Q3) ACCEPT A PARAGRAPH OF TEXT CONSISTING OF SENTENCES TERMINATED BY EITHER
".","?",":" OR "!" FOLLOWED BY A SPACE.ASSUME THAT THERE CAN BE MAXIMUM 5 SENTENCES
IN A PARAGRAPH. WAP TO 1) ARRANGE THE SENTENCES IN ALPHABETICAL ORDER OF WORDS
SENTENCE BY SENTENCE. 2) SEPERATE THE WORDS BEGIN WITH A VOWEL
import java.io.*;
public class Sent
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String s;
s=b.readLine();
int l=s.length();
String a[]={"","","","",""};

int k=0;
//Loop to extract the sentences
for(int i=0;i<l;i++)
{
if(s.charAt(i)=='.'||s.charAt(i)=='?'||s.charAt(i)=='!')
{
a[k]=a[k]+s.charAt(i);
i++;//to avoid storing the leading blank space in a sentence
k++;
}
else
{
a[k]=a[k]+s.charAt(i);

}
}
int c=0;
for(int i=0;i<k;i++)
{
String senw[]={"","","","",""};
a[i]=a[i]+" ";//to extract the last word of the sentence a[i]
int len=a[i].length();
for(int m=0;m<len;m++)
{
if(a[i].charAt(m)!=' ')
{ senw[c]=senw[c]+a[i].charAt(m);}
else
{ c++;
}
}
for(int j=0;j<c-1;j++)//to sort the words in alphabetical order using bubble sort of senw[ ] array
{
for(int k1=0;k1<c-1;k1++)
{ //not to compare the special characters
if((senw[k1].compareToIgnoreCase(senw[k1+1])>0)&&
Character.isLetter(senw[k1+1].charAt(0)))
{
String t=senw[k1];
senw[k1]=senw[k1+1];
senw[k1+1]=t;
}
}
}
//Loop to print the arranged words
for(int h=0;h<c;h++)
System.out.print(senw[h]+" ");
c=0;
}// i loop
}//main
}//class

Important Instructions for the computer practical examination: 24/2/2010


1. Write the Question number
2. Write aim of the program then algorithm
3. If your program contains user defined function other than main function then
write separate algorithm for it
4. Write Java program and document it(write comments)
5. Write important variables description
6. If there is any formula used mention it
7. Before compiling you must save the program
8. You must have 2 copies of your program(use save as option)
9. Don’t forget to write the question no, aim and the comments in the typed program
10. If the program runs successfully, show the output to the external examiner and
paste the sample inputs and outputs as given in the question paper along with the
program.
11. Don’t forget to write the index no on the print out
12. Attach the print out at the back of your answer paper.
13. Don’t forget to bring your record work
ALL THE BEST
//2005 Q.no.3 Printing the permutations(anagrams) of a word
//Q3_2005 PROGRAM TO PRINT ALL POSSIBLE PERMUTATIONS OF A WORD
/* MAIN LOGIC :
/*PERMUTATIONS CAN BE FOUND OUT BY LEFT SHIFTING AS FOLLOWS
/* 1234
/* / | |
/* 2341 3412 4123 1234 //AFTER SHIFTING ALL THE ‘N’ DIGITS
/* / | / | / | /|
/*2413 2134 2341 . . . 4231 4312 4123 . . . //KEEPING FIRST DIGIT FIXED
/* / / / / / / / / / / / /
/* 2143 2134 4213 4231 .............. //KEEPING 1 & 2 DIGITSFIXED
/*LIKEWISE AFTER THE LAST STEP THAT IS KEEPING ALL THE DIGITS FIXED
/*EXCEPT THE LAST ONE & DO SHIFTING WITH THOSE TWO DIGITS, WE WILL GET n!*/
import java.io.*;
public class Q3_2005
{
public static void main()throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String s; int i;
System.out.print("Enter a word : ");
s=b.readLine();
System.out.println(s);
int l=s.length();
char c[]=new char[l];
for(i=0;i<l;i++)
c[i]=s.charAt(i);
System.out.println("Possible permutations are :");
per(c,l,l);
}
//func to display the sequence
public static void dis(char a[],int n)
{
for(int k=n-1;k>=0;k--)
{
System.out.print(a[k]);
}
System.out.println();
}//fn end
//function to left shift last n elements of the array by one position
public static void shift(char a[],int l)
{
char s;
s=a[l-1];
for(int i=l-1;i>0;i--)
a[i]=a[i-1];
a[0]=s;
}
//func to find all possible arrangements
public static void per(char a[],int m,int n)
{
for(int i=0;i<m;i++)
{
if(m>1)
{
shift(a,m);
per(a,m-1,n); //generating the tree
}
else
{
dis(a,n); //displaying the leaf nodes of the tree
}
}//i loop
}//fn end
}//class
/*OUTPUT
------
Enter a word: 123
Possible permutations are :
213
231
321
312
132
123
*/

You might also like