Generate Fibonacci Series using Loops in C
To generate the Fibonacci series in C, we use loops such as for, while, or do-while to compute the sequence iteratively. The Fibonacci series starts with 0 and 1, and each subsequent term is the sum of the previous two terms. This tutorial demonstrates multiple ways to generate the Fibonacci sequence using loops in C.
Examples of Generating Fibonacci Series in C
1. Generating Fibonacci Series Using a for Loop
In this example, we will use a for loop to generate the Fibonacci series up to a given number of terms.
main.c
#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++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}
Explanation:
- We declare three variables:
first(initialized to 0),second(initialized to 1), andnext(to store the sum of the previous two terms). - We take user input for the number of terms (
n). - We use a
forloop to generate the Fibonacci series fornterms. - In each iteration, we print
first, computenextas the sum offirstandsecond, then updatefirstandsecond.
Output (Example for n = 5):
Enter the number of terms: 5
Fibonacci Series: 0 1 1 2 3
2. Generating Fibonacci Series Using a while Loop
In this example, we will use a while loop to generate the Fibonacci sequence until we reach the required number of terms.
main.c
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next, count = 0;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
while (count < n) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
count++;
}
return 0;
}
Explanation:
- We declare a
countvariable to track iterations. - We initialize
firstandsecondto 0 and 1. - We take user input for the number of terms (
n). - The
whileloop continues untilcountreachesn. - Each iteration prints
first, computesnext, and updatesfirstandsecond.
Output (Example for n = 6):
Enter the number of terms: 6
Fibonacci Series: 0 1 1 2 3 5
3. Generating Fibonacci Series Using a do-while Loop
In this example, we use a do-while loop to generate the Fibonacci series. This ensures that at least one term is printed even if the user enters 1 as the number of terms.
main.c
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next, count = 0;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
do {
printf("%d ", first);
next = first + second;
first = second;
second = next;
count++;
} while (count < n);
return 0;
}
Explanation:
- We declare and initialize
first,second, andnextto generate the sequence. - We take user input for the number of terms (
n). - The
do-whileloop executes at least once, ensuring the first term is always printed. - The loop continues generating and printing Fibonacci numbers until
countreachesn.
Output (Example for n = 4):
Enter the number of terms: 4
Fibonacci Series: 0 1 1 2
Conclusion
In this tutorial, we explored how to generate Fibonacci series using loops in C:
forLoop: Best when the number of iterations is known.whileLoop: Useful when a condition-based iteration is required.do-whileLoop: Ensures at least one iteration.
