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

Write the following program in c++: Each function in your program should have a

ID: 3801208 • Letter: W

Question

Write the following program in c++: Each function in your program should have a header comment as well. Describe its purpose, the parameters and the return value (if any). Use a format similar to:

// Function name:

// Purpose:

// Parameters:

// Return value

The Greatest Common Divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.

Write a function named calc_gcd that can take two user inputted integers and returns the greatest common divisor of those two integers. If the function is passed an argument that is not greater than Zero, then the function should return the value -1 to indicate that an error occurred.

For example,

cout << calc_gcd (8,12) ; // will print 4

cout << calc_gcd(256,625) ; // will print 1

cout << calc_gcd (0,8) ; // will print -1

cout << calc_gcd (10,-2) ; // will print -1

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.


#include <iostream>
using namespace std;

// function prototype
int calc_gcd_helper(int, int);
int calc_gcd(int , int);


// Driver program to test above function
int main()
{
cout << calc_gcd (8,12)<<endl; // will print 4
cout << calc_gcd(256,625)<<endl; // will print 1
cout << calc_gcd (0,8)<<endl; // will print -1
cout << calc_gcd (10,-2)<<endl; // will print -1
return 0;
}

// Function name: calc_gcd
// Purpose: Function to return gcd of a and b
// Parameters: a, b
// Return value: integre
int calc_gcd(int a, int b)
{
// Error case
if (a <= 0 || b <= 0)
return -1;
return calc_gcd_helper(a, b);
}

// Function name: calc_gcd_helper
// Purpose: Function to return gcd of a and b
// Parameters: a, b
// Return value: integre
int calc_gcd_helper(int a, int b){
if (a == 0)
return b;
return calc_gcd_helper(b%a, a);
}