Question
code : #include /***************************************************************** * gcd - finds greatest common divisor between two positive integers * * restrictions - Both parameters must be positive. *****************************************************************/
code :
#include
/*****************************************************************
* gcd - finds greatest common divisor between two positive integers
*
* restrictions - Both parameters must be positive.
*****************************************************************/
int gcd(int val1, int val2){
int modVal = val1 % val2;
if (modVal == 0) {
return val2;
} else {
int result = gcd(val2, modVal);
return result;
}
}
/*****************************************************************
* main - main program to exercise 'gcd'
*****************************************************************/
int main(int argc, char *argv[]){
int num1, num2;
printf("Please type two positive numbers: ");
scanf("%d %d", &num1, &num2);
if(num1
printf("Invalid input. Numbers must be positive.");
return 0;
}
printf("Their gcd is %d. ", gcd(num1, num2));
return 0;
}
1. (10 pts) Examine the following code for calculating the greatest common divisor. The code is implemented recursively #includeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started