Skip to content

Instantly share code, notes, and snippets.

@stinkymonkeyph
Created August 1, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stinkymonkeyph/ca00549e8faf30987156f613e23a77e5 to your computer and use it in GitHub Desktop.
Save stinkymonkeyph/ca00549e8faf30987156f613e23a77e5 to your computer and use it in GitHub Desktop.
/*Author: Nelmin Jay Magan Anoc
*
*/
#include<stdio.h>
void printFibonnaciSequence(int);
int setNumberOfTerms();
int main(){
/*we simply call a setter function to set number of terms
*and pass the return value to print fibonacci sequence
*/
printFibonnaciSequence(setNumberOfTerms());
return 0;
}
int setNumberOfTerms(){
int numberOfTerms = 0;
printf("Enter the number of terms: ");
scanf("%d",&numberOfTerms);
return numberOfTerms;
}
void printFibonnaciSequence(int numberOfTerms){
int term1 = 0 ;
int term2 = 1 ;
int nextTerm = 0;
int incrementControl ;
/*we know that first two terms of fibo sequence is 0 and 1 so we print
*it directly
*/
printf("Fibonacci Sequence: %d, %d, ",term1,term2);
for(incrementControl=3; incrementControl <= numberOfTerms ; ++incrementControl){
nextTerm = term1 + term2 ;
term1 = term2;
term2 = nextTerm;
printf("%d, ",nextTerm);
}
/*the computation is always the sum of two consecutive integers
*next we swap these terms
*and print the result
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment