Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The first ten numbers in the well-known and widely-used Fibonacci series are the

ID: 3686560 • Letter: T

Question

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.

USING MICROSOFT VISUAL C++

Explanation / Answer

#include<stdio.h>

void fibIter(int n);
int fibonacciRec(int n);

int main ()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);

printf("Iterative call ");
fibIter(n);

printf(" Recursive Call ");
int i;
for(i=1; i<=n; i++){
       printf("%d ",fibonacciRec(i));
   }
return 0;
}

// iterative fibonacci number generation
void fibIter(int n)
{
int f1 = 0, f2 = 1;

if(n==0){
       printf("%d ", f1);
       return;
   }
if(n==1){
       printf("%d ", f2);
       return;
   }

// otherwise print using for loop
printf("%d %d ", f1,f2);
int c, i;
for (i = 3; i <= n; i++)
{
     c = f1 + f2;
     f1 = f2;
     f2 = c;
     printf("%d ", c);
}
}

// recursive function defination
int fibonacciRec(int n)
{
    if (n==1)
    {
        return 0;
    }
    else if (n==2)
    {
        return 1;
    }
    else
    {
        return (fibonacciRec(n-1)+fibonacciRec(n-2));
    }
}


/*

Sample run:

Enter a number: 9
Iterative call
0 1 1 2 3 5 8 13 21
Recursive Call
0 1 1 2 3 5 8 13 21

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote