-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonnacci.c
64 lines (50 loc) · 1.47 KB
/
fibonnacci.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
// Function to displays the fibonnacci sequence of a given number
void fibonnacci(int user_input, int term_1, int term_2, int next_term);
void main()
{
// Store the positive interger input by a user
int number=0;
// Display purpose of the program
printf("The program that calculates the fibonnacci sequence of a given number.\n\n");
// Ensure input is a positive interger
do
{
printf("Enter a positive number: ");
scanf("%d", &number);
} while (number < 0);
// Begin the fibonnacci sequence
int term_1=0, term_2=1;
printf("\nThe fibonnacci sequence.\n");
// Incase the input is 0
if (number == 0)
{
printf(" %d ", term_1);
}
// Incase the input is 1
else if (number == 1)
{
printf(" %d %d ", term_1, term_2);
}
// Incase other numbers have been input
else
{
printf(" %d %d ", term_1, term_2);
int next_term = term_1 + term_2;
fibonnacci(number, term_1, term_2, next_term);
}
}
// Function to displays the fibonnacci sequence of a given number
void fibonnacci(int user_input, int term_1, int term_2, int next_term)
{
// Stop when the expected next term exceeds the user input
if (next_term > user_input)
{
return;
}
printf(" %d ", next_term);
term_1 = term_2;
term_2 = next_term;
next_term = term_1 + term_2;
fibonnacci(user_input, term_1, term_2, next_term);
}