Write a C program to Display Fibonacci Series Up to n Terms.

Function Main
    Declare Integer i
    Declare Integer n
    Declare Integer t1
    Declare Integer t2
    Assign t1 = 0
    Assign t2 = 1
    Declare Integer nextTerm
    Assign nextTerm = t1 + t2
    Output "Enter the number of terms: "
    Input n
    Output "Fibonacci Series: " &t1
    Output ", " &t2
    For i = 3 to n
        Output ", " &nextTerm
        Assign t1 = t2
        Assign t2 = nextTerm
        Assign nextTerm = t1 + t2
    End
End

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

  int i, n;
  int t1 = 0, t2 = 1;
  int nextTerm = t1 + t2;
  printf("Enter the number of terms: ");
  scanf("%d", &n);

  printf("Fibonacci Series: %d, %d, ", t1, t2);

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

  return 0;
}

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

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.