Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <string.h>
- #include <vector>
- using namespace std;
- int fibonacci(int row);
- int main(void)
- {
- int userValue = 0;
- cout<<"Enter desired row of fibonaci : ";
- cin>>userValue;
- cout<<"\n"<<fibonacci(userValue)<<endl;
- return 0;
- }
- int fibonacci(int row)
- {
- int result = 0;
- int nmtwo = 0;
- int nmone = 0;
- if(row == 0)
- {
- cout<<"Finbonacci's row start at one"<<endl;
- //return 0;
- exit(0);
- }
- for (int i=0;i<=row;i++)
- {
- int tmp = nmtwo;
- nmtwo = nmone;
- nmone = nmone + tmp;
- if(i == row)
- {
- result = nmone; }
- if (i == 1)
- {
- nmone = 1;
- result = 0;
- }
- else if (i == 2)
- {
- nmtwo = 0;
- nmone = 1;
- result = 1;
- }
- }
- return result;
- }
- //sudo gcc main.cpp -lstdc++ -g -w -o fibonacci
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement