Java Programming Examples

Let us look at some basic program developed in java. You are advised to start practice with these basic programs and try to understand the working of program step by step with output.


Program to print number 1 to 10.

>
					
class PrintNum
{ 
  public static void main(String ar[])
   { 
   for(int i=1;i<=10;i++) 
   { 
   System.out.println(i); 
   } 
   } 
}
Output :
1
2
3
4
5
6
7
8
9

Program to calculate average of three number.

				
 class PrintNum{
    public static void main(String ar[])
    {
    int num1=10;
    int num2=16;
    int num3=22;
    int avg=(num1+num2+num3)/3;
    System.out.println("average is : "+avg);
    }

}
output:
average is : 16


Program to check a number odd or even.

>
						class PrintNum{

    public static void main(String ar[])

    {

    int x=5;

    if ( x % 2 == 0 )

    System.out.println(x +" is even number.");

    else

    System.out.println(x +" is odd number.");

    }

}

output:

5 is odd number.


  • Program of Fibonacci series.
  • 
    class PrintNum
    
    {
    
        public static void main(String ar[])
    
        {
    
        int f1=1, f2=1, temp;
    
        System.out.println(f1);
    
        System.out.println(f2);
    
        for(int i=1;i<=5;i++)
    
        {
    
        temp=f1+f2;
    
        f1=f2;
    
        f2=temp;
    
        System.out.println(temp);
    
        }
    
        }
    
    }
    
    
    output:
    
    1
    
    1
    
    2
    
    3
    
    5
    
    8
    
    13
    
    

  • Program to calculate factorial of 5.
  • 
    class PrintNum
    
    {
    
        public static void main(String ar[])
    
        {
    
        int limit=5;
    
        fact=1;
    
        for(int i=1;i<=limit;i++)
    
        {
    
        fact=fact*i;
    
        }
    
        System.out.println("Factorial of "+limit+" is : "+fact);
    
        }
    
    }
    
    
    output:
    
    
    
    

  • Program to swap two number in single statement.
  • 
    class PrintNum
    
    {
    
        public static void main(String ar[])
    
        {
    
        int i=4;
    
        int j=5;
    
        i=(i+j)-(j=i); //number swapped
    
        System.out.println("i = "+i);
    
        System.out.println("j = "+j);
    
        }
    
    }
    
    
    output:
    
    i = 5
    
    j = 4
    
    

  • Program to check a number is palindrome or not.
  • 
    class PrintNum
    
    {
    
        public static void main(String ar[])
    
        {
    
        int num=12321, a, r;
    
        a=num;
    
        while(n>0)
    
       {
    
         r=num%10;
    
         rev=rev*10+r;
    
         num=num/10;
    
       }
    
       if(rev==a)
    
       System.out.println(a+" is palindrome");
    
       else
    
       System.out.println(a+"is not palindrome");
    
    }
    
    }
    
    
    output:
    
    12321 is palindrome
    
    

  • Program to check a number is armstrong or not.
  • 
    class PrintNum
    
    {
    
        public static void main(String ar[])
    
        {
    
        int n=153,c=0,r,dup;
    
        dup=n;
    
        while(n>0)
    
        {
    
        a=n%10;
    
        n=n/10;
    
        c=c+(a*a*a);
    
        }
    
        if(dup==c)
    
        System.out.println(dup+" is an armstrong number"); 
    
        else
    
        System.out.println(dup+" is not an armstrong number"); 
    
        }
    
    }
    
    
    output:
    
    153 is an armstrong number.