Вы находитесь на странице: 1из 28

11. Write a program to check whether a number is a Kaprekar No.

or
not.( The representation of whose square in that base can be split
into two parts that add up to the original number again. For
instance, 45 is a Kaprekar number, because 45² = 2025 and
20+25 = 45. )

Code:

import java.io.*;
class Kaprekar
{
public static void main(String args[])throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader (ir);
System.out.println("Enter No. :");
int x=Integer.parseInt(br.readLine());
int num=x*x,no=x,digit=0;;
int rev=0;
while(no>0)
{
digit++;
no=no/10;
}
no=num;
while(digit > 0)
{
rev=rev*10+no%10;
no=no/10;
digit--;
}
int r=0;
while(rev > 0)
{
r=r*10+rev%10;
rev=rev/10;
}
if((r+no)==x)
{
System.out.print("It is a Kaprekar No. ");
}
else
{
System.out.print("It is not a Kaprekar No. ");
}
}
}}//Copyright © Srijit Banerjee 2010
Output :

Enter No. :
9
It is a Kaprekar No.

Enter No. :
297
It is a Kaprekar No.

Enter No. :
1256
It is not a Kaprekar No.
12. Write a program, which inputs a positive number n and prints the
possible consecutive natural number combinations, which when
added give n.

Code:

import java.io.*;
import java.lang.*;
class SumNum
{
public static void main(String args[])throws IOException
{
int n=0,s=0,i=0,j=0,k=0;
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter number:");
n=Integer.parseInt(br.readLine());
System.out.println("n="+n);
for(i=1;i<n;i++)
{
s=i;
for(j=i+1;j<n;j++)
{
s=s+j;
if(s==n)
{
for(k=i;k<=j;k++)
{
System.out.print(k+" ");
}
System.out.println(" ");
}
}
}
}
}}//Copyright © Srijit Banerjee 2010

Output:

Enter number:
9
n=9

234
45
Enter number:
15

n=15
12345
456
78

Enter number:
21
n=21
123456
678
10 11
13. Write a program to input and store n integers in a single
subscripted variable and print each number with their frequencies
of existence.

Code:

import java.io.*;
import java.lang.*;
class NumFrequency
{
public static void main(String args[])throws IOException
{
int i=0,j=0,t=0,p=0;
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter array limit : ");
int x=Integer.parseInt(br.readLine());
int ar[]=new int[x];
for(i=0;i<x;i++)
{
System.out.println("Input no:");
ar[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(ar[i]>ar[j])
{
p=ar[i];
ar[i]=ar[j];
ar[j]=p;
}
}
}
for(i=0;i<x;i++)
{
p=ar[i];
for(j=0;j<x;j++)
{
if(p==ar[j])
{
t=t+1;
ar[j]=0;
}
}
if(p==0)
{
t=0;
continue;
}
else
{
System.out.print(p+" X "+t);
t=0;
}
System.out.println(""); } } }}//Copyright © Srijit Banerjee 2010

Output:

Enter array limit :


12
Input no:
3
Input no:
3
Input no:
5
Input no:
7
Input no:
3
Input no:
3
Input no:
5
Input no:
8
Input no:
9
Input no:
12
Input no:
7
Input no:
7
3X4
5X2
7X3
8X1
9X1
12 X 1
14. Write a program to find the duration for which each user logged
and output record of the user who logged for the longest duration.

Code:

import java.io.*;
import java.lang.*;
class UserLogin
{
public static void main(String args[])throws IOException
{
String inh,inm,onh,onm,dnh,dnm,odh,odm,m2,m3,m4,m5;
int
n=0,i=0,j=0,m6,m1,m7,m8,cinh,d1=0,cinm,conh,conm,cidh,cidm,codh,cod
m;
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Maximum login time 48 hours,maximum users 100:");
System.out.println("Enter no of users:");
n=Integer.parseInt(input.readLine());
int user[]=new int[n];
String li[]=new String[n];
String lid[]=new String[n];
String lo[]=new String[n];
String lod[]=new String[n];
int diff[]=new int[n];
int h[]=new int[n];
int m[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("Enter user identification number:");
user[i]=Integer.parseInt(input.readLine());
System.out.println("Enter login time:(hh:mm)");
li[i]=input.readLine();
System.out.println("Enter login date:(dd-mm)");
lid[i]=input.readLine();
System.out.println("Enter logout time:(hh:mm)");
lo[i]=input.readLine();
System.out.println("Enter logoutdate:(dd-mm)");
lod[i]=input.readLine();
}
for(i=0;i<n;i++)
{
inh=li[i].substring(0,2);
inm=li[i].substring(3);
cinh=Integer.parseInt(inh);
cinm=Integer.parseInt(inm);
onh=lo[i].substring(0,2);
onm=lo[i].substring(3);
conh=Integer.parseInt(onh);
conm=Integer.parseInt(onm);
dnh=lid[i].substring(0,2);
dnm=lid[i].substring(3);
cidh=Integer.parseInt(dnh);
cidm=Integer.parseInt(dnm);
odh=lod[i].substring(0,2);
odm=lod[i].substring(3);
codh=Integer.parseInt(odh);
codm=Integer.parseInt(odm);
if((lid[i]).equals(lod[i]))
{
diff[i]=(((conh*60)+conm)-((cinh*60)+cinm));
h[i]=diff[i]/60;
m[i]=diff[i]%60;
}
else
{
d1=codh-cidh;
if(d1==1)
{
diff[i]=(((1440-((cinh*60)+cinm)))+((conh*60)+conm));
}
else if(d1>1)
{
diff[i]=(1440)+(((1440-((cinh*60)+cinm)))+((conh*60)+conm));
}
}
h[i]=diff[i]/60;
m[i]=diff[i]%60;
}

for(i=0;i<n;i++)
{
System.out.println(" User Login Logout
Duration ");
System.out.println("Identification Time & Date Time & Date
Hours:Mins");
System.out.println(" "+user[i]+" "+li[i]+" "+lid[i]+"
"+lo[i]+" "+lod[i]+" "+h[i]+" "+m[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(diff[i]>diff[j])
{
m6=diff[i];
diff[i]=diff[j];
diff[j]=m6;
m1=user[i];
user[i]=user[j];
user[j]=m1;
m2=li[i];
li[i]=li[j];
li[j]=m2;
m3=lid[i];
lid[i]=lid[j];
lid[j]=m3;
m4=lo[i];
lo[i]=lo[j];
lo[j]=m4;
m5=lod[i];
lod[i]=lod[j];
lod[j]=m5;
m7=h[i];
h[i]=h[j];
h[j]=m7;
m8=m[i];
m[i]=m[j];
m[j]=m8;
}
}
}
System.out.println("The user who logged in for longest duration:");
System.out.println(" "+user[n-1]+" "+li[n-1]+" "+lid[n-1]+"
"+lo[n-1]+" "+lod[n-1]+" "+h[n-1]+" "+m[n-1]);

}
}

Output:

Enter no of users:
3
Enter user identification number:
149
Enter login time:(hh:mm)
20:10
Enter login date:(dd-mm)
20-12
Enter logout time:(hh:mm)
02:50
Enter logoutdate:(dd-mm)
21-12
Enter user identification number:
173
Enter login time:(hh:mm)
12:30
Enter login date:(dd-mm)
20-12
Enter logout time:(hh:mm)
12:30
Enter logoutdate:(dd-mm)
21-12
Enter user identification number:
142
Enter login time:(hh:mm)
16:20
Enter login date:(dd-mm)
20-12
Enter logout time:(hh:mm)
16:30
Enter logoutdate:(dd-mm)
20-12
User Login Logout Duration
Identification Time & Date Time & Date Hours:Mins
149 20:10 20-12 02:50 21-12 6 40
173 12:30 20-12 12:30 21-12 24 0
142 16:20 20-12 16:30 20-12 0 10

The user who logged in for longest duration:


173 12:30 20-12 12:30 21-12 24 0
15. Write a program to accept a day number, year from user to
generate and display the corresponding date. Also accept ‘N’ from
user to compute and display the future date corresponding to ‘n’
days after generated date.

Code:

import java.io.*;
class DayAfter
{
public static void main (String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
int year, day, C=0, J, N ,D, M, Y, n=0, q; String tag;
int days_of_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String month[]={"
","JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGAST",
"SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
System.out.println("INPUT :");
System.out.print("DAY NUMBER :");
day=Integer.parseInt(in.readLine());
if(day<1 && day>366)
{
System.out.println("Invalid day number :");
System.exit(0);
}// end if
System.out.print("YEAR :");
year=Integer.parseInt(in.readLine());
System.out.print("DATE AFTER (N) :");
N=Integer.parseInt(in.readLine());
if(year%4 == 0)
days_of_month[2]=29;
else
days_of_month[2]=28;
for(J=1;J<=12;J++)
{
C=C+days_of_month[J];
if(C>day)
{
n=J;
break;
}// end if
}// end for
C=0;
for(J=1; J<n; J++)
{
C=C+days_of_month[J];
}// end for
day=day-C;
q=day%10;
if(q==1) tag="ST";
else if(q==2) tag="ND";
else if(q==3) tag="RD";
else tag="TH";
System.out.println("OUTPUT:");
if(day==0)
{
if(days_of_month[n-1]==30) tag="TH";
else tag="ST";
System.out.println(days_of_month[n-1]+tag+" "+year);
D=days_of_month[n-1]; M=n-1;
}
else
{
System.out.println(day+tag+" "+month[n]+" "+year);
D=day; M=n;
}
Y=year; C=0;
while(C<N)
{
C++;
D++;
if(D>days_of_month[M])
{
D=1;
M++;
}
if(M>12)
{
M=1;
Y++;
if(Y%4==0)
days_of_month[2]=29;
else
days_of_month[2]=28;
}
}// end while
q=D%10;
if(q==1)
{ tag="ST"; }
else if (q==2)
{ tag="ND"; }
else if (q==3)
{ tag="RD"; }
else
{ tag="TH"; }
System.out.print("DATE AFTER "+N+" DAYS :");
System.out.println(D+tag+" "+month[M]+" "+Y);
}// end main() function
}// end class

Output:

INPUT :
DAY NUMBER :233
YEAR :2008
DATE AFTER (N) :17
OUTPUT:
20TH AUGAST 2008
DATE AFTER 17 DAYS :6TH SEPTEMBER 2008

INPUT :
DAY NUMBER :360
YEAR :2008
DATE AFTER (N) :45
OUTPUT:
25TH DECEMBER 2008
DATE AFTER 45 DAYS :8TH FEBRUARY 2009
16. Write a program to arrange the words in ascending order.

Code:

import java.io.*;
class StringLength
{
public static void main(String args[])throws IOException
{
int t=0,i,j,k=0,k1=0,p,m;
String s,w="",w2="",temp="";
char ch,h;
String a[]=new String[100];
String b[]=new String[100];

BufferedReader input=new BufferedReader(new


InputStreamReader(System.in));
//To input the string from user
System.out.println("Enter any text:");
s=input.readLine();
p=s.length()-1;
//Checking the program for the full-stop
if((s.charAt(p)!='.')||(p>80))
{
System.out.println(" ERROR: (i) Enter text with a full-stop in the end; (ii) Do
not exceed 80 characters.");
System.exit(0);
}
else
{
//Extraction of words from string
w="";
k=0;
t=0;
for(i=0;i<s.length();i++)
{
ch=s.charAt(i);
if((ch==' ')||(ch=='.'))
{
t++;
if((w!="")&&(t==1))//Eliminating extra space
{
a[k]=w;//Adding elements into array
k++;
w="";
}
}
else
{
w=w+ch;
t=0;
}
}

//Arranging the array in alphabetical order


k1=0;
for(i=65;i<=91;i++)
{
w="";
for(j=0;j<k;j++)
{
w=a[j];
ch=w.charAt(0);
p=(int)ch;
if((p==i)||(p==(i+32)))
{
if(p==i)
{
b[k1]=w;
k1++;
}
else
if(p==(i+32))
{
p=p-32;//Converting into upper-case
h=(char)p;

w2="";
for(m=1;m<w.length();m++)
{
w2=w2+w.charAt(m);
}
w2=h+w2;
b[k1]=w2;
k1++;}}}}
//Arranging the array according to word-length
for(i=0;i<k1;i++)
{
for(j=0;j<k1-i-1;j++)
{
if(b[j+1].length()>b[j].length())
{
temp=b[j+1];
b[j+1]=b[j];
b[j]=temp;}}}
//Making the final string
w="";
for(i=k1-1;i>=0;i--)
{
w=w+b[i]+" ";
}
w=w+".";
System.out.println("Your string:");
System.out.println(w);}}} }//Copyright © Srijit Banerjee 2010

Output:

Enter any text:


The lines are printed in reverse order.
Your string:
In The Are Order Lines Reverse Printed .

Enter any text:


Print the sentence in ascending order.
Your string:
In The Print Order Sentence Ascending .

Enter any text:


I love my country.
Your string:
I My Love Country .
17. Write a program to print the words in reverse order.

Code:

import java.io.*;
class StringRev
{
public static void main(String args[])throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
String str=" ";
int t=0,p=0,len,c=0,i=0,l,n;
char h;
System.out.println("Enter no. of sentences : ");
n=Integer.parseInt(br.readLine());
String s[]=new String[n];
if(n>20)
{
System.out.println("ERROR !!! 'n' must be less than 20...");
}
else
{
for(i=0;i<n;i++)
{
System.out.println("Enter sentence "+i);
s[i]=br.readLine();
l=s[i].length();
if(l>=80)
{
System.out.println("ERROR !!! Length must be less than 80
characters...");
break;
}
else
{
str=str+s[i]+" ";
}
}
}
//System.out.println(str);
String x=" ";
String s1[]=new String[50];
len=str.length();
for(i=0;i<len;i++)
{
h=str.charAt(i);
if((h>32)&&(h<=64))
{
continue;
}
else
{
x=x+h;
}
}
int len1= x.length();
for(i=0;i<len1;i++)
{
h=x.charAt(i);
if((h==' ')||(h=='.'))
{
p=i;
s1[c]=x.substring(t,p);
t=(p+1);
c++;
}
}
for(i=c-1;i>=0;i--)
{
System.out.print(s1[i]+" ");
}
}
}}//Copyright © Srijit Banerjee 2010

Output:

Enter no. of sentences :


2
Enter sentence 0
Emotions controlled and directed to work, is character.
Enter sentence 1
By Swami Vivekananda.
Vivekananda By Swami character is work to directed and controlled Emotions

Enter no. of sentences :


1
Enter sentence 0
Do not judge a book by its cover.
cover its by book a judge not Do
18. Write a program to fill in a two dimensional array in circular
fashion.

Code:

import java.io.*;
class CircularMatrix
{
public static void main(String[] args)throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter array limit : ");
int dim=Integer.parseInt(br.readLine());
int[][] spiralArray = new int[dim][dim];
int num = (int) Math.ceil((dim) / 2.0);
int j;
int sideLen = dim;
int currNum = 1;
for (int i = 0; i < num; i++)
{
// do top side
for (j = 0; j < sideLen; j++)
{
spiralArray[i][i + j] = currNum++;
}
// do right side
for (j = 1; j < sideLen; j++)
{
spiralArray[i + j][dim - 1 - i] = currNum++;
}
// do bottom side
for (j = sideLen - 2; j > -1; j--)
{
spiralArray[dim - 1 - i][i + j] = currNum++;
}
// do left side
for (j = sideLen - 2; j > 0; j--)
{
spiralArray[i + j][i] = currNum++;
}
sideLen -= 2;
}
for (int i=0;i<dim;i++)
{
for (j=0;j<dim;j++)
{
System.out.print(" "+spiralArray[i][j] + " ");
}
System.out.println(" ");
}
}
}//Copyright © Srijit Banerjee 2010

Output:

Enter array limit :


4

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
19. Write a program to sort the boundary elements of an array then
print the rearranged matrix and then print only the boundary
elements.

Code:

import java.io.*;
class ArrArrange
{
public static void main(String[] args)throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
int a[]=new int[50];
System.out.println("Enter array size : ");
int n=Integer.parseInt(br.readLine());
int dim=n;
int[][] spiralArray = new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("Enter element : ");
spiralArray[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("_______________");
System.out.println("ORIGINAL MATRIX");
System.out.println("_______________");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(+spiralArray[i][j]+" ");
}
System.out.println(" ");
}

int num = (int) Math.ceil((dim) / 2.0);


int j;
int sideLen = dim;
int currNum = 1;
int t=0,s=0;
for (int i = 0; i < 1; i++)
{
// do top side
for (j = 0; j < sideLen; j++)
{
a[t]=spiralArray[i][i + j];
t++;
}
// do right side
for (j = 1; j < sideLen; j++)
{
a[t]=spiralArray[i + j][dim - 1 - i];
t++;
}
// do bottom side
for (j = sideLen - 2; j > -1; j--)
{
a[t]=spiralArray[dim - 1 - i][i + j];
t++;
}
// do left side
for (j = sideLen - 2; j > 0; j--)
{
a[t]=spiralArray[i + j][i];
t++;
}
sideLen -= 2;
}
for (int i=0;i<t;i++)
{
for (j=0;j<t;j++)
{
if(a[i]<a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for (int i=0;i<t;i++)
{
s=s+a[i];
}
t=0;
sideLen=dim;
for (int i = 0; i < 1; i++)
{
// do top side
for (j = 0; j < sideLen; j++)
{
spiralArray[i][i + j]=a[t];
//System.out.print(" ist sec"+a[t] + " ");
t++;
}
// do right side
for (j = 1; j < sideLen; j++)
{
spiralArray[i + j][dim - 1 - i]=a[t];
//System.out.print(" 2nd sec"+a[t] + " ");
t++;
}
// do bottom side
for (j = sideLen - 2; j > -1; j--)
{
spiralArray[dim - 1 - i][i + j]=a[t];
//System.out.print(" 3rd sec"+a[t] + " ");
t++;
}
// do left side
for (j = sideLen - 2; j > 0; j--)
{
spiralArray[i + j][i]=a[t];
//System.out.print(" 4th sec"+a[t] + " ");
t++;
}
sideLen -= 2;
}
System.out.println("_________________");
System.out.println("REARRANGED MATRIX");
System.out.println("_________________");
for (int i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
System.out.print(" "+spiralArray[i][j] + " ");
}
System.out.println(" ");
}
System.out.println("_________________");
System.out.println("BOUNDARY ELEMENTS");
System.out.println("_________________");
for(int i=0;i<n;i++)
System.out.print(+spiralArray[0][i]+" ");
System.out.print("\n");
for(int i=1;i<n-1;i++)
{
for(j=0;j<n;j++)
{
if(j==0 || j==n-1)
System.out.print(+spiralArray[i][j]+" ");
else
System.out.print(" ");
}
System.out.print("\n");
}
for(int i=0;i<n;i++)
System.out.print(+spiralArray[n-1][i]+" ");
System.out.println(" ");
System.out.println("_________________________________________");
System.out.println("SUM OF OUTER ROW AND COLOUM ELEMENTS : "+s);
System.out.println("_________________________________________");

}
}//Copyright © Srijit Banerjee 2010

Output:

Enter array size :


3
Enter element :
1
Enter element :
7
Enter element :
4
Enter element :
8
Enter element :
2
Enter element :
5
Enter element :
6
Enter element :
3
Enter element :
9
_______________
ORIGINAL MATRIX
_______________
174
825
639
_________________
REARRANGED MATRIX
_________________
1 3 4
9 2 5
8 7 6
_________________
BOUNDARY ELEMENTS
_________________
134
9 5
876
________________________________________________
SUM OF OUTER ROW AND COLOUM ELEMENTS : 43
________________________________________________

Enter element :
8
Enter element :
13
Enter element :
8
Enter element :
4
Enter element :
15
Enter element :
6
Enter element :
3
Enter element :
11
Enter element :
7
Enter element :
12
Enter element :
23
Enter element :
8
_______________
ORIGINAL MATRIX
_______________
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
_________________
REARRANGED MATRIX
_________________
1 2 4 5
23 13 8 7
15 6 3 8
12 11 9 8
_________________
BOUNDARY ELEMENTS
_________________
1 2 4 5
23 7
15 8
12 11 9 8
_________________________________________________
SUM OF OUTER ROW AND COLOUM ELEMENTS : 105
_________________________________________________
20. Write a 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. [Note: only the denomination used should be
displayed and amount should be less than 6 digits only].

Code:

import java.io.*;
class BankNote
{
public static void main(String args[])throws IOException {
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int rev=0,amount,dummy;
System.out.println("Enter the Amount:");
amount=Integer.parseInt(br.readLine());
if(amount>99999)
{
System.out.println("Invalid Amount...");
}
dummy=amount;
while(dummy>0){
rev=rev*10+dummy%10;
dummy=dummy/10;
}
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;
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);
}}}//Copyright © Srijit Banerjee 2010

Output:

Enter the Amount:


526325
Invalid Amount...

Enter the Amount:


15445

DENOMINATORS:

1000 X 15 = 15000
100 X 4 = 400
20 X 2 = 40
5X1=5

Enter the Amount:


14836

DENOMINATORS:

1000 X 14 = 14000
500 X 1 = 500
100 X 3 = 300
20 X 1 = 20
10 X 1 = 10
5X1=5
1X1=1