Coding Practice

Write a C program to print Fibonacci series up to n terms

Print Fibonacci series up to n terms
Sample Output
Enter a limit: 10

Fibonacci series: 0 1 1 2 3 5 8 13 21 34
Source Code
// Print Fibonacci series up to n terms

#include<stdio.h>

int main()
{
    int i, limit, first = 0, second = 1, last;

    /*
    FirstNum + LastNum = LastNum
    Fibonacci series: 0 1 1 2 3 5 8 13. . .
    */

    printf("Enter a limit: ");
    scanf("%d", &limit);

    printf("\nFibonacci series: ");
    for(i = 1; i <= limit; i++)
    {
        printf("%d ", first);

        last = first + second;
        first = second;
        second = last;
    }
    printf("\n\n");

    return 0;
}
Sample Output
Enter a limit: 11

Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55

No comments:

Post a Comment

Change Theme
X