Question #21189

Create a function int Fibonacci(int n), that will calculate the Fibonacci number.
Use the parameters in main, int argc, char *argv[].

Expert's answer

#include <iostream>

#include <conio.h>

using namespace std;

int Fibonacci(int n)

{

int curr = 1;

int prev = 1;

int tmp;

for (int c = 3; c <= n; c++)

{

tmp = curr;

curr += prev;

prev = tmp;

}

return curr;

}

void main(int argc, char* argv[])

{

if (argc != 2)

return;

int n = atoi(argv[1]);

cout << "Fibonacci number for N = " << n << " is " << Fibonacci(n) << endl;

getch();

}

#include <conio.h>

using namespace std;

int Fibonacci(int n)

{

int curr = 1;

int prev = 1;

int tmp;

for (int c = 3; c <= n; c++)

{

tmp = curr;

curr += prev;

prev = tmp;

}

return curr;

}

void main(int argc, char* argv[])

{

if (argc != 2)

return;

int n = atoi(argv[1]);

cout << "Fibonacci number for N = " << n << " is " << Fibonacci(n) << endl;

getch();

}

## Comments

## Leave a comment