how do you make an application that will calculate a fabonac
how do you make an application that will calculate a fabonacci number with a given input. Give the user the choice of 25, 50, 75, 100 runs
Solution
Answer:
#include <iostream>
using namespace std;
int fibonaccinumber(int n)
{
int before = 1;
int now = 1;
int later = 1;
for (int i = 3; i <= n; ++i)
{
later = now + before;
before = now;
now = later;
}
return later;
}
int main()
{
int n;
while (1)
{
cout<<\"Enter integer n to calculate a fibonacci number and press 0 to exit): \";
cin>>n;
if (n == 0)
break;
cout<<fibonaccinumber(n)<<endl;
}
return 0;
}
Output :
Enter integer n to calculate a fibonacci number and press 0 to exit): 25
75025
Enter integer n to calculate a fibonacci number and press 0 to exit): 50
-298632863
Enter integer n to calculate a fibonacci number and press 0 to exit): 75
1845853122
Enter integer n to calculate a fibonacci number and press 0 to exit): 100
-980107325
Enter integer n to calculate a fibonacci number and press 0 to exit): 0
