Saturday, 21 May 2016

How to print fibonacci series in java


import java.util.Scanner;
class Main{
public static void main(String args[]){
int i,n1=0,n2=1,n3;
Scanner input=new Scanner(System.in);
System.out.println("Enter the number");
int n=input.nextInt();
System.out.println("Fibonacci series is "+n1);
System.out.println("Fibonacci series is "+n2);
for(i=0;i<=n;i++)
{
n3=n1+n2;
System.out.println("Fibonacci series is "+n3);
n1=n2;
n2=n3;
}
}
}

OUTPUT:-
C:\Users\Satyendra\Desktop>javac Main.java
C:\Users\Satyendra\Desktop>java Main
Enter the number

10

Fibonacci series is 0

Fibonacci series is 1

Fibonacci series is 1

Fibonacci series is 2

Fibonacci series is 3

Fibonacci series is 5

Fibonacci series is 8

Fibonacci series is 13

Fibonacci series is 21

Fibonacci series is 34

Fibonacci series is 55

Fibonacci series is 89

Fibonacci series is 144

No comments:

Post a Comment