nth-fibonacci

mail@pastecode.io avatar
unknown
c_cpp
5 months ago
425 B
5
Indexable
#include<stdio.h>
int main()
{
    int n;
    printf("Enter the value of n: ");
    scanf("%d", &n);

    int a = 0, b = 1;
    
    // printing the 0th and 1st term
    printf("%d, %d", a, b);
    
    int nextTerm;
    
    // printing the rest of the terms here
    for(int i = 2; i < n; i++){
        nextTerm = a + b;
        a = nextTerm;
        b = a;
        
        printf(", %d", nextTerm);
    }

    return 0;
}
Leave a Comment