C Program to Find First N Fibonacci Numbers - IProgramX

Q. Write a program to display the first n Fibonacci numbers


Program

#include <stdio.h>
int main()
{
  int n, first = 0, second = 1, next, c=1;

  printf("Enter the number of terms\n");
  scanf("%d", &n);

  printf("First %d terms of Fibonacci series are:\n", n);

  while(c <= n)
  {
    if (c <= 1)
      next = c;
    else
    {
      next = first + second;
      first = second;
      second = next;
    }
    printf("%d\t", next);
    c++;
  }

  return 0;
}

Output:

Enter the number of terms
5
First 5 terms of Fibonacci series are:
1       1       2       3       5

Post a Comment

0 Comments