Friday, 21 October 2016

Write a java program to print Fibonic series

Write a java program to print Fibonic series
   

Answer :

public class JavaFibonacciSeriesExample 
{
  public static void main(String[] args) 
  {

  //number of elements to generate in a series
     int limit = 20;
     long[] series = new long[limit];
     //create first 2 series elements
     series[0] = 0;
     series[1] = 1;
     //create the Fibonacci series and store it in an array
     for(int i=2; i < limit; i++)
     {
        series[i] = series[i-1] + series[i-2];
     }
     //print the Fibonacci series numbers
     System.out.println("Fibonacci Series upto " + limit);
     for(int i=0; i< limit; i++)
     {
        System.out.print(series[i] + " ");
     }
   }
}

   output: Fibonacci Series upto 20
   0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181


Write a java program to determine wheather the given year is LEAP year or not

Write a java program to determine wheather the given year is LEAP year or not
   

Answer :

public class DetermineLeapYearExample 
{
  public static void main(String[] args) 
  {
//year we want to check
    int year = 2004;
//if year is divisible by 4, it is a leap year
    if(year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))
    System.out.println("Year " + year + " is a leap year");
    else
    System.out.println("Year " + year + " is not a leap year");
  }
}

   output: Year 2004 is a leap year


Wednesday, 5 October 2016

Write a java program to find the Factorial of a number

Write a java program to find the Factorial of a number
   

Answer :


/*
List Even Numbers Java Example
This List Even Numbers Java Example shows how to find and list even
numbers between 1 and any given number.
*/
 public class FactorialExa1
 {

  public static void main(String[] args) 
  {
    int number = 5;
    /*
     Factorial of any number is! n.
     For example, factorial of 4 is 4*3*2*1.
    */
    
    int factorial = number;
    for(int i =(number - 1); i > 1; i--)  
    {
       factorial = factorial * i;
    }
     System.out.println("Factorial of the number is " + factorial);
  }

 }

   output: Factorial of the number is 120

Using Recursion


 class FactorialExa2
 {  
  static int factorial(int n)
  {    
    if (n == 0)    
      return 1;    
    else    
      return(n * factorial(n-1));    
  }
    
  public static void main(String args[])
  {  
    int i,fact=1;  
    int number=5;   
    fact = factorial(number);   
    System.out.println("Factorial of "+number+" is: "+fact);    
  }  
}  

   output: Factorial of 5 is 120


Write a java program to display the List of even numbers

Write a java program to display the List of even numbers
   

Answer :


/*
List Even Numbers Java Example
This List Even Numbers Java Example shows how to find and list even
numbers between 1 and any given number.
*/
 public class EvenExample
 {

  public static void main(String[] args) 
  {
        
    int limit = 30;//define limit
    System.out.println("Printing Even numbers between 1 and " +limit);

    for(int i=1; i <= limit; i++)
    {
     // if the number is divisible by 2 then it is even
        if( i % 2 == 0)
        {
            System.out.print(i + " ");
        }
    }
  }

 }

   output: Printing Even numbers between 1 and 30 
           2 4 6 8 10 12 14 16 18 20 22 24 26 28 30