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

Create a main program that will use the three functions above to accomplish the

ID: 3601015 • Letter: C

Question

Create a main program that will use the three functions above to accomplish the following:
            a. Prompt the user for a value for n between 1 and 25 (asking again if not in range)

            b. compute and print the nth Fibonacci number and display

            c. determine if nth fibonacci number is prime and display appropriate message

Be sure to use appropriate include files and other items needed for a complete program. For this purpose you can assume that the functions created above are included in the same C file as main and are defined above main so that prototypes are not needed.

Explanation / Answer

#include <stdio.h>

int inputValue()

{

int n;

printf("Enter a value between 1 and 25 : ");

scanf("%d",&n);

if(n<1 || n>25)

{

printf(" The value of n should be in the range 1 to 25. Enter again : ");

scanf("%d",&n);

}

return n;

}

void fibonacci(int n)

{

int x0 = 0, x1 = 1, x2, i; //first term is x0 and second term is 1

if( n == 0)

printf(" %d", x0);

for (i = 3; i <= n; i++) //starting from 3rd term to nth term compute fibonacci term

{

x2 = x0 + x1;

x0 = x1;

x1 = x2;

}

printf("%d",x1); //return the nth term in fibonacci series

}

int isPrime(int n)

{

int i,flag = 1;

for(i=2;i<n/2;i++) //if n is divisible by any number from 2 to that number/2

{

if(n%i == 0)

flag = 0; // not prime if divisible

}

return flag;

}

int main(void) {

int n = inputValue();

printf(" The %d fibonacci number is ",n);

fibonacci(n);

if(isPrime(n))

printf(" The %dth fibonacci number is prime",n);

else

printf(" The %dth fibonacci number is not prime",n);

return 0;

}

Output:

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