-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from chinmaykumbhare/master
Fixed: Solution for Fibonacci Sequence using C(Issue #15)
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
|
||
/** | ||
* GitHub username: chinmaykumbhare | ||
* | ||
* Issue Statement: | ||
* Implement Fibonacci sequence in any language. Recursive is appreciated. | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
void main (void) { | ||
|
||
int num = 0; | ||
|
||
//enter the number of elements you would like to display as output | ||
|
||
scanf(" %d", &num); | ||
|
||
//using 3 variables, namely, prev for previous term. current for current term and next for next term in the sequence | ||
|
||
int prev = 0, current = 1, next = 1; | ||
|
||
//loop for calculating and printing the next number in fibonacci series | ||
|
||
for(int loop = 0; loop < num; loop++) { | ||
|
||
printf("%d ", prev); | ||
|
||
next = current + prev; | ||
|
||
prev = current; | ||
|
||
current = next; | ||
|
||
} | ||
|
||
printf("\n"); | ||
|
||
} |