Given two numbers a and b. Define them to be the middle geometry
// Function to calculate the Geometric Mean of two numbers
double geometricMean(double a, double b) {
if (a < 0 || b < 0) {
std::cerr << “Geometric Mean is not defined for negative numbers.\n”;
return -1; // Return error code
}
return sqrt(a * b);
}
int main() {
double a, b;
// Input two numbers
std::cout << "Enter the first number (a): ";
std::cin >> a;
std::cout << "Enter the second number (b): ";
std::cin >> b;
// Calculate and display the geometric mean
double result = geometricMean(a, b);
if (result != -1) { // Check for valid result
std::cout << "The Geometric Mean of " << a << " and " << b << " is: " << result << std::endl;
}
return 0;
}