1. //WAP in java to print "HAPPY BIRTHDAY" message
ten times on the screen.
class l1
{
public static void main(String args[])
{
int i;
for(i=0;i<10;i++)
System.out.println("HAPPY BIRTHDAY");
}
}
2. //WAP in java to print the
alternate week day names.Do not input the week day number.
class l2
{
public static void main(String args[])
{
int i;
for(i=0;i<10;i++)
{
if(i==1)
System.out.println("Sunday");
else if(i= =3)
System.out.println("Tuesday");
else if(i= =5)
System.out.println("Thursday");
else if(i= =7)
System.out.println("Saturday");
}
}
}
3. //WAP in java to print first 100 even number
class l3
{
public static void main(String args[])
{
int i;
for(i=0;i<100;i+=2)
System.out.println(i);
}
}
4. //WAP in java to print all positive odd numbers upto 155
class l4
{
public static void main(String args[])
{
int i;
for(i=1;i<155;i+=2)
System.out.println(i);
}
}
5. //WAP in java to print the table of any number
import java.io.*;
class l5
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int i,c=0;
System.out.println("Enter the number which you want
print the table");
int n=Integer.parseInt(input.readLine());
for(i=1;i<=10;i++)
{
c= n*i;
System.out.println(n+ "*" +i+ "="+c);
}
}
}
6. //WAP to print the first seven multiple of 6
class l6
{
public static void main(String args[])
{
int i,j=6,r;
for(i=1;i<=7;i++)
{
r=j*i;
System.out.println(j+ "*"+i+ "="+r);
}
}
}
7. //WAP in java to print all factors of a number n accepted
by user
import java.io.*;
class l7
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int f=0;
System.out.println("Enter the value of a no. whose
factors are to be printed");
int n=Integer.parseInt(input.readLine());
int i;
System.out.print("Factor=");
for(i=1;i<=n;i++)
{
f=n%i;
if(f= =0)
System.out.println("
"+i);
}
}
}
8. /*WAP in java accept a number from a keyboard and check
if it is a prime number or a non prime number.*/
import java.io.*;
class l8
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int i,j,n;
System.out.println("Enter any number");
n=Integer.parseInt(input.readLine());
for(i=2;i<n;i++)
{
if (n%i= =0)
{
System.out.println("Number is not prime");
break;
}
}
if(i= =n)
System.out.println("Number is prime");
}
}
9. //WAP To enter a number and print the separated digits in
reverse order.
import java.io.*;
class l9
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int c,n,d,r=0;
System.out.println("Enter the digit");
c=n;
do
{
d=n%10;
r=r*10+d;
n=n/10;
}
while(n!=0);
System.out.println("The reversed digit is"+r);
}
}
10. //WAP in java that a number,computes and display the sum
of the digit.
import java.io.*;
class l10
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int n,q=1,s=0;
System.out.println("enter the digit");
n=Integer.parseInt(input.readLine());
while(q!=0)
{
int r=n%10;
q=n/10;
n=q;
s=s+r;
}
System.out.println("The sum of the digit is "+s);
}
}
11. //armstrongseries
class l11
{
public static void main(String args[])
{int s=0,rem=0;
for(int i=1;i<500;i++)
{
int p=i;
s=0;
while(p!=0)
{
rem=p%10;
s=s+rem*rem*rem;
p=p/10;
}
if(s= =i)
{
System.out.println(s);
}
}
}
}
12. /*WAP in java to display the given pattern
12345
1234
123
12
1*/
class l12
{
public static void main(String args[])
{
int i,j;
for(i=7;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
13. /*Wap in java to display the given pattern
1
12
123
1234
12345*/
class l13
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j+ " ");
}
System.out.println();
}
}
}
14. //To display first fifty prime numbers
class l14
{
public static void main(String args[])
{
int i,j;
for(i=2;i<=50;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
{
break;
}
}
if(j= =i)
System.out.println(i);
}
}
}
15. // 1+(1*2)+(1*2*3)+...........10 terms
class l15
{public static void main(String args[])
{int a,s,p;
s=0;
p=1;
for(a=1;a<=10;a++)
{p=p*a;
s=s+p;
}
System.out.println("the sum of series is= "+s);
}
}
16. /*to display the pattern
a
aa
aaa
aaaa
aaaaa*/
class l16
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("a");
}
System.out.println();
}
}
}
17. /*to display the pattern
a
a a
a a a
a a a a
a a a a a
*/
class l17
{
public static void main(String args[])
{
int i,j,k,m;
m=6;
for(i=1;i<=5;i++)
{
for(j=1;j<(m-i);j++)
{
System.out.print(" ");
}
for(k=1;k<=i;k++)
{
System.out.print("
"+"a");
}
System.out.println();
}
}
}
18. /*54321
4321
321
21
1*/
class l18
{
public static void main(String args[])
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
\19. /*1
21
321
4321
54321*/
class l19
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
}
20. //WAP in java to accept 10 different nos and display the
greatest and the smallest nos.
import java.io.*;
class l20
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int a,n,m,min,max;min=0;max=0;
System.out.println("Enter your first no.");
n=Integer.parseInt(input.readLine());
min=n;max=n;
for(a=2;a<=10;a++)
{
System.out.println("Enter your remaining nine
nos");
m=Integer.parseInt(input.readLine());
if(min>m)
min=m;
if(max<m)
max=m;
}
System.out.println("Minimum of 10 given
nos.="+min);
System.out.println("Maximum of 10 given
nos.="+max);
}
}
21. // WAP in Java to check whether the numbers are twin
prime or not.
import java.io.*;
class l21
{ public static void
main(String args[]) throws IOException
{ BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
int n,n1,i;
n = Integer.parseInt(input.readLine());
n1 = Integer.parseInt(input.readLine());
int f=1;
int f1=1;
int p=0;
int q=0;
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
f=0;
break;
}}
for(i=2;i<=n1-1;i++)
{ if(n1%i= =0)
{
f1=0;
break;
}}
if((f= =1 && f1= =1) &&(n1= =2 || n1-n= =2))
System.out.println("The number " + n + "and
" + n1 + " are twin primes");
else
System.out.println("The number " + n + "and
" +n1 +" are not twin primes");
}}
22. // convert binary to decimal
import java.io.*;
class l22
{ public static void main(String args[])throws IOException
{ BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int n=Integer.parseInt(input.readLine());
double a=2.0;
double s=0.0;
double q=0.0;
int i=0;
int r;
while(n!=0)
{ r=n%10;
q=Math.pow(a,i);
s=s+r*q;
n=n/10;
i++;
}
System.out.println(s);
}}
23. // to convert decimal to binary
import java.io.*;
class l23
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int n=Integer.parseInt(input.readLine());
int m[]=new int[100];
int i,j;
i=0;
System.out.println("Decimal number="+n);
int k;
k=0;
while(n!=0)
{
m[i]=n%2;
n=n/2;
i++;
}
for(j=i-1;j>=0;j--)
{
System.out.print(m[j]);
}
}
}
24.//To print the following series : 1, 4,9, 16 ….900 and
print it sum also
import java.io.*;
class l24
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
int i;
int n;
int s=0;
System.out.println("series");
for(i=1;i<=30;i++)
{
n=i*i;
s=s+n;
System.out.println(n);
}
System.out.println("sum of the series:"+s);
}
}
25. //To print the sum of following series : 1 + 1/2
+1/3……+1/20
import java.io.*;
class l25
{
public static void main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int i;
double n=0.0d;
double s=1.0d;
for(i=1;i<=20;i++)
{
n=1.0/i;
s=s+n;
}
System.out.println("sum of the series:"+s);
}
}
26. //To print the sum of following series : 1 + 1/3
+1/5……+1/19
class l26
{
public static void main(String args[])
{
int i;
double n=0.0d;
double s=1.0d;
for(i=1;i<20;i+=2)
{
n=1.0/i;
s=s+n;
}
System.out.println("sum of the series:"+s);
}
}
27. //To print the sum of following series : 1/2
+2/3+3/4……+19/20
class l27
{
public static void main(String args[])
{
int i;
double n=0.0d;
double s=0.0d;
int b=2;
for(i=1;i<=3;i++)
{
n=(double)i/b;
System.out.println(n);
s=s+n;
b++;
}
System.out.println("sum of the series:"+s);
}
}
28. //To print the prime numbers
class l28
{
public static void main(String args[])
{
int i,j;
System.out.println("Prime Numbers are");
for(i=1;i<=100;i++)
{
int f=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
f=1;
break;
}
}
if(f= =0)
System.out.println(i);
}
}
}
29. //To print the
following series : 2,5,10,17…
import java.io.*;
class l29
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter number");
int n=Integer.parseInt(input.readLine());
int a;
int s=1;
System.out.println("The series:");
for(a=1;a<n;a+=2)
{
s=s+a;
System.out.println(s);
}
}
}
30. //To print the
factorial of first ten numbers
class l30
{
public static void main(String args[])
{
int a,j;
for(a=1;a<=10;a++)
{
int f=1;
for(j=1;j<=a;j++)
{
f=f*j;
}
System.out.println("Factorial of:" +a + " is
" +f);
}
}
}
31. //To print the
sum of following series : S=a2
+a2/2+ a2/3….+a2//10.. Where a=2
class l31
{
public static void main(String args[])
{
int a=2;
int i;
double s=0.0d;
double n=0.0d;
System.out.println("The series:");
for(i=1;i<=10;i++)
{
n=(double)(a*a)/i;
s=s+n;
}
System.out.println("sum of the series:" +s);
}
}
32. //To print the
sum of following series : S=a2
+a2/2!+ a2/3!….+a2//10!..
// Where a=2!
Means factorial of 2,3 and so on.
class l32
{
public static void main(String args[])
{
int a=2;
int i,j;
double s=0.0d;
double n=0.0d;
System.out.println("The series:");
for(i=1;i<=10;i++)
{
int f=1;
for(j=1;j<=a;j++)
{
f=f*j;
}
n=(double)(a*a)/f;
s=s+n;
}
System.out.println("sum of the series:" +s);
}
}
33. //To print the
sum of following series :1*2+2*3.......19*20
import java.io.*;
class l33
{
public static void main(String args[])
{
System.out.println("Enter number");
int i;
int s=0;
System.out.println("The series:");
for(i=1;i<=19;i++)
{
s=s+i*(i+1);
}
System.out.println("sum of the series:" +s);
}
}
34. //To print the
sum of following series :1+(1+2)+(1+2+3)… + (1+2+3+…n)
import java.io.*;
class l34
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter number");
int n=Integer.parseInt(input.readLine());
int i;
int s=1;
System.out.println("The series:");
for(i=1;i<=n;i++)
{
s=s+i;
s++;
}
System.out.println("sum of the series:" +s);
}
}
35. //To print the
sum of following series :S=1-2+3-4+5……..20
import java.io.*;
class l35
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
int i;
int s=0;
for(i=1;i<=20;i++)
{
if(i%2= =0)
s=s-i;
else
s=s+i;
}
System.out.println("sum of the series:" +s);
}
}
//To print the sum of
following series :S=: S=1-a/2!+ a3/4! - a5/6!….a19/20!
// a3 means 3 is raising power of a and so on
import java.io.*;
class l36
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
double t;
int i,j;
double s=1.0d;
int b=2;
System.out.println("Enter number");
int a=Integer.parseInt(input.readLine());
for(i=0;i<10;i++)
{
int f=1;
for(j=1;j<=b;j++)
{f=f*j;
}
System.out.println(f);
t=Math.pow(a,2*i+1)*Math.pow(-1,i+1);
System.out.println(t);
s=(double)s+(t/f);
b+=2;
}
System.out.println("sum of the series:" +s);
}
}
37. // To find the gross salary of 100 employees of a co.
accordingly taking basic salary
//as input HRA =20%
of the Basic, DA=25% of the Basic, CTA=10% of Basic,
//GS=Basic+HRA+DA+CTA
//The program also display the number of employees
//getting gross salary as Rs. 25000 or more.
import java.io.*;
class l37
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
int c=0;
int i;
double hra=0.0d;
double da=0.0d;
double cta=0.0d;
double gs=0.0d;
for(i=0;i<100;i++)
{
System.out.println("Enter the salary");
int bs=Integer.parseInt(input.readLine());
hra=20.0/100.0*bs;
da=25.0/100.0*bs;
cta=10.0/100.0*bs;
gs=bs+hra+da+cta;
System.out.println("The gross salary of the employee is
: " +gs);
if(gs>=25000)
{
c++;
}
}
System.out.println();
System.out.println("The total number of employees
getting 250000 or more are : "+c);
}
}
38. //To print the
fibonacci series and its sum : 0,1,1,2,3,5,8,13....n
import java.io.*;
class l38
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
double t;
int i,j;
int a=0;
int b=1;
int c=0;
System.out.println("Enter limit");
int n=Integer.parseInt(input.readLine());
System.out.println(a);
System.out.println(b);
for(i=3;i<=n;i++)
{
c=a+b;
System.out.println(c);
a=b;
b=c;
}
}
}
39. //To print the following series : 0,7,26,63,124…n
import java.io.*;
class l39
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
int i;
int j=0;
System.out.println("Enter limit");
int n=Integer.parseInt(input.readLine());
for(i=1;i<=n;i++)
{
j=(i*i*i-1);
System.out.println(j);
}
}
}
40. //To print the following series : 3,8,15,24……n
import java.io.*;
class l40
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
int i;
int j=0;
System.out.println("Enter limit");
int n=Integer.parseInt(input.readLine());
System.out.println("Series");
for(i=2;i<=n;i++)
{
j=(i*i-1);
System.out.println(j);
}
}
}
41. //To print the sum of the following series : x!/10 +
(x+2)!/15+(x+4)!/20 ….(x+n)!/m
import java.io.*;
class l41
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
double s=0.0d;
double p=1.0d;
int c=10;
int i,j;
System.out.println("Enter limit");
int n=Integer.parseInt(input.readLine());
System.out.println("Enter the value of x");
int x=Integer.parseInt(input.readLine());
for(i=0;i<=n;i+=2)
{
for(j=1;j<=x;j++)
{
p=p*j;
}
s=s+p/c;
c+=5;
}
System.out.println("Sum of the series" +s);
}
}
42. //To count the
no.s of times a user gives the keystroke as 1.
//Assume that he repeats pressing keys 10 times.
import java.io.*;
class l42
{
public static void main(String args[]) throws IOException
{
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
int i=1;
int c=0;
System.out.println("Please press any key.....");
while(i<=10)
{
char ch=(char)input.read();
if(ch=='1')
{
c++;
}
i++;
}
System.out.println("No. of times You have pressed
1=" +c);
}
}
43. //To print the
twin prime numbers upto your entered limit.
import java.io.*;
class l43
{ public static void
main(String args[]) throws IOException
{ BufferedReader
input = new BufferedReader(new InputStreamReader(System.in));
int n;
int k;
n = Integer.parseInt(input.readLine());
int f=1;
int f1=1;
int p=0;
int q=0;
int j;
int t;
int i;
for(i=3;i<=n;i++)
{ f=1;
for(j=2;j<=i-1;j++)
{
if(i%j= =0)
{
f=0;
break;
} }
if(f= =1)
{
p=i;
}
f1=1;
t=i+2;
for(k=2;k<t-1;k++)
{
if(t%k==0)
{
f1=0;
break;
}
}
if(f1==1)
{
q=t;
}
t=0;
if((f= =1) &&(q-p= =2 || p-q= =2))
{
System.out.println(p+ "
" +q);
}
}
}
}
44. //WAP in java to find the hcf of two number
import java.io.*;
class l44
{ public static void
main(String args[])throws IOException
{
BufferedReader input=new BufferedReader(new
InputStreamReader(System.in));
int num1,num2,r=1,h,q;
System.out.println("Enter two numbers");
int n1=Integer.parseInt(input.readLine());
int n2= Integer.parseInt(input.readLine());
if(n1>n2)
{
num1=n1;
num2=n2;
}
else
{ num1=n2;
num2=n1;
}
while(r!=0)
{
r=n1%n2;
n1=n2;
n2=r;
}
System.out.println("The hcf of two number is" +n1);
} }
45. /*1
11
111
1111
11111
111111
1111111
11111111
111111111
1111111111
*/
class l45
{
public static
void main(String args[])
{ int x,p=0,
c=0;
int i,j;
for(i=0;i<10;i++)
{ x=1;
for(j=1;j<=i;j++)
x=x*10;
p=p+x;
System.out.println(p
+ " ");
}}}
46. //To to print the sum of following series :
S=x/1!-x2/2! +x3/3!…..xn/n!
import
java.io.*;
class l46
{
public static
void main(String args[]) throws IOException
{ BufferedReader
input = new BufferedReader(new InputStreamReader(System.in));
double y;
int i,j;
double s=0.0d;
System.out.println("Enter
the limit");
int
n=Integer.parseInt(input.readLine());
System.out.println("Enter
the value of x");
int
x=Integer.parseInt(input.readLine());
for(i=1;i<=n;i++)
{ int f=1;
for(j=1;j<=i;j++)
{ f=f*j;
}
y=Math.pow(x,i)/f;
if(i%2==0)
s=s-y;
else
s=s+y;
}
System.out.println("sum
of the series:" +s);
}}
47. // WAP in
Java enter a number and print in words.
import
java.io.*;
class l47
{ public static void main(String args[])
throws IOException
{ BufferedReader
input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
the number");
int
n=Integer.parseInt(input.readLine());
int temp,digit;
String
result=" ";
temp=n;
do
{ digit=temp%10;
String
ans=" ";
switch(digit)
{ case 0:
ans="zero";
break;
case 1:
ans="one";
break;
case 2:
ans="two";
break;
case 3:
ans="three";
break;
case 4:
ans="four";
break;
case 5:
ans="five";
break;
case 6:
ans="six";
break;
case 7:
ans="seven";
break;
case 8:
ans="eight";
break;
case 9:
ans="nine";
break;
}
result=ans +
" " +result;
temp=temp/10;
}
while(temp!=0);
System.out.println("The
number " + n + " in words is :\n " +result);
}
}
48.// WAP in
Java to check whether the given number is palindrome or not
//Palindrome is
that which is same as forward or backward
import
java.io.*;
class l48
{ public static
void main(String args[]) throws IOException
{ BufferedReader
input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter
the number");
int
n=Integer.parseInt(input.readLine());
int num=n;
int rev=0,d;
do
{ d=n%10;
rev=(rev*10)+d;
n=n/10;
}while(n!=0);
System.out.println("The
reverse of the number is :" +rev);
if(num= =rev)
System.out.println("The
number is palindrome");
else
System.out.println("The
number is not palindrome");
} }
49. /*to
calculate the compound interest based on the
conditions
import
java.io.*;
class l49
{ public static void main(String args[])
throws IOException
{ BufferedReader
input = new BufferedReader(new InputStreamReader(System.in));
double r;
double i=0.0d;
int b;
for(b=0;b<10;b++)
{ System.out.println("Enter the principal
and time");
double
p=Double.parseDouble(input.readLine());
double
t=Double.parseDouble(input.readLine());
if(t<=1.0)
{ r=3.0;
double
a=p*(Math.pow((1+r/100.0),t));
i=a-p;
System.out.println("Interest:"+i);
break;
}
else if(t>1
&& t<=2)
{ r=5.0;
double
a=p*(Math.pow((1+r/100.0),t));
i=a-p;
System.out.println("Interest:"+i);
}
else
{ r=7.0;
double
a=p*(Math.pow((1+r/100.0),t));
i=a-p;
System.out.println("Interest:"+i);
}
}}}
50. //to accept
a number and check whether it is perfect or not
import
java.io.*;
class l50
{
public static
void main(String args[]) throws IOException
{
BufferedReader
input = new BufferedReader(new InputStreamReader(System.in));
int i;
int n=Integer.parseInt(input.readLine());
int s=0;
int num=n;
for(i=1;i<n;i++)
{
if(n%i==0)
{
s=s+n;
break;
}
}
if(s = =num)
{
System.out.println("Number is
perfect");
}
else
{
System.out.println("Number is not perfect");
}
}}
Comments
Post a Comment