Thursday, April 2, 2020

c program fibonacci sequence upto certain number using while loop algorithm

C program fibonacci sequence upto certain number using while loop algorithm C program

#include 
int main()
{
    int a1 = 0, a2 = 1, nextTerm = 0, n; //  integer variables
    printf("Enter a positive number: "); // user input value
    scanf("%d", &n); // storing user entered value

    // displays the first two terms which is always 0 and 1
    printf("Fibonacci Series: %d, %d, ", a1, a2);
    nextTerm = a1 + a2;

    while (nextTerm <= n) // while loop 
    {
        printf("%d, ", nextTerm);
        a1 = a2;
        a2 = nextTerm;
        nextTerm = a1 + a2;
    }

    return 0;
}

Output

Enter a positive integer = 100
fibonacci series = 0,1,1,2,3,5,8,13,21,34,55,89,
Happy Coding
Location: Pakistan

0 comments:

Post a Comment

Recent Posts