Coding Challenges < C >

What will be the output of this program when n is 50?

#include <stdio.h>

void fibonacci(int n) {
    int a = 0, b = 1, c = 0;
    printf("Fibonacci Sequence up to %d:\n", n);
    while (c <= n) {
        printf("%d ", c);
        a = b;
        b = c;
        c = a + b;
    }
    printf("\n");
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    fibonacci(n);
    return 0;
}
Solution
Fibonacci Sequence up to 50:
0 1 1 2 3 5 8 13 21 34
Was this page helpful?