diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 37bd98e3..adb67ee7 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -32,6 +32,7 @@ ## Maths - [Palindrome Number](maths/palindrome.c) +- [Fibonacci Series](maths/fibonacci-series.c) ## Queues diff --git a/algorithms/C/maths/fibonacci-series.c b/algorithms/C/maths/fibonacci-series.c new file mode 100644 index 00000000..b42d0580 --- /dev/null +++ b/algorithms/C/maths/fibonacci-series.c @@ -0,0 +1,35 @@ +/** + * The Fibonacci Sequence is the series of numbers: + * + * 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... + * + * The next number is found by adding up the two numbers before it:, next number is the sum of previous two numbers + * for example 0, + * 1, 1, 2, 3, 5, 8, 13, 21 etc. + * The first two numbers of fibonacci series are 0 and 1. + * Time complexity: O(n) +* */ + +#include + +void fibonacci(int n) +{ + int a = 0, b = 1, c = 0; + printf("%d %d ", a, b); // print 0 1 + for (int i = 2; i < n; i++) //loop starts from 2 because 0 and 1 are already printed + { + c = a + b; + printf("%d ", c); + a = b; + b = c; + } +} + +int main() +{ + int n; + printf("Enter the number of terms: "); + scanf("%d", &n); + fibonacci(n); + return 0; +} \ No newline at end of file