C Program to Find First N Fibonacci Numbers

This C program finds the first N Fibonacci numbers and displays them.

Problem statement

Write a C program to find the first N Fibonacci numbers.

C Program to Find First N Fibonacci Numbers

#include <stdio.h>

void fibonacci(int n) {
    int i, t1 = 0, t2 = 1, nextTerm;

    printf("The first %d Fibonacci numbers are: ", n);

    for (i = 1; i <= n; ++i) {
        printf("%d, ", t1);
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;

int main() {
    int n;

    printf("Enter the value of N: ");
    scanf("%d", &n);


    return 0;

How it works?

  1. The program defines a function fibonacci that takes an integer n as input.
  2. Inside the fibonacci function, two variables t1 and t2 are initialized to the first two Fibonacci numbers, 0 and 1.
  3. The program then enters a loop that runs n times. In each iteration, it prints the current Fibonacci number (t1), calculates the next Fibonacci number by adding t1 and t2, and updates the values of t1 and t2 accordingly.
  4. The loop continues until the desired number of Fibonacci numbers are printed.
  5. In the main function, the user is prompted to enter the value of N.
  6. The input value is read using scanf and stored in the variable n.
  7. Finally, the fibonacci function is called with the value of n as an argument.


C Program to Find First N Fibonacci Numbers