56 lines
No EOL
1.7 KiB
C
56 lines
No EOL
1.7 KiB
C
// header files
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
// local constants
|
|
|
|
/*
|
|
Name: Fibonacci Sequence
|
|
Process: Accepts single integer, prints out up to nth term of the sequence.
|
|
Function input/parameters: argc and argv command line arguments
|
|
Function output/parameters: NONE
|
|
Function output/returned: NONE
|
|
Device input/---: NONE
|
|
Device output/---: NONE
|
|
Dependencies: included by standard libraries
|
|
*/
|
|
int main(int argc, char *argv[])
|
|
{
|
|
// define local variables
|
|
int fibonacciNumber = 0;
|
|
int prevFibonacciNumber, prevPrevFibonacciNumber;
|
|
int fibN1 = 0, fibN2 = 1;
|
|
int nthTerm = atoi(argv[1]);
|
|
int counter;
|
|
// do calculations
|
|
if(argc == 2 && nthTerm >= 0)
|
|
{
|
|
// check to see if the correct number of arguments are included
|
|
// fibonacci sequence loop until n.
|
|
fibonacciNumber = fibN1 + fibN2;
|
|
prevFibonacciNumber = fibonacciNumber;
|
|
fibonacciNumber = fibN2 + prevFibonacciNumber;
|
|
prevPrevFibonacciNumber = prevFibonacciNumber;
|
|
prevFibonacciNumber = fibonacciNumber;
|
|
for(counter = 0; counter < nthTerm - 2; counter++)
|
|
{
|
|
// get the next fibonacci number
|
|
fibonacciNumber = prevPrevFibonacciNumber + prevFibonacciNumber;
|
|
prevPrevFibonacciNumber = prevFibonacciNumber;
|
|
prevFibonacciNumber = fibonacciNumber;
|
|
}
|
|
printf("The %dth term of the Fibonacci Sequence is: %d\n", nthTerm, prevPrevFibonacciNumber);
|
|
}
|
|
else
|
|
{
|
|
// fail program
|
|
printf("Please provide one integer greater than zero.\n");
|
|
return -1;
|
|
}
|
|
// display results
|
|
// done in do calculations
|
|
// return success
|
|
return 0;
|
|
} |