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

PLease help programming in C The first thing you\'ll do is prompt the user using

ID: 3820552 • Letter: P

Question

PLease help programming in C

The first thing you'll do is prompt the user using this prompt: [C]alculate series, calculate [F]ib(n)>

If 'C' or 'c' is selected, print the following prompt:

Enter n to print the Fibonacci series up to fib(n)>

If 'F' or 'f' is selected, print the following prompt:

Enter n for fib(n)>

The function long long int fib(int n) will calculate the Fibonacci number for n RECURSIVELY, then print the following: fib(4) = 3

The function void fibseries(int n) will print the Fibonacci sequence through n in the following format:The fib(4) series is: 0 1 1 2 3

There is no specific requirement for how you code the second function (iteratively or recursively; it's your choice). When compiling, use the -ftrapv flag to ensure you trap any overflows. You should test enough values to ensure that your algorithms work properly.

Explanation / Answer

#include <stdio.h>

long long int fib(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return fib(n-1) + fib(n-2);
}
}

void fibseries(int n)
{
int i;
for(i = 1; i < n; i++)
{
printf("%lld ", fib(i));
}
printf(" ");
}

int main()
{
char choice;
printf("[C]alculate series, calculate [F]ib(n)> ");
scanf("%c", &choice);
  
int n;
if (choice == 'C' || choice == 'c')
{
printf("Enter n to print the Fibonacci series up to fib(n)> ");
scanf("%d", &n);
fibseries(n);
}
else if(choice == 'F' || choice == 'f')
{
printf("Enter n for fib(n)> ");
scanf("%d", &n);
printf("%lld ", fib(n));
}
  
return 0;
}

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