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

(60-141) Lab Exercises #1 (Due at the end of the lab period July 4th or July 9th

ID: 3911421 • Letter: #

Question

(60-141) Lab Exercises #1 (Due at the end of the lab period July 4th or July 9th, 2018) Objectives: Working with iterative and recursive functions Pre-requisite(s): Read and review chapters 1-5 Code and document the following functions using ITERATIVE and RECURSIVE functions (two implementations for each function: iteratively and recursively) Test the functions by calling them from a simple interactive main0 function using a menu, with different values used to select the choice of function. Overall, you should have one C program (call it firstnameLastname Lab1.c) containing one main) function and 10 other functions, where the functions are called based on the user input to the menu choices. 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 Factorial(0) 1; 1 Factorial(n)n*(n-1)*. . . '2*1 Requirement: n0; reject with error message otherwise Fibonacci(0) 0; Fibonacci(1)1; Fibonacci(n)Fibonacci(n-1)+Fibonacci(n-2): Requirement n0; reject with error message otherwise gcd (x, y,-x, if y:0 3gcd (x, y)-gd (y, x MOD y). ify >0 Requirement:x and y both 0; reject with error message otherwise 4 Power(a.b)- ab Requirement: a> 0, b0, b is an integer, reject with error message otherwise digProduct (x) = rightDigit digProduct ( x/10) Requirement: x is an unsigned integer(> 0); reject with error message otherwise

Explanation / Answer

#include<stdio.h>

//factorial function

int rec_fact(int num){

if(num==0)return 1;

return num*rec_fact(num-1);

}

int iter_fact(int num){

int result=1;

while(num>0){

result*=num;

num--;

}

return result;

}

//Fabonacci Function

int rec_fab(int num){

if(num<=1)return 1;

return rec_fab(num-1)+rec_fab(num-2);

}

int iter_fab(int n)

{

int result, num1 = 0,num2 = 1,i;

if(n==0)result=0;

else if(n==1)result=1;

else

for ( i = 2; i <= n; i++){

result=num2+num1;

num1=num2;

num2=result;

}

return result;

}

//GCD Function

int rec_GCD(int a, int b)

{

if (a == 0 || b == 0)

return 0;

if (a == b)

return a;

if (a > b)

return rec_GCD(a-b, b);

return rec_GCD(a, b-a);

}

int iter_GCD(int a,int b){

if(a==0||b==0)return 0;

while(a!=b){

if(a>b)a-=b;

else b-=a;

}

return a;

}

//Power Function

int rec_power(int x,int y){

if(y==0)return 1;

return x*rec_power(x,y-1);

}

int iter_power(int x,int y){

int result=1;

if(y==0)return 1;

while(y>0){

result*=x;

y--;

}

return result;

}

//Digit product Function

int rec_dig(int num){

if(num==0)return 0;

if(num<10)return num;

return (num%10)*rec_dig(num/10);

}

int iter_dig(int num){

if(num==0)return 0;

int result=1;

while(num){

result*=(num%10);

num/=10;

}

return result;

}

//Main Function

int main(){

int choice,num1,num2,result,more;

do{

printf("enter choice 1)factorial 2)fabonacci 3)GCD 4)power 5)Digit product choice: ");

scanf("%d",&choice);

switch(choice){

case 1:{

printf("enter number:");

scanf("%d",&num1);

result=rec_fact(num1);

printf("facrorial of %d using recursive factorial function is result %d ",num1,result);

result=iter_fact(num1);

printf("facrorial of %d using iterative factorial function is result %d ",num1,result);

break;

}

case 2:{

printf("enter number:");

scanf("%d",&num1);

result=rec_fab(num1);

printf("fabonacci of %d using recursive fabonacci function is result %d ",num1,result);

result=iter_fab(num1);

printf("fabonacci of %d using iterative fabonacci function is result %d ",num1,result);

break;

}

case 3:{

printf("enter two number:");

scanf("%d%d",&num1,&num2);

result=rec_GCD(num1,num2);

printf("GCD of %d and %d using recursive GCD function is result %d ",num1,num2,result);

result=iter_GCD(num1,num2);

printf("GCD of %d and %d using iterative GCD function is result %d ",num1,num2,result);

break;

}

case 4:{

printf("enter two number:");

scanf("%d%d",&num1,&num2);

result=rec_power(num1,num2);

printf("power of %d and %d using recursive power function is result %d ",num1,num2,result);

result=iter_power(num1,num2);

printf("power of %d and %d using iterative power function is result %d ",num1,num2,result);

break;

}

case 5:{

printf("enter number:");

scanf("%d",&num1);

result=rec_dig(num1);

printf("digit product of %d using recursive digit_product fuction is result %d ",num1,result);

result=iter_dig(num1);

printf("digit product of %d using iterative digit_product function is result %d ",num1,result);

break;

}

}

printf("for more press 1 else press 0 ");

scanf("%d",&more);

}while(more==1);

return 0;

}