Generating Fibonacci Numbers in C


This program is a simple program that generates and prints the first 'n' Fibonacci numbers. The program starts by including the stdio.h header file, which contains the declarations of the functions used in the program.

In the main function, the program declares three integer variables, "a", "b", and "c" , which are used to generate the Fibonacci numbers, an integer variable "n" to store the limit of numbers to generate, and an integer variable "i" for the loop. Initially, "a" and "b" are assigned the values -1 and 1 respectively, which are the first two numbers in the Fibonacci series.

It uses the printf function to prompt the user to enter the limit of numbers to generate, and uses the scanf function to read the input and store it in the "n" variable.

The program then uses a for loop to generate 'n' Fibonacci numbers. Inside the loop, the program assigns the value of "a+b" to "c" , and then assigns the value of "b" to "a" and the value of "c" to "b" . This is done repeatedly to generate the next numbers in the series.

Finally, it uses the printf function to print out the Fibonacci numbers and returns 0 to indicate that the program has executed successfully.

Source Code

//C Program to Display Fibonacci Sequence
//0, 1, 1, 2, 3, 5
#include<stdio.h>
int main()
{
    int a=-1,b=1,c,n,i;
    printf("\nEnter The Limit : ");
    scanf("%d",&n);//4
    for(i=0;i<n;i++)
    {
        c=a+b;//0  1 1 2
        a=b;//1 0 1
        b=c;//0 1 1
        printf("\n%d",c);
    }
     return 0;
}
 
To download raw file Click Here

Output

Enter The Limit : 10

0
1
1
2
3
5
8
13
21
34

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C