Q. Write a program to display the first n Fibonacci numbers
Program
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c=1;
printf("Enter the number of terms\n");
scanf("%d", &n);
printf("First %d terms of Fibonacci series are:\n", n);
while(c <= n)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\t", next);
c++;
}
return 0;
}
Output:
Enter the number of terms
5
First 5 terms of Fibonacci series are:
1 1 2 3 5
Program
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c=1;
printf("Enter the number of terms\n");
scanf("%d", &n);
printf("First %d terms of Fibonacci series are:\n", n);
while(c <= n)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\t", next);
c++;
}
return 0;
}
Output:
Enter the number of terms
5
First 5 terms of Fibonacci series are:
1 1 2 3 5
0 Comments