fibonacci number from main(). For this problem, the 3th Fibonacci number is 1, t
ID: 3620343 • Letter: F
Question
fibonacci number from main(). For this problem, the 3th
Fibonacci number is 1, the 4th is 2, the 5th is 3, etc.
(The series is: 0, 1, 1, 2, 3, 5 etc)
b) Pass the input value to the function fibonacci(n) that
returns the value. Display the value to the console.
c) Have your program calculate the value of the largest fibonacci
number. Do not hard-code this into your program. The purpose is
to be able to take your code to another system and discover the
value without changing your code. If you are unable to do this
just print a statement you do not have a solution for this part
of the assignment.
Example: Which fibonacci number do you want? 5
The 5th fibonacci number is: 3
The largest fibonacci number that can be calculated on
this system is: (number here)
Explanation / Answer
please rate - thanks #include<conio.h>#include<stdio.h>
#include<limits.h>
int fibonacci(int);
int main()
{int n;
int fibnum;
printf("Which fibonacci number do you want?");
scanf("%d",&n);
fibnum=fibonacci(n);
printf("The %dth fibonacci number is: %d ",n,fibnum);
fibnum=fibonacci(50000); //generate a large number of fibonacci numbers
printf("The largest fibonacci number that can be calculated on ");
printf("this system is %d",fibnum);
getch();
return 0;
}
int fibonacci(int n)
{int first=0,second=1,next=0;
int i;
for(i=0;i<n-2; i++)
{
next=first+second;
if(next>INT_MAX||next<0) //too big?
return second;
first=second;
second=next;
}
return next;
}
-------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.