Skip to content
doubtly

2 answers

Best answer

Answer: A function fibonacci(n) starts with a = 0, b = 1 and repeatedly prints a and moves forward (c = a + b; a = b; b = c).

Logic: each Fibonacci term is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, …

Program:

#include <stdio.h>

void fibonacci(int n)
{
    int a = 0, b = 1, c, i;

    for (i = 1; i <= n; i++)
    {
        printf("%d ", a);
        c = a + b;
        a = b;
        b = c;
    }
    printf("\n");
}

int main()
{
    int n;

    printf("Enter number of terms: ");
    scanf("%d", &n);
    fibonacci(n);

    return 0;
}

Sample output (actual run):

Enter number of terms: 8
0 1 1 2 3 5 8 13 
Doubtly · 23 Apr 2022 Report
Was this answer helpful?

Answer: a function that starts with 0 and 1 and prints each next term as the sum of the previous two.

#include <stdio.h>

void fibonacci(int n) {
    int a = 0, b = 1, next, i;
    for (i = 1; i <= n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
}

int main() {
    int n;
    printf("How many Fibonacci terms? ");
    scanf("%d", &n);
    fibonacci(n);
    return 0;
}

Explanation: a and b hold two consecutive terms. In each pass the function prints a, computes the next term a + b, and shifts the pair forward. The loop runs n times, so exactly n terms are printed.

Sample: n = 8 prints 0 1 1 2 3 5 8 13.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.