Write a C program for the Fibonacci series of n numbers using array.
Fi = F(i-1) + F(i-2)
Example: if n =5
F1 = 1
F2 = 1
F3 = F2+ F1 = 1 + 1 = 2
F4 = F3 + F2 = 2 + 1 = 3
F5 = F4 + F3 = 3 + 2 = 5
#include <stdio.h>
int main()
{
int num;
printf("Enter the number of Fibonacci numbers: ");
scanf("%d", &num);
if (num > 0)
{
printf("F1 = 1\n");
if (num != 1)
{
printf("F2 = 1\n");
if (num != 2)
{
int pp = 1, p = 1, c;
for (int i = 3; i <= num; ++i)
{
c = p + pp;
printf("F%d = F%d + F%d = %d + %d = %d\n", i, i - 2, i - 1, pp, p, c);
pp = p;
p = c;
}
}
}
}
return 0;
}
Comments
Leave a comment