#include<stdio.h>
int main()
{
int count, first_term = 0, second_term = 1, next_term, i;
//Ask user to input number of terms
printf("Enter the number of terms:\n");
scanf("%d",&count);
printf("First %d terms of Fibonacci series:\n",count);
for ( i = 0 ; i < count ; i++ )
{
if ( i <= 1 )
next_term = i;
else
{
next_term = first_term + second_term;
first_term = second_term;
second_term = next_term;
}
printf("%d\n",next_term);
}
return 0;
}
import java.util.*;
public class Learner {
public static void main(String[] args) {
//Ask user to input number of terms
System.out.println("Enter the number of terms:");
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
int first_term = 0;
int second_term = 1;
int next_term;
System.out.println(count + " first terms of Fibonacci series:");
for (int i = 0; i < count; i++) {
if (i <= 1){
next_term = i;
}else {
next_term = first_term + second_term;
first_term = second_term;
second_term = next_term;
}
System.out.println(next_term);
}
}
}
OUTPUT
Enter the number of terms: 8
First 8 terms of Fibonacci series:
0
1
1
2
3
5
8
13