Fibonacci Sequence in C

This program generates the Fibonacci sequence up to a given number of terms.

#include <stdio.h>

 int main() {
    int n, t1 = 0, t2 = 1, nextTerm = 0;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
 
    printf("Fibonacci Series: %d, %d, ", t1, t2);
    nextTerm = t1 + t2;
 
    for (int i = 3; i <= n; ++i) {
        printf("%d, ", nextTerm);
        t1 = t2;
        t2 = nextTerm;
        nextTerm = t1 + t2;
    }
    printf("\b\b \n");  // Remove the last comma and space
    return 0;
}

No comments:

Post a Comment

MS Excel Logical Functions

Logical functions in Excel are powerful tools that help you make decisions based on conditions. Whether you're comparing values or testi...

Post Count

Loading...