Fibonacci series in C using for loop
In this post, you will learn how to write a Fibonacci series of program using for loop in the C programming language.
The Fibonacci series is the sequence of numbers in which the next number is the sum of the previous two numbers. The Fibonacci series was well-known hundreds of years earlier. The "Fibonacci" name came from the nickname "Bonacci".
We can easily remember Fibonacci Sequence using the Fibonacci Day, which is November 23rd. As 23rd November has the digits "1, 1, 2, 3" which is part of the sequence.
In the above image, the first two numbers are 0 and 1. So, according to the Fibonacci rule, the third number is 1 (sum of 0 and 1). The fourth number is 2, and so on.
0 + 1 = 1 // 0, 1, 1
1 + 1 = 2 // 0, 1, 1, 2
1 + 2 = 3 // 0, 1, 1, 2, 3
2 + 3 = 5 // 0, 1, 1, 2, 3, 5
0 ,1 , 1, 2, 3, 5, 8, 13, 21, 34....
C program to print Fibonacci series using for loop
Here, you will learn how to print the Fibonacci series using a for loop in the C programming language. In this below code, f1 contains the first number, i.e., 0 and f2 contains the second number, i.e., 1 and n contains the total Fibonacci series number count.
#include<stdio.h>
#include<conio.h>
int main ()
{
int f1=0, f2=1, f3, i, n;
printf ("Enter the length of the Fibonacci series: \n");
scanf ("%d", &n);
printf ("The Fibonacci series:\n");
printf ("%d\n%d\n", f1, f2);
for (i = 2; i <= n; i++)
{
f3 = f1 + f2;
printf ("%d\n", f3);
f1 = f2;
f2 = f3;
}
return 0;
}
Output of the above code:
Enter the length of the Fibonacci series:
15
The Fibonacci series:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative