Program to Print Fibonacci Series in C
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. In this article, we will discuss different ways to write the C program to print the Fibonacci series.
Using a Loop to Print Fibonacci Series in C
One of the simplest and most common ways to generate the Fibonacci sequence is by using a loop. The program iterates through a given number of terms and prints each term of the sequence.
Here’s the C code for generating the Fibonacci sequence using a loop:
#include <stdio.h>
void printFibonacci(int n) {
int firstTerm = 0, secondTerm = 1, nextTerm, i;
printf("Fibonacci Sequence: ");
for (i = 0; i < n; i++) {
if (i <= 1)
nextTerm = i;
else {
nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
printf("%d ", nextTerm);
}}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printFibonacci(n);
return 0;
}
The `printFibonacci` function takes an integer `n` as a parameter, representing the number of terms to be printed. It initializes the first and second terms of the sequence as 0 and 1, respectively. Then, using a `for` loop, it calculates the next term by summing the previous two terms. The loop iterates `n` times, printing each term.
In the `main` function, the user is prompted to enter the number of terms they want to display. The input is then passed to the `printFibonacci` function.“
Alternative Approaches to Print Fibonacci Series in C
While the above method using a loop is straightforward, there are other ways to generate the Fibonacci sequence. Here, we will discuss two alternative approaches: using recursion and using an array.
C Program to print Fibonacci Series Using Recursion:
Recursion is a technique where a function calls itself. In the case of the Fibonacci sequence, the function recursively calls itself to calculate the terms.
Here’s the C code for generating the Fibonacci sequence using recursion:
#include <stdio.h>
int fibonacci(int n)
{
if (n <= 1){
return n;}
return fibonacci(n - 1) + fibonacci(n - 2);
}
void printFibonacci(int n) {
printf("Fibonacci Sequence: ");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printFibonacci(n);
return 0;
}
In this code, the `fibonacci` function is defined to calculate the nth term of the Fibonacci sequence. It uses recursion to call itself, summing the previous two terms until it reaches the base case (n <= 1), where it returns n.
The `printFibonacci` function is similar to the one in the previous method. It iterates `n` times and calls the `fibonacci` function to calculate each term.
C Program for Fibonacci Series Using an Array:
Another approach to generating the Fibonacci sequence is by using an array. Instead of recalculating each term, we can store the sequence in an array and access the values as needed.
Here’s the C code for generating the Fibonacci sequence using an array:
#include <stdio.h>
void printFibonacci(int n) {
int sequence[n];
sequence[0] = 0;
sequence[1] = 1;
printf("Fibonacci Sequence: %d %d ", sequence[0], sequence[1]);
for (int i = 2; i < n; i++) {
sequence[i] = sequence[i - 1] + sequence[i − 2];
printf("%d ", sequence[i]);
}
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printFibonacci(n);
return 0;
}
In this code, we create an array `sequence` of size `n` to store the Fibonacci sequence. We initialize the first two terms as 0 and 1. Then, using a `for` loop, we calculate each subsequent term by summing the previous two terms and store it in the array. Finally, we print each term of the sequence.
Output:
Fibonacci Sequence: 0 1 1 2 3 5 8 13 21 34
Although the methods differ in their working, the end result is the same
Conclusion
In this article, we covered various methods to generate and print the Fibonacci sequence in C. We discussed a loop-based approach, recursion, and using an array. Each method has its own advantages and can be used depending on the specific requirements of a program. By understanding these approaches, you can effectively generate and manipulate the Fibonacci sequence in your C programs.
Your opinion matters
Please write your valuable feedback about DataFlair on Google

