Fibonacci Series Generator
Learn how to generate the Fibonacci series in C using this attractive program.
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}
Explanation
-
Step 1:
The program starts by including the standard input/output library
stdio.h
. -
Step 2:
In the
main()
function, integer variablesn
,first
,second
, andnext
are declared. - Step 3: The program prompts the user to enter the number of terms in the Fibonacci series.
-
Step 4:
The program generates and prints the Fibonacci series using a
for
loop. -
Step 5:
The
next
term is calculated by adding thefirst
andsecond
terms. The values offirst
andsecond
are updated accordingly.
If you have any doubts, Please let me know