A series of whole numbers in which each number is the sum of the two preceding n
ID: 3548295 • Letter: A
Question
A series of whole numbers in which each number is the sum of the two preceding numbers.
Beginning with 0 and 1, called Fibonacci series. The sequence of Fibonacci numbers would
be 0,1,1, 2, 3, 5, 8, 13, 21, 34, etc. Using the formula n = n(?1) + n(?2), where the n(?1)
means "the last number before n in the series" and n(?2) refers to "the second last one
before n in the series." You can calculate Fibonacci series.
Write a program that inputs a integer n and prints n number in Fibonacci series. Here is the
sample run.
Your program: please insert n
User: 5
Your program: 3
Hint: you can use recursive function to solve the problem.
Explanation / Answer
#include <stdio.h>
//Recursive Function to get Fibonaci number
int fiboSeries(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fiboSeries(n-1) + fiboSeries(n-2);
}
main()
{
int num; //Variable to read from user
int numValue; //Variable to store fibonaci number
printf(" Please insert n: ");
scanf("%d",&num); //Reading number from user
numValue = fiboSeries(num); //Calling fibonaci number function
printf("Your Program : %d",numValue); //Print to the user
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.