Description:
This program prints the Fibonacci sequence, where each number is the sum of the two preceding ones. It starts from 0 and 1 and continues for “n” terms. It introduces loops and helps in understanding series generation using iteration.
Usage:
The user inputs the number of terms they want in the Fibonacci sequence, and the program prints the series.
Code:
C
#include <stdio.h>
int main() {
int n, i, t1 = 0, t2 = 1, nextTerm;
// Asking user for the number of terms
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Explanation:
- The Fibonacci series starts with 0 and 1, and each subsequent term is the sum of the two preceding terms.
- The for loop runs to print n terms of the Fibonacci sequence.
Test here: