Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Fibonacci Sequence

#include <stdio.h>
int main() {

  int i, n;

  // initialize first and second terms
  int value1 = 0, value2 = 1;

  // initialize the next term (3rd term)
  int nextTerm = value1 + value2;

  // get no. of terms from user
  printf("Enter the number of terms: ");
  scanf("%d", &n);

  // print the first two terms value1 and value2
  printf("Fibonacci Series: %d, %d, ", value1, value2);

  // print 3rd to nth terms
  for (i = 3; i <= n; ++i) {
    printf("%d, ", nextTerm);
    value1 = value2;
    value2 = nextTerm;
    nextTerm = value1 + value2;
  }

  return 0;
}

    Leave a Reply

    Your email address will not be published.

    Need Help?