Write the following program in c Each function in your progr
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
(1) 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 takes two positive integer arguments 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
Solution
#include <iostream> // std:s:cin, std::cout
#include <fstream> // std::ifstream
#include <map>
using namespace std;
//purpose: To find Greatest common divisoin of given two input numbers
//: parameters:Two numbers
//:Return value:This function will return the greatest common divisor of two number
int calc_gcd(int a,int b)
{
if(a<=0 || b<=0)
return -1;
int c;
while ( a != 0 ) {
c = a; a = b%a; b = c;
}
return b;
}
int main () {
cout<<calc_gcd(8,12)<<endl;
cout<<calc_gcd(256,625)<<endl;
cout<<calc_gcd(0,8)<<endl;
cout<<calc_gcd(10,-2)<<endl;
return 0;
}
