In: Computer Science
Code and document the following functions using NON-RECURSIVE
ITERATION only.
Test the functions by calling them from a simple interactive main()
function using a menu, with different values used to select the
choice of function. Overall, you should have one C program (call it
Lab1.c) containing one main() function and 5 other functions, where
the functions are called based on an interactive user menu. The
program should contain a loop that permits users to enter a new
choice of function for each loop, until exit from the loop
explicitly.
1
Factorial(0) = 1;
Factorial(n) = n * (n-1) * . . . * 2 * 1
Requirement: n >= 0; reject with error message
otherwise
2
Fibonacci(0) = 0;
Fibonacci(1) = 1;
Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2);
Requirement: n >= 0; reject with error message
otherwise
3
gcd (x, y) = x, if y=0
gcd (x, y) = gcd (y, x MOD y), if y > 0
Requirement: x and y both > 0; reject with error message
otherwise
4
Power(a,b) = ??
Requirement: a > 0, b > 0, b is an integer; reject with
error message otherwise
5
digProduct (x) = x if x <9
digProduct (x) = rightDigit * digProduct ( x/10)
Requirement: x is an unsigned integer (> 0); reject with
error message otherwise
Sample Interaction
Lab 1
1 - int Factorial(int n);
2 - int Fibonacci(int n);
3 - int Gcd(int x, int y);
4 - double Power(int a, int b);
5 – int digProduct (int x);
0 - QUIT
Please enter a selection: 6
Invalid Input.
Lab 1
1 - int Factorial(int n);
2 - int Fibonacci(int n);
3 - int Gcd(int x, int y);
4 - double Power(int a, int b);
5 – int digProduct (int x);
0 - QUIT
Please enter a selection: 1
Enter a positive Integer: 5
Answer: 120
Lab 1   
1 - int Factorial(int n);
2 - int Fibonacci(int n);
3 - int Gcd(int x, int y);
4 - double Power(int a, int b);
5 – int digProduct (int x);
0 - QUIT
Please enter a selection: 4
Enter the first positive Integer: 2
Enter the second positive Integer: 3
Answer: 8
Lab 1
1 - int Factorial(int n);
2 - int Fibonacci(int n);
3 - int Gcd(int x, int y);
4 - double Power(int a, int b);
5 – int digProduct (int x);
0 - QUIT
Please enter a selection: 0
Goodbye!
#include <stdio.h>
// function to calculate factorial of number
int Factorial(int n) {
   // if number is negative
   if(n < 0) {
      printf("Invalid input!!\n\n");
      return -1;
   }
   // calculate factorial
   int fact = 1;
   for(int i = 1; i <= n; i++) {
      fact = fact * i;
   }
   // return the result
   return fact;
}
// function to find the fibonacci number
int Fibonacci(int n) {
   // if n is negative 
   if (n < 0) {
      printf("Invalid input!!\n\n");
      return -1;
   }
   // declaring and initializing variables
   int first = 0;
   int second = 1;
   int next = 1;
   if(n == 0) {
      return first;
   }
   else if(n == 1) {
      return second;
   }
   else{
      // calculting the nth fibonacci number
      for(int i = 2; i <= n; i++) {
         next = first + second;
         first = second;
         second = next;
      }
   }
   // return the result
   return next;
}
// function to calculate the gcd of two number
int gcd(int x, int y) {
   // if numbers are less than 1
   if(x < 1 || y < 1) {
      printf("Invalid input!!\n\n");
      return -1;
   }
   // calculating gcd
   // while both number are not equal
   while(x != y) {
      // if x is greater
      if(x > y){
         x = x - y;
      }
      // if y is greater
        else {
         y = y - x;
      }
    }
   // return the result
   return x;
}
// function to calculate the a to the power b
double Power(int a, int b) {
   // validating input
   if(a < 1 || b < 0) {
      printf("Invalid input!!\n\n");
      return -1;
   }
   double result = 0;
   // if b is 0 which mean a raised to power 0 which is always 1
   if(b == 0) {
      return 1;
   }
   else {
      // calculating the power
      for(int i = 1; i < b; i++) {
         result = result + (a * a);
      }
   }
   // return the result
   return result;
}
// function to calculate the product of the digits of x
int digProduct(int x) {
   // if x is negative
   if(x < 1) {
      printf("Invalid input!!\n\n");
      return -1;
   }
   // if x is a single digit number
   if(x <= 9) {
      return x;
   }
   int result = 1;
   // while x is positive
   while(x > 0) {
      // calcualting product
      result = result * (x % 10);
      // reducing x by a place
      x = x / 10;
   }
   // return the result
   return result;
}
// main function to drive the code
int main(void) {
   // using loop to run the code until user wants to quit
   while(1) {
      // display menu
      printf("Lab 1\n");
      printf("1 - int Factorial(int n);\n");
      printf("2 - int Fibonacci(int n);\n");
      printf("3 - int Gcd(int x, int y);\n");
      printf("4 - double Power(int a, int b);\n");
      printf("5 – int digProduct (int x);\n");
      printf("0 - QUIT\n");
      // read user option
      int option;
      printf("Please enter a selection: ");
      scanf("%d", &option);
      // validate option
      if(option < 0 || option > 5) {
         printf("Invalid Input.\n\n");
         continue;
      }
      // perform the required operation as option selected
      else {
         if(option == 0) {
            printf("Goodbye!");
            break;
         }
         else if(option == 1) {
            int n;
            printf("Enter a positive Integer: ");
            scanf("%d", &n);
            printf("Answer: %d\n\n", Factorial(n));
         }
         else if(option == 2) {
            int n;
            printf("Enter a positive Integer: ");
            scanf("%d", &n);
            printf("Answer: %d\n\n", Fibonacci(n));
         }
         else if(option == 3) {
            int x, y;
            printf("Enter first positive Integer: ");
            scanf("%d", &x);
            printf("Enter second positive Integer: ");
            scanf("%d", &y);
            printf("Answer: %d\n\n", gcd(x, y));
         }
         else if(option == 4) {
            int a, b;
            printf("Enter first positive Integer: ");
            scanf("%d", &a);
            printf("Enter second positive Integer: ");
            scanf("%d", &b);
            printf("Answer: %lf\n\n", Power(a, b));
         }
         else if(option == 5) {
            int x;
            printf("Enter a positive Integer (greater than 0): ");
            scanf("%d", &x);
            printf("Answer: %d\n\n", digProduct(x));
         }
      }
   }
}

FOR HELP PLEASE COMMENT.
THANK YOU.