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
for
loop to generate the Fibonacci series forn
terms. - In each iteration, we print
first
, computenext
as the sum offirst
andsecond
, then updatefirst
andsecond
.
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
count
variable to track iterations. - We initialize
first
andsecond
to 0 and 1. - We take user input for the number of terms (
n
). - The
while
loop continues untilcount
reachesn
. - Each iteration prints
first
, computesnext
, and updatesfirst
andsecond
.
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
, andnext
to generate the sequence. - We take user input for the number of terms (
n
). - The
do-while
loop executes at least once, ensuring the first term is always printed. - The loop continues generating and printing Fibonacci numbers until
count
reachesn
.
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:
for
Loop: Best when the number of iterations is known.while
Loop: Useful when a condition-based iteration is required.do-while
Loop: Ensures at least one iteration.