JAVA PROGRAMS
1) Write a simple java program to print Welcome
RGUKT?
class sample
{
public static void main(String args[])
{
[Link]("Welcome RGUKT");
}
}
OUTPUT:
Welcome RGUKT
2)Write a java program print first ten natural numbers
and its sum?
class loop
{
public static void main(String args[])
{
int i=1,num=10,sum=0;
for(i=1;i<=num;i++)
{
sum=sum+i;
[Link](" "+i);
}
[Link]("Sum of first 10 natural
numbers is :"+sum);
}
}
OUTPUT:
1
2
3
4
5
6
7
8
9
10
Sum of first 10 natural numbers is :55
3)Write a java program to perform addition,
substraction, multiplication and division of two
numbers?
class math
{
public static void main(String args[])
{
int n1=10,n2=20,sum,sub,mul,div;
[Link]("The number n1 is :"+n1);
[Link]("The number n2 is :"+n2);
sum=n1+n2;
sub=n1-n2;
mul=n1*n2;
div=n2/n1;
[Link]("Sum of n1 and n2
is:"+sum);
[Link]("Substraction of n1 and n2
is :"+sub);
[Link]("Multiplication of n1 and n2
is:"+mul);
[Link]("Division of n1 and n2
is:"+div);
}
}
OUTPUT:
The number n1 is :10
The number n2 is :20
Sum of n1 and n2 is:30
Substraction of n1 and n2 is :-10
Multiplication of n1 and n2 is:200
Division of n1 and n2 is:2
4)Write a java program to print even numbers ,even
numbers sum and odd numbers sum?
class sum
{
public static void main(String args[])
{
int i,sum=0,odd=0;
[Link]("Even numbers are:");
for(i=1;i<=10;i++)
{
if(i%2==0)
{
[Link](" "+i);
sum=sum+i;
}
else
{
odd=odd+i;
}
}
[Link]("");
[Link]("The sum of even numbers
is:"+sum);
[Link]("The sum of odd numbers
is:"+odd);
}
}
OUTPUT:
Even numbers are: 2 4 6 8 10
The sum of even numbers is:30
The sum of odd numbers is:25
5)write a java program to find the given number
positive ,negative or zero?
class number
{
public static void main(String args[])
{
int n=10;
if(n!=0)
{
if(n>0)
{
[Link]("n is positive number");
}
else
{
[Link]("n is negative
number");
}
}
else
{
[Link]("n is zero");
}
}
}
OUTPUT:
n is positive number
6)Write a java program to swap two numbers?
class swap
{
public static void main(String args[])
{
int a=10,b=20;
[Link]("Numbers a and b before
swapping are "+a+" "+b);
a=a+b;
b=a-b;
a=a-b;
[Link]("Numbers a and b after swapping
are "+a+" "+b);
}
}
OUTPUT:
Numbers a and b before swapping are 10 20
Numbers a and b after swapping are 20 10
7)Write a java program to reverse a number?
class reverse
{
public static void main(String args[])
{
int n=234,r,temp,sum=0;
[Link]("Before reversing the number n is
"+n);
temp=n;
while(n!=0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
[Link]("After reversing the number n is
"+sum);
}
}
OUTPUT:
Before reversing the number n is 234
After reversing the number n is 432
8)Write a java program to check whether the number
is prime or not?
class prime
{
public static void main(String args[])
{
int i=1,n=7,count=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count=count+1;
}
}
if(count==2)
{
[Link]("prime Number");
}
else
{
[Link]("Not a Prime Number");
}
}
}
OUTPUT:
Prime Number
9)Write a java program to print first n prime numbers?
class example
{
public static void main(String args[])
{
int i,j,k,num=10,n=2,count=0;
[Link]("First "+num+" prime numbers
are:");
for(i=1;i<=num;)
{
count=0;
for(j=1;j<=n;j++)
{
if(n%j==0)
{
count=count+1;
}
}
if(count==2)
{
[Link](" "+n);
i++;
}
n++;
}
}
}
OUTPUT:
First 10 prime numbers are: 2 3 5 7 11 13 17 19 23 29
10)Write a java program to print prime numbers upto
n numbers?
class nprime
{
public static void main(String args[])
{
int i,j,num=20,count=0;
[Link]("Prime numbers upto "+num+"
are:");
for(i=1;i<=num;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
count=count+1;
}
}
if(count==2)
{
[Link](" "+i);
}
}
}
}
OUTPUT:
Prime numbers upto 20 are: 2 3 5 7 11 13 17 19
11)Write a java program to check whether the given
number is palindrome or not?
class palindrome
{
public static void main(String args[])
{
int num=121,temp,r,sum=0;
temp=num;
while(num!=0)
{
r=num%10;
sum=(sum*10)+r;
num=num/10;
}
if(temp==sum)
{
[Link]("Palindrome");
}
else
{
[Link]("Not a palindrome");
}
}
}
OUTPUT:
Palindrome
12)Write a java program to check whether given
number is armstrong number or not?
class armstrong
{
public static void main(String args[])
{
int r,num=153,temp,sum=0;
temp=num;
while(num!=0)
{
r=num%10;
sum=sum+(r*r*r);
num=num/10;
}
if(temp==sum)
{
[Link]("Armstrong number");
}
else
{
[Link]("Not a armstrong number");
}
}
}
OUTPUT:
Armstrong number
13)Write a java program to check whether the number
is perfect number or not?
class perfect
{
public static void main(String args[])
{
int i,n=6,sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(n==sum)
{
[Link]("Perfect number");
}
else
{
[Link]("Not a Perfect number");
}
}
}
OUTPUT:
Perfect number
14)Write a java program to print fibonacci series upto
n?
class fibonacci
{
public static void main(String args[])
{
int a,b,c,n=20;
a=0;
b=1;
[Link]("Fibonacci series:");
[Link](""+a+" "+b);
c=a+b;
while(c<=n)
{
[Link](" "+c);
a=b;
b=c;
c=a+b;
}
}
}
OUTPUT:
Fibonacci series:0 1 1 2 3 5 8 13
15)Write a java program to print the factors of the
number?
class factor
{
public static void main(String args[])
{
int i,n=10;
[Link]("Factors of "+n+" is:");
for(i=1;i<n;i++)
{
if(n%i==0)
{
[Link](" "+i);
}
}
}
}
OUTPUT:
Factors of 10 is: 1 2 5
16)Write a java program to find factorial of the
number?
class factorial
{
public static void main(String args[])
{
int i,fact=1,n=5;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
[Link]("The factorial of 5 is:"+fact);
}
}
OUTPUT:
The factorial of 5 is:120
17)Write a java program to print the following pattern?
1
12
123
1234
12345
class pattern1
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link](" "+j);
}
[Link]("");
}
}
}
18)Write a java program to print the following series?
*
**
***
****
*****
class pattern2
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link]("*");
}
[Link]("");
}
}
}
19)Write a java program to print following pattern?
5
45
345
2345
12345
class pattern3
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if((i+j)<6)
{
[Link](" ");
}
else
{
[Link](" "+j);
}
}
[Link]("");
}
}
}
20)Write a java program to print the following pattern?
*
**
***
****
*****
class pattern4
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if((i+j)<6)
{
[Link](" ");
}
else
{
[Link](" *");
}
}
[Link]("");
}
}
}
21)Write a java program to print the foolwing pattern?
1
21
321
4321
54321
class pattern5
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if((i+j)<6)
{
[Link](" ");
}
else
{
[Link](" "+(5-j+1));
}
}
[Link]("");
}
}
}
22)Write a java program to print the following prime
number pattren?
2
3 5
7 11 13
17 19 23 29
class pattern6
{
public static void main(String args[])
{
int i,j,count=0,n=2,k;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;)
{
count=0;
for(k=2;k<=n/2;k++)
{
if(n%k==0)
{
count=count+1;
}
}
if(count==0)
{
[Link](" "+n);
j++;
}
n++;
}
[Link]("");
}
}
}
23)Write a program to print the foolowing fibonacci
pattern?
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377
class fibonaccipat
{
public static void main(String args[])
{
int a=0,b=1,c,i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link](" "+a);
c=a+b;
a=b;
b=c;
}
[Link]("");
}
}
}
24)Write a java program to print 5 table?
class table
{
public static void main(String args[])
{
int i,n=5;
for(i=1;i<=10;i++)
{
[Link](""+n+" x "+i+" ="+(n*i));
}
}
}
OUTPUT:
5 x 1 =5
5 x 2 =10
5 x 3 =15
5 x 4 =20
5 x 5 =25
5 x 6 =30
5 x 7 =35
5 x 8 =40
5 x 9 =45
5 x10=50
25)Write a java program to print the following pattern?
$
*$
**$
***$
****$
class p7
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(i==j)
{
[Link](" $");
}
else
{
[Link](" *");
}
}
[Link]("");
}
}
}