Index « Previous Next »

Question

Write a program to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34 55 89...

Source Code

#include <stdio.h>

int main()
{
  int first = 0, second = 1, third, n, i;

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

  printf("%d %d", first, second);

  for (i = 3; i <= n; i++)
  {
    third = first + second;
    printf(" %d", third);
    first = second;
    second = third;
  }

  return 0;
}

Output

Enter the number of terms of Series : 10
0 1 1 2 3 5 8 13 21 34