chore(C): add fibonacci series (#666)

pull/675/head
Anish 2022-01-17 18:20:23 +05:30 committed by GitHub
parent f97cb54a2e
commit 0c32ec78cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -32,6 +32,7 @@
## Maths ## Maths
- [Palindrome Number](maths/palindrome.c) - [Palindrome Number](maths/palindrome.c)
- [Fibonacci Series](maths/fibonacci-series.c)
## Queues ## Queues

View File

@ -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 <stdio.h>
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;
}