code : #include <stdio.h> /*****************************************************
ID: 3754777 • Letter: C
Question
code :
#include <stdio.h>
/*****************************************************************
* 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 <= 0 || num2 <= 0){
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 #include * gcd - finds greatest common divisor between two positive integers * restrictions - Both parameters must be positive. int gcd (int vall, int val2) [ int modal va11 % va 12; if (modVal0) return val2; else t int result-gcd (val2, modVal); return result; * main - main program to exercise 'gcd' int main (int argc, char argv[]) int numl, num2: printf ("Please type two positive numbers: ") scanf ("%d %d", &num1, &num2); printf ("Invalid input. Numbers must be positive."); return 0; printf ("Their return 0; gcd is %d" ", gcd(num1, num2 )); Assume that the user types 12 and 80 for the code below. Trace the execution of each recursive call by stating the arguments of each invocation of the gcd function. gcd (12, 80) gcd , ) // continue completing the list of recursive calls // until the program terminates 2. (3 pts) What value is returned by gcd(12, 80)?Explanation / Answer
Tracing execution:
gcd(12,80)
val1 = 12
val2 =80
modval = val1 % val2 = 12%80 = 12
if condition fails since modVal is not 0
in else
//a recusive call
----gcd(val2,modVal)//modVal=12,val2 =12
----gcd(80,12))
----val1 = 80
----val2 =12
----modval = val1 % val2 = 80%12 = 8
----if condition fails since modVal is not 0
----in else
----//a recusive call
--------gcd(val2,modVal)//modVal=8,val2 =12
--------gcd(12,8))
--------val1 =12
--------val2 =8
--------modval = val1 % val2 = 12%8 = 4
--------if condition fails since modVal is not 0
--------in else
--------//a recusive call
------------gcd(val2,modVal)//modVal=4,val2 =8
------------gcd(8,4))
------------val1 =8
------------val2 =4
------------modval = val1 % val2 = 8%4 = 0
------------if condition staisfied and because modval is zero
------------val2 which is 4 is returned recursively to the main
//the final value returned is 4
//PLS give a thumbs up if you find this helpful, its helps me alot, thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.