C language Please!! 13.23 Functions - Ex3 In mathematics, the Fibonacci numbers
ID: 3735148 • Letter: C
Question
C language Please!!
13.23 Functions - Ex3
In mathematics, the Fibonacci numbers (in this case only positive numbers) are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:
Write a function called fibonacci that takes two integers arguments and prints out in ascending order all fibonacci numbers between the first (or closest higher or equal) and the second argument (closest lower or equal). Your function must return the amount of fibonacci numbers printed out.
Example 1:
When the input is:
The output is
The function must return 5.
Example 2:
When the input is:
The output is
The function must return 7.
Requirements
Read the inputs from the user
Declare the prototype of your function before the main function
Call your function to do the printing based on the inputs from the user
Print a space after each number including the last one printed
Explanation / Answer
#include<stdio.h>
int fibonacci(int n1, int n2) {
int a =0,b=1,c;
int n = 0;
c = a + b;
while(c<=n2) {
a=b;
b=c;
if(c>=n1) {
printf("%d ", c);
n++;
}
c = a + b;
}
printf(" ");
return n;
}
int main(void) {
int n1, n2, result;
printf("Enter the two numbers: ");
scanf("%d %d", &n1,&n2);
result = fibonacci (n1,n2);
printf("Result: %d ", result);
return 0;
}
Output:
Enter the two numbers: 4 100
5 8 13 21 34 55 89
Result: 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.