toolbar maker A computer science portal for programming: C Program to Fibonacci Series

Tuesday, May 19, 2020

C Program to Fibonacci Series

Fibonacci Series

The Fibonacci series are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ……..
In mathematical terms, the sequence Xn of Fibonacci series is defined by this formula
Xn+2= Xn+1 + Xn

Program to Fibonacci Sequence Up to a Certain Number

   #include <stdio.h>
int main() {
    int t1 = 0, t2 = 1, nextTerm = 0, n;
    printf("Enter a positive number: ");
    scanf("%d", &n);

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

    while (nextTerm <= n) {
        printf("%d, ", nextTerm);
        t1 = t2;
        t2 = nextTerm;
        nextTerm = t1 + t2;
    }

    return 0;
}
Output:
Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

No comments:

Post a Comment

https://www.programc.in/

C Programming examples