Fibonacci Series in C Using For Loop
In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers.
The syntax of for loop used in the C programming language is:
for (initialization statement; termination condition; increment or decrement statement (used for modifying value for further evaluation))
{
/* main body of the “for” loop */
}
Working of the for loop in C
In for loop, the initialization command is implemented only once. After that, it checks the test expression of the loop and finds whether the test expression is false or true. And, for loop is dismissed if the test expression is false.
If the test expression of the 'For loop becomes true, then the program line provided in the For loop’s body is executed, and the update expression is changed accordingly.
Again, the entire process of evaluation is to be done. The loop executes its body until and unless the value of the test expression in for loop becomes false. The loop automatically terminates as soon as the test expression result becomes false.
What is Fibonacci Series?
In the Fibonacci series, the next coming term is the sum of the previous two terms. But, 0 and 1 are always considered as the first two terms in the Fibonacci series.
The Fibonacci series commence like:
0, 1, 1, 2 and so on.
Program of Fibonacci series up to the given nth terms:
#include <stdio.h>
int main()
{
int a, b;
// declaring variables and initializing first and second terms without value
int c1 = 0, c2 = 1; // declaring the value of term 1 as c1 = 0 and term 2 as c2 = 1
// initializing the upcoming third term (upterm)
int upterm= t1 + t2;
printf("Enter the no. of terms of the Fibonacci series you want to print: "); // taking input from the user about the no. of terms user wants to print
scanf("%d", &b); // assigning the value entered by the user to b
printf("Fibonacci Series is as follow: %d, %d, ", c1, c2); // printing the values of the first two declared as terms c1 and c2
for (a = 3; a <= n; ++a) // (initialization statement; termination condition; increment or decrement statement)
// printing the 3rd term and to the nth terms
{
printf("%d, ", upterm);
c1 = c2;
c2 = upterm;
upterm = c1 + c2;
}
return 0;
}
Output:
Enter the no. of terms of the Fibonacci series you want to print: 8
Fibonacci Series is as follow: 0 1 1 2 3 5 8 13
Explanation:
Let us suppose the number entered by the user is 10 for b variable. Initially, we will have to print the first two terms of the Fibonacci series prior to using a “for” loop which prints the upcoming b terms.
Let us see how the for loop works in the following table:
Value of a | Value of c1 | Value of c2 | uptterm |
3 | 0 | 1 | 1 |
4 | 1 | 1 | 2 |
5 | 1 | 2 | 3 |
6 | 2 | 3 | 5 |
7 | 3 | 5 | 8 |
8 | 5 | 8 | 12 |
Program to print the Fibonacci Series Up to a Certain Number:
#include <stdio.h>
int main()
{
int c1 = 0, c2 = 1, upterm = 0, b;
printf("Enter the no. of terms of the Fibonacci series you want to print: ");
scanf("%d", &b); // assigning the value entered by the user to b
printf("Fibonacci Series is as follow: %d, %d, ", c1, c2); // printing the values of the first two declared as terms c1 and c2
upterm = c1 + c2;
while (nextTerm <= b) // (termination condition of the while loop)
{
printf("%d, ", upterm);
c1 = c2;
c2 = upterm;
upterm = c1 + c2;
}
return 0;
}
Output:
Enter the no. of terms of the Fibonacci series you want to print: 100
Fibonacci Series is as follow: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
In this program, we have used a while loop to print all the Fibonacci numbers up to nth terms.
Suppose, if the value of ‘b’ is considered as part of the Fibonacci series, then we will just print the Fibonacci series up to the natural positive number which is closest and lesser than the value of b entered by the user.
The following table describes how the for loop works in the above program:
t1 t2 uptterm uptterm <= n
0 1 1 true. Print uptterm.
1 1 2 true. Print uptterm.
1 2 3 true. Print uptterm.
... ... ... ...
34 55 89 true. Print uptterm.
55 89 144 false. Terminate Loop.