C program to print the Fibonacci series up to n terms
Here we are going to see how we can generate the Fibonacci series in C using for loop. In the code below, we will initialize the variable at first. The first two variables of the Fibonacci series are always both 1. We will generate the other series after the first number by adding the first two numbers. The code for generating Fibonacci series is mentioned below.
//To calculate Febonacci series upto n terms #include<stdio.h> int main(){ int f1=1, f2=1, f3, n, i; printf("How many terms do you want to generate? "); scanf("%d", &n); printf("%d %d ",f1,f2); for(i=1; i<=(n-2);i++) { f3=f1+f2; printf("%d ",f3); f1=f2; f2=f3; } }
The output of the above code can be displayed as below.
Output:
How many terms do you want to generate? 6
1 1 2 3 5 8
1 1 2 3 5 8