×

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 aValue of c1Value of c2uptterm
3011
4112
5123
6235
7358
85812

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.


Related Topics

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.