Can someone help with this problem with three modifications below? It has to wor
ID: 3564044 • Letter: C
Question
Can someone help with this problem with three modifications below? It has to work in putty with description of the code please. Thanks.
The fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21,...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. a) write a nonrecursive function fibonacci (n) that calculates the n fibonacci number. b) determine the largest fibonacci number that can be printed on your system. modify the program of part a) to use double instead of int to calculate and return fibonacci numbers. let the program loop until it fails because of an excessively high value.
There is a modification to this exercise. the modifications is as follows:
a) Prompt the user for an integer n that calcualtes the nth
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
Hi , Updated program,with error noted by you. In function 'main': warning: return type of 'main' is not 'int'
and its not an error, its comiler problem of your machine and my machine ...
I fixed this issue.
Can you check now?
=============
Updated program
=============
#include<stdio.h>
int fibonacci(int n);
int largestFibonacciNum();
int main()
{
int n;
int nthFibNum;
int largestFibNum;
printf("Which fibonacci number do you want? ");
scanf("%d",&n);
nthFibNum = fibonacci(n);
printf(" The %dth fibonacci number is: = %d",n,nthFibNum);
largestFibNum = largestFibonacciNum(n);
printf(" The largest fibonacci number that can be calculated on this system is: = %d",largestFibNum);
return(1);
}
int fibonacci(int n)
{
int i;
int a=0,b=1.0,c=0;
if(n==1)
return a;
if(n==2)
return b;
for(i=2;i<n;i++) // for printing from 3rd number in series
{
c=a+b;
a=b;
b=c;
}
return c;
}
int largestFibonacciNum()
{
int temp;
int last = 1;
int fib = 1;
while (fib + last > fib) {
temp = fib;
fib += last;
last = temp;
}
return fib;
}
===================
Output
=====================
Which fibonacci number do you want?
The 5th fibonacci number is: = 3
The largest fibonacci number that can be calculated on this system is: = 1836311903
==================================================================================