The Fibonacci series is a sequence where each term is the sum of the two preceding ones, starting from 1 and 1. The sequence looks like this:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...In this program, we aim to generate and print the Fibonacci series up to a specified number of terms, entered by the user. Let’s break down the logic and the full code implementation:
#include <stdio.h> // Include the Standard Input-Output header for printf and scanf functions
// Function prototype for Fibonacci function
int Fibonacci(int x);
void main() {
int term, i;
// Prompt the user to enter the number of terms
printf("Enter the number of terms of Fibonacci Series which is going to be printed: ");
scanf("%d", &term); // Read the number of terms from the user
// Loop to print the Fibonacci series up to the given number of terms
for(i = 0; i < term; i++) {
printf("%d ", Fibonacci(i)); // Print each Fibonacci number using the Fibonacci function
}
// Add a newline after printing the series
printf("\n");
}
// Fibonacci function to calculate the Fibonacci number at position x
int Fibonacci(int x) {
if(x == 0 || x == 1) // Base case: If the position is 0 or 1, return 1
return 1;
else
// Recursive case: Fibonacci number is the sum of the two previous Fibonacci numbers
return (Fibonacci(x - 1) + Fibonacci(x - 2));
}
Explanation of the Code:
Header File:
#include <stdio.h>
: This includes the Standard Input/Output library, which is necessary for usingprintf()
andscanf()
functions.
Main Function:
Variables:
term
: This variable stores the number of terms that the user wants to print in the Fibonacci series.i
: This is used as a loop counter.
User Input:
- The program prompts the user to input the number of terms for the Fibonacci sequence using
printf()
. - It then reads the input using
scanf()
and stores it in the variableterm
.
- The program prompts the user to input the number of terms for the Fibonacci sequence using
Printing the Fibonacci Series:
- The
for
loop runs fromi = 0
toi < term
, and in each iteration, it calls theFibonacci(i)
function to get the Fibonacci number at positioni
. - Each Fibonacci number is printed using
printf()
, followed by a space.
- The
Fibonacci Function:
- The function
Fibonacci(int x)
is a recursive function that calculates the Fibonacci number at positionx
. - Base Case: If
x
is 0 or 1, the function returns1
because the first two numbers in the Fibonacci sequence are both1
. - Recursive Case: For other values of
x
, the function recursively calls itself to compute the sum of the two preceding Fibonacci numbers (Fibonacci(x-1) + Fibonacci(x-2)
).
- The function
Output Formatting:
- After printing the Fibonacci numbers, a newline (
\n
) is printed to move the cursor to the next line for clean output.
- After printing the Fibonacci numbers, a newline (
Sample Output:
If the user enters 6
as the number of terms, the program will output
Enter the number of terms of Fibonacci Series which is going to be printed: 6
1 1 2 3 5 8
This is the Fibonacci series for the first 6 terms: 1, 1, 2, 3, 5, 8
.
How the Fibonacci Function Works:
The recursive function Fibonacci(x)
computes the Fibonacci number for position x
by adding the results of the two previous positions. Let’s look at an example for Fibonacci(4)
:
Fibonacci(4) = Fibonacci(3) + Fibonacci(2)
= (Fibonacci(2) + Fibonacci(1)) + (Fibonacci(1) + Fibonacci(0))
= ((Fibonacci(1) + Fibonacci(0)) + 1) + (1 + 1)
= ((1 + 1) + 1) + (1 + 1)
= 2 + 2
= 3
This is how the program computes each Fibonacci number recursively.