Java program to find Fibonacci series of a given number

Java program to find Fibonacci series of a given number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*Write a program to find Fibonacci series of a given no.
  Example :
  Input - 8
  Output - 1 1 2 3 5 8 13 21
 */
class Fibonacci{
	public static void main(String args[]){
		int num = Integer.parseInt(args[0]); //taking no. as command line
		argument.
			System.out.println("*****Fibonacci Series*****");
		int f1, f2=0, f3=1;
		for(int i=1;i<=num;i++){
			System.out.print(" "+f3+" ");
			f1 = f2;
			f2 = f3;
			f3 = f1 + f2;
		}
	}
}

75 Responses to “Java program to find Fibonacci series of a given number”

    • sudheer

      f1=0
      f2=1
      f3=0+1 =1

      f1=1
      f2=1
      f3=1+1 =2

      f1=1
      f2=2
      f3=1+2=3

      f1=2
      f2=3
      f3=2+3=5 Etc..

      Reply
  1. anitha

    ineed a fibonacci program with just 4 lines including class name and main, there should be only 4 lines.

    Reply
  2. public class FibonacciMethod{

    public static void main(String [] felight){

    System.out.println(” “+FibonacciMethod(25));
    }

    public static String FibonacciMethod(int n){
    int i=0;int fib=0;int j=1;int temp=0;String fib1=””;String fib2=””;
    while(i<n-1){
    temp=fib+j;
    fib=j;
    j=temp;

    if(i==0){
    fib=0;

    }

    fib1=fib+"";

    //System.out.print(" "+fib1);
    i++;

    }
    return fib1;

    }
    }

    Reply

Leave a Reply