Generating the Fibonacci series involves producing a sequence where each number is the sum of the two preceding ones, starting from 0 and 1. Here’s the algorithm, C program, and detailed explanation for generating the Fibonacci series.
Algorithm to Generate Fibonacci Series
Plaintext
Step 1: Start
Step 2: Input: Read the number of terms n (positive integer) to generate in the Fibonacci series.
Step 3: Process:
- Initialize variables a, b, and c.
- a and b represent the first two terms of the Fibonacci series
(a = 0 and b = 1).
- Print a (first term of the series).
- Print b (second term of the series).
For i from 3 to n:
- Calculate c = a + b.
- Print c.
- Update a to b and b to c for the next iteration.
Step 4: Output: Display the Fibonacci series up to n terms.
Step 5: End
C Program to Generate Fibonacci Series
C
#include <stdio.h>
int main() {
int n, a = 0, b = 1, c, i;
// Step 2: Read the number of terms from the user
printf("Enter the number of terms in Fibonacci series: ");
scanf("%d", &n);
// Step 3: Generate and print Fibonacci series
printf("Fibonacci Series up to %d terms:\n", n);
// Print the first two terms of Fibonacci series
printf("%d %d ", a, b);
// Generate and print the rest of the series
for (i = 3; i <= n; i++) {
c = a + b; // Calculate next term
printf("%d ", c); // Print the next term
a = b; // Update a to b
b = c; // Update b to c
}
// Step 5: End
return 0;
}
Explanation of Each Step
Include Header:
C
#include <stdio.h>
- This line includes the standard input-output library.
Main Function:
C
int main() {
- The main function is the entry point of the C program.
Variable Declaration:
C
int n, a = 0, b = 1, c, i;
- Four integer variables are declared:
n
to store the number of terms in the Fibonacci series,a
andb
to store the first two terms (0 and 1 respectively), c to store the next term in the series, and i for iteration.
Reading Input:
C
printf("Enter the number of terms in Fibonacci series: ");
scanf("%d", &n);
- printf prompts the user to enter the number of terms (n) in the Fibonacci series.
- scanf reads the integer and stores it in the variable n.
Generating and Printing Fibonacci Series:
C
printf("Fibonacci Series up to %d terms:\n", n);
- printf displays a message indicating the number of terms for the Fibonacci series.
Printing First Two Terms:
C
printf("%d %d ", a, b);
- Prints with the initial values of a (0) and b (1), which are the first two terms of the Fibonacci series.
Generating the Rest of the Series:
C
for (i = 3; i <= n; i++) {
c = a + b; // Calculate next term
printf("%d ", c); // Print the next term
a = b; // Update a to b
b = c; // Update b to c
}
- The for loop iterates from
3
to n (inclusive). - In each iteration:
- c is calculated as the sum of a and b, which is the next term in the Fibonacci series.
- c is printed using printf.
- a is updated to b (a now holds the previous value of b).
- b is updated to c (b now holds the current value of c).
End:
C
return 0;
- This line indicates successful completion of the program.
This program effectively generates and prints the Fibonacci series up to n
terms as specified by the user. It uses iterative logic to calculate each subsequent term based on the previous two terms (a
and b
).
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.