×

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

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.