Thursday, 19 September 2013

Java Program to print Fibonacci Series

import java.util.Scanner;

class Fibonacci 
{
     public static void main(String args[])
     {
          int a=0,b=1,c,num,i=2 ;
          
          System.out.println("Enter the limit where you want to print Fibonacci series");
           
          Scanner obj = new Scanner(System.in);
          
          num = obj.nextInt();
          System.out.println("Fibonacci Series:");
          System.out.print(a+"  ");       
     
          while( i <= num )
          {
               c = b;
               b = a;
               a = b + c;
               System.out.print(a+"  ");
               i++;
          }         
              
     }
}     


Output:






Wednesday, 18 September 2013

Java Program to find reverse of entered number

import java.util.Scanner;

class Reverse
{
     public static void main(String args[])
     {
          int num, copynum, reverse = 0;
           
          System.out.println("Enter the number to make a reverse");
           
          Scanner obj = new Scanner(System.in);
          
          num = obj.nextInt(); 
          copynum=num;
           
          while(num != 0)
          { 
               reverse = reverse * 10;
               reverse = reverse + num%10;
               num = num/10;
          }
           
          System.out.println("Reverse of " + copynum + " is " + reverse);
     }    
}     


Output:

Java Program will print 'Hello World'

class Hello
{
     public static void main(String args[])
     {
          System.out.println("Hello World");
     }

}


Output:


Java Program to find Square Root of a number through Commandline

import java.lang.Math;

class Squareroot
{
     public static void main (String args[])
     {
          float num,sqnum;
          num = Float.valueOf(args[0]).floatValue();
          sqnum = (float)Math.sqrt(num);
          System.out.println("Square Root of "+num+" is "+sqnum);
     }
}    


Output:

Wednesday, 11 September 2013

Java Program to find factorial of a given number through Commandline

class Factorial
{
     public static void main(String ar[])
     {
          int n=Integer.parseInt(ar[0]);
          int f=0;
           
          fact(n);
     }
      
     static void fact(int n1)
     {
          int f=1;
          for(int i=1;i<=n1;i++)
          {
               f=f*i;
          }
          System.out.println("Factrorial is = "+f);
     }
} 


Output:

Tuesday, 10 September 2013

Java Program to find out entered number is armstrong or not through Commandline

class Arm
{
     public static void main(String ar[])
     {
          int num=Integer.parseInt(ar[0]);
          int n=num;
          int check=0,remainder;
          
          while(num>0)
          {
               remainder=num%10;
               check=check+(int) Math.pow(remainder,3);
               num=num/10;
          }
          
          if(check==n)
          {
               System.out.println("This is armstrong number");
          }
          else
          {
               System.out.println("This is not armstrong number");
          }
     }
}


Output:

Monday, 9 September 2013

Java Program to make table of user entered a number through Commandline

class Table
{
     public static void main(String ar[])
          {
               int num=Integer.parseInt(ar[0]);
               for(int i=1;i<=10;i++)
               {
                    System.out.println(+num +"*"+ i +"=" + (num*i));
               }
                 
          }
}               


Output: