Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

code : #include /***************************************************************** * gcd - finds greatest common divisor between two positive integers * * restrictions - Both parameters must be positive. *****************************************************************/

image text in transcribed

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 #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)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago