Fibonacci sequence using java programming

Fibonacci sequence using java programming

Fibonacci sequence

The Fibonacci numbers are a sequence of numbers where each number after the first two is a sum of the prior two. As an illustration, here is a short sequence given starting values of (0, 1) is (0, 1, 1, 2, 3, 5, 8, 13).

Write a program to generate and print the first n terms of the Fibonacci sequence where n >=1.

Input

Each test case will contains a single integer n where n >=1.

Output

For each input case, generate and print the first n terms of the Fibonacci sequence.


Sample Input

4

Sample Output

0
1
1
2


 import java.util.Scanner;

public class practice {
public static void main(String[] args) {
int sum1 = 0 , sum2 = 1 , sum3 ;
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = 0;
if(i==0){
System.out.println(sum1);
if(n>1){
i++;
}
}
if(i==1){
System.out.println(sum2);
}
for(i = 3 ; n>=i ; i++)
{
sum3 = sum1 + sum2;
sum1 = sum2;
sum2 = sum3;
System.out.println(sum3);
}

}
}

Post a Comment

Previous Post Next Post