Last active
January 11, 2021 01:05
-
-
Save bebyx/ba64d3e7cc53367b0228e1d4ceda1173 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int fibonacci(int num); | |
int main() { | |
int i; | |
printf("Enter number:\n"); | |
int result = scanf("%d", &i); | |
if (result == 0) { | |
printf("Need integer input!\n"); | |
return 1; | |
} else if (i < 0) { | |
printf("Need natural number!\n"); | |
return 1; | |
} | |
printf("Fibonacci number: %d\n", fibonacci(i)); | |
return 0; | |
} | |
int fibonacci(int num) { | |
if (num == 0) { | |
return num; | |
} else if (num == 1) { | |
return num; | |
} else { | |
num = fibonacci(num - 1) + fibonacci(num - 2); | |
return num; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment