-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.c
39 lines (30 loc) · 928 Bytes
/
fibonacci.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
void generateFibonacci(int n) {
int t1 = 0, t2 = 1, nextTerm;
printf("\nFibonacci Sequence up to %d terms:\n", n);
printf("%d, %d", t1, t2);
for (int i = 3; i <= n; i++) {
nextTerm = t1 + t2;
printf(", %d", nextTerm);
t1 = t2;
t2 = nextTerm;
}
printf("\n\n");
}
int main() {
int n;
printf("====================================\n");
printf(" Fibonacci Sequence Generator \n");
printf("====================================\n");
printf("Enter the number of terms you want to generate: ");
scanf("%d", &n);
if (n <= 0) {
printf("\nPlease enter a positive integer.\n\n");
} else {
generateFibonacci(n);
}
printf("====================================\n");
printf(" Program Ended \n");
printf("====================================\n");
return 0;
}