Write a C Program to Generate Fibonacci Series
Source Code:
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ") ;
if(n==1)
printf("%d",t1);
else if(n>1)
printf("%d, %d, ", t1, t2);
else
printf("Enter number greater than 0");
for (i = 3; i <= n; i++) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Output:
Enter the number of terms: 6
Fibonacci Series: 0, 1, 1, 2, 3, 5,
No comments:
Post a Comment