Fibonacci series program in Java

  • 0
Fibonacci series is one the most series es in the world. In a Fibonacci series, each term is the sum of last two terms. For the first two terms, 1st is taken as 0 and second is taken as 1. Fibonacci series has attracted a lot of attention and research. So. let us devote a JAVA program to print the Fibonacci series.
package MathUtility;

class FibonacciSeries
{
 void series(int i)
 {
  if(i == 0)
   System.out.println(i);
  else if(i == 1)
   System.out.println(i-1 + " " + i);
  else
  {
   int first, second, third, len;
   
   first =0;
   second =1;
   System.out.print("0" + " " + "1");
   
   for(len=1;len<=(i-2);len++)
   {
    third = first + second;
    System.out.print(" " +third);
    first = second;
    second = third;
   }
  }
 }
 
 public static void main(String args[])
 {
  if(args.length==0)
  {
   System.out.println("Input Not Provided");
  }
  else
  {
   int ser = Integer.parseInt(args[0]);
   FibonacciSeries fs = new FibonacciSeries();
   fs.series(ser);
  }
 }
}
To compule the program
javac -d . FabonacciSeries.java
And to run the program
java MathUtility.FabonacciSeries 10

No comments:

Post a Comment