Google Add

Search

C Program to Print Fibonacci Series using While, Do While & For Loop

Write a C program to print fibonacci series. In this tutorial, We'll write a c program to print fibonacci series using while, do while and for loop.

C Program to Print Fibonacci Series using While, Do While & For Loop

What is Fibonacci series?


In Fibonacci series, The first two numbers are 0 and 1, and each subsequent numbers are the sum of previous two numbers. For example- 0  1  1  2  3  5  8  13 ............


So first two numbers are 0 and 1, next number 1 is the sum of 0 + 1. Similarly, 2 is the sum of 1 + 1 and so on.


Interview Questions For Practice

C Program to Print Fibonacci Series using Recursion


C Program to Print Fibonacci Series using For Loop


#include <stdio.h>

int main()
{
    int i, n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);


    for(i = 2; i < n; i++){
  
     /* Each subsequent number is the sum  
        of previous two numbers
     */

      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
  }

 return 0;
}


Output :

How many elements you want to print in a series  6

Fibonacci Series is

0   1   1   2   3   5

C Program to print factorial of a number

C Program to reverse a number

C Program to Print Fibonacci Series using While Loop

We have written a code to print Fibonacci series using for loop. In this example, We are going to write a C program to print Fibonacci series using while loop.

#include <stdio.h>

int main()
{
    int n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);

    /*
     First two elements of a fibonacci series is printed
     start printing number from index 2
     */
     
    int i = 2;
    
    //While loop
    while (i < n){
  
     /* Each subsequent number is the sum  
        of previous two numbers
     */

      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
      
      //Increment the value of i
      
      i++;
  }

  return 0;
}

C Program to Print Fibonacci Series using Do While Loop


In this example, we write to code to print Fibonacci series using do while loop.

#include <stdio.h>

int main()
{
    int n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);

    /*
     First two elements of a fibonacci series is printed
     start printing number from index 2
     */
     
    int i = 2;
    
    //Do while loop logic

    do {
    
    /* Each subsequent number is the sum  
        of previous two numbers
     */
      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
      
      //Increment the value of i
      i++;
       
    } while (i < n);

 return 0;
}

In this tutorial, I have explained how to print Fibonacci series using multiple code examples. If you have any doubt you can ask them through your comments.

No comments:

Post a Comment