Program to print Fibonacci
Series in java
This program is about to print the series of Fibonacci.
The program is written as you can understand loop wise while it’s running. You’ll
easily understand the flow of code and loop of the Fibonacci series.
Find below the program written.
import java.util.Scanner;
public class MyClass {
public static
void main(String args[]) {
Scanner
scan = new Scanner(System.in);
int n =
scan.nextInt();
int a = 0,
b = 0, c = 1;
for(int i =
0; i < n; i++){
System.out.println("Loop No. "+i);
a = b;
System.out.println("a = b | a = "+b);
b = c;
System.out.println("b = c | b = "+c);
c = a +
b;
System.out.println("c = a + b | c = " + a + " + " +
b);
System.out.println(a+" ");
System.out.println("-----------------------");
}
System.out.println();
a = 0;
b = 0;
c = 1;
for(int i =
0; i < n; i++){
a = b;
b = c;
c = a +
b;
System.out.print(a+" ");
}
}
}
Output:
0 Comments:
Post a Comment