C program to print fibonacci series:
The Fibonacci sequence is a series where the next term is the sum of pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1.
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
#Example
  #include <stdio.h>
  void main()
  {
     int i, n, t1 = 0, t2 = 1, nextTerm = 0;
     printf("Enter the number of terms: ");
     scanf("%d", &n);
     printf("Fibonacci Series: ");
     for (i = 1; i <= n; ++i)
     {
        // Prints the first two terms.
        if(i == 1)
        {
            printf("%d, ", t1);
            continue;
        }
        if(i == 2)
        {
            printf("%d, ", t2);
            continue;
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        printf("%d, ", nextTerm);
     }
  }
Output:
Enter the number of terms: 10 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

 
 
 
 
 
0 comments:
Post a Comment