📅  最后修改于: 2023-12-03 15:29:43.290000             🧑  作者: Mango
Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In this article, we present a C program for Fibonacci series.
#include <stdio.h>
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 1; i <= n; ++i)
{
// Prints the first two terms.
if(i == 1)
{
printf("%d, ", t1);
continue;
}
if(i == 2)
{
printf("%d, ", t2);
continue;
}
// Calculates the next term.
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("%d, ", nextTerm);
}
return 0;
}
The above code is a C program for Fibonacci series. Here is how it works:
nextTerm = t1 + t2
.In this article, we presented a C program for Fibonacci series. This code can be used to print any number of terms in the Fibonacci series.