# Fibonacci series in C using while 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 while loop in the C programming language is following:

``````while (test expression or termination condition) {
// the main body of the ‘while’ loop is written here}
``````

### Working of While loop in C

Initially, we use the parentheses for storing the termination condition of the While loop. Then, there can be two outcomes True or False.
Firstly, if the result is true, then the control will automatically execute the body of the loop. It will continue to evaluate again and again until the termination condition is false.
Secondly, if the result is false, then the control will definitely come out of the While loop and will dismiss the While Loop.

### 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 nth terms

``````#include<stdio.h>
int main ()
{
int a=0, b=1, c, x=3, terms; /* declaring variables and initializing values of all variables */
printf ("Enter the no. of terms of the Fibonacci series you want to print: "); /* taking input from the user to print the result */
scanf ("%d", &terms); /* assigning the value entered to the variable -'terms' */
printf ("%d\t %d", a, b); /* program command to print the values of 'a' and 'b' */
while(x<=terms) /* termination condition or test expression */
{
c=b + a; /* using assignment operator to assign the sum total value of 'b' and 'a'to 'c' */
printf ("\t %d", c); /* printing the value of variable 'c' that is equivalent to (b=a) */
a=b; /* using assignment operator to assign the value of 'a' to the 'b' */
b=c; /* using assignment operator to assign the value of 'b' to the 'c' */
x=x+1; /* using assignment operator to assign the incremented value of 'x' to 'x' itself */
}
return 0;
}
``````

Output:

``````Enter the no. of terms of the Fibonacci series you want to print: 15
0       1        1       2       3       5       8       13      21      34      55      89      144     233     377
``````

### Program to print the Fibonacci Series Up to a Certain Number

``````#include <stdio.h>
int main ()
{
int c1 = 0, c2 = 1, upterm = 0, b;  /* declaring variables and initializing values of c1, c2, and upterm variables */
printf ("Enter the no. of terms of the Fibonacci series you want to print: "); /* taking input from the user to print the result */
scanf ("%d", &b); // assigning the value entered by the user to ‘b’
printf ("Fibonacci Series is as follow:\t%d\t%d, ", c1, c2); // printing the values of the first two declared terms ‘c1’ and ‘c2’
uptterm = c1 + c2; /* using assignment operator to assign the sum of 'c1' and 'c2' to 'upterm' */
while (upterm <= b) // while (termination condition of the while loop)
{
printf ("\t%d", upterm);
c1 = c2; /* using assignment operator to assign the value of 'c1' to the 'c2' */
c2 = uptterm; /* using assignment operator to assign the value of 'c2' to the 'uptterm' */
uptterm = c1 + c2; /* using assignment operator to assign the sum value of ‘c1’ and ‘c2’*/
}
return 0;
}
``````

Output:

``````Enter the no. of terms of the Fibonacci series you want to print: 15
Fibonacci Series is as follow:  0       1        1       2       3       5       8       13
``````

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.

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.

The following table describes how the for loop works in the above program:

t1       t2       uptterm                    (uptterm<=15)

0        1            1                       true Print uptterm.

1        1            2                       true Print uptterm.

1        2            3                       true Print uptterm.

2        3            5                       true Print uptterm.

3        5            8                       true Print uptterm.

5        8            13                     true Print uptterm.

8        13           21                     false Terminate loop.