C Program to Generate Fibonacci Series

«
»

This is a C program to generate fibonacci series.

Problem Description

This C Program generates fibonacci series.

Problem Solution

In fibonacci series the first two numbers in the Fibonacci sequence are 0 and 1 and each subsequent number is the sum of the previous two. For example fibonacci series is 0, 1, 1, 2, 3, 5, 8,13, 21…………

advertisement
Program/Source Code

Here is source code of the C program to generate fibonacci series. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to generate Fibonacci Series. Fibonacci Series
 * is 0 1 1 2 3 5 8 13 21 ...
 */
#include <stdio.h>
 
void main()
{
    int  fib1 = 0, fib2 = 1, fib3, limit, count = 0;
 
    printf("Enter the limit to generate the Fibonacci Series \n");
    scanf("%d", &limit);
    printf("Fibonacci Series is ...\n");
    printf("%d\n", fib1);
    printf("%d\n", fib2);
    count = 2;
    while (count < limit)
    {
        fib3 = fib1 + fib2;
        count++;
        printf("%d\n", fib3);
        fib1 = fib2;
        fib2 = fib3;
    }
}
Program Explanation

In this C program, we are reading the limit to generate the Fibonacci series using limit variable. In Fibonacci series the first two numbers in the Fibonacci sequence are 0 and 1 and each subsequent number is the sum of the previous two. For example Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, 21…………

advertisement

Initially assign the value of ‘fib1’ variable as 0, the value of ‘fib2’ variable as 1 and the value of ‘count’ variable as 2. While loop is used to check the condition that the value of ‘count’ variable is less than the value of ‘limit’ variable.

If the condition is true then execute the loop. Compute the value of ‘fib1’ variable and the value of ‘fib2’ variable then assign the value to ‘fib3’ variable. Increment the value of ‘count’ variable by 1. Assign the value of ‘fib2’ variable to ‘fib1’ variable and the value of ‘fib3’ variable to ‘fib2’ variable. Print the Fibonacci series using printf statement.

Runtime Test Cases
 
$ cc pgm40.c
$ a.out
Enter the limit to generate the Fibonacci Series
6
Fibonacci Series is ...
0
1
1
2
3
5

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn