Fibonacci Sequence of n-terms

Spread the love

Problem Statement

Write a program to generate and print the first n-terms of the Fibonacci Sequence where n>=1

Code

//Fibonacci Sequence without Recursion

#include <stdio.h>
int main()
{
int n1=0, n2=1, n3, i, n;
   printf(“Enter the number of terms: “);
        scanf(“%d”, &n);
  if(n==1){
    printf(“%d”, n1);
}
  else{
    if(n==2){
printf(“%d\n%d”, n1,n2);
        }
    else{
printf(“%d\n%d\n”, n1,n2);
for(i=3; i<=n; i++){
n3=n1+n2;
printf(“%d\n”,n3);
n1=n2;
n2=n3;
}
    }
    }
/* https://tech.arclasses.net */
    return 0;
}

Sample Input

4

Sample Output

0

1

1

2

Read more…


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *