HOMEWORK 8 The first ten numbers in the well-known and widely-used Fibonacci ser
ID: 2249199 • Letter: H
Question
HOMEWORK 8
The first ten numbers in the well-known and widely-used Fibonacci series are the following integer numbers which are positive or zero:
0,1,1,2,3,5,8,13,21,34,…
This series has two seed or baseline values, namely, 0 and 1. Except for the baseline values, every other number in the series is equal to the sum of the previous two numbers in the series. Design two programs in C using a prototype function which, upon running, will print on the console the first n numbers in the series, with n being user-specified:
PROGRAM A: This program will use a nonrecursive method employing “for” looping
PROGRAM B: This program will use a recursive approach.
Please explain
Explanation / Answer
Answer:- PROGRAM A, non-recursive method:- Here we need to start with value zero, and one then we will add the previous two values and display the added value in a loop till number of value displayed is equal to number enterd by user. The program is written below-
#include<stdio.h> //header file included
void disp_fibo(int); //function prototype with one argument as number of terms
int main(void) //main function starts here
{
int num_term; //variable definition
printf("Enter number of terms: "); //ask user for number of terms
scanf("%d", &num_term); //store the value in the variable num_term
disp_fibo(num_term); //call the function to display the fibonacci series upto num_term terms
return 0;
}
//function definition for function disp_fibo(int n)
void disp_fibo(int num)
{
int temp1=0, temp2=1, temp_next, loop_cnt;
for(loop_cnt = 0, loop_cnt < num; loop_cnt++) //loop starts here
{
printf("%d ", temp1); //display the number
temp_next = temp1 + temp2; //add the two numbers
temp1 = temp2; //to display the added value
temp2 = temp_next; //for next adding value
}
}
OUTPUT:-
Enter number of terms: 10
0 1 1 2 3 5 8 13 21 34
PROGRAM B, recursive method- In this case the function will return value each time it has been called recursively. The code is written below-
#include<stdio.h> //header file included
int disp_fibo(int); //function prototype with one argument as number of terms and return type is inetger
int main(void) //main function starts here
{
int loop_cnt, num_term; //variable definition
printf("Enter number of terms: "); //ask user for number of terms
scanf("%d", &num_term); //store the value in the variable num_term
for(loop_cnt = 0; loop_cnt < num_term; loop_cnt++)
{
printf("%d ", disp_fibo(loop_cnt) ); //call the function to display the fibonacci series upto num_term terms
}
return 0;
}
//function definition for function disp_fibo(int n)
disp disp_fibo(int num)
{
if(num == 0)
return 0;
else if(num == 1)
return 1;
else
return ( disp_fibo(num - 1) + disp_fibo(num - 2) ); //function calling itself, recursion
}
OUTPUT:-
Enter number of terms: 10
0 1 1 2 3 5 8 13 21 34
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.