C++ Math Functions - Practice Problem Solutions
Basic Arithmetic and Power
1. Write a program to calculate x^y using pow()
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x, y;
cout << "Enter base (x): ";
cin >> x;
cout << "Enter exponent (y): ";
cin >> y;
double result = pow(x, y);
cout << x << " raised to power " << y << " = " << result << endl;
return 0;
}
2. Take a number and print its square root
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num;
cout << "Enter a number: ";
cin >> num;
double squareRoot = sqrt(num);
cout << "Square root of " << num << " = " << squareRoot << endl;
return 0;
}
3. Find the cube root of a number using cbrt()
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num;
cout << "Enter a number: ";
cin >> num;
double cubeRoot = cbrt(num);
cout << "Cube root of " << num << " = " << cubeRoot << endl;
return 0;
}
4. Compute the result of 5^3 + sqrt(81)
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double result = pow(5, 3) + sqrt(81);
cout << "5^3 + sqrt(81) = " << result << endl;
return 0;
}
5. Compare pow(x, 0.5) with sqrt(x) — are the results always the same?
cpp
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double x;
cout << "Enter a number: ";
cin >> x;
double result1 = pow(x, 0.5);
double result2 = sqrt(x);
cout << fixed << setprecision(15);
cout << "pow(" << x << ", 0.5) = " << result1 << endl;
cout << "sqrt(" << x << ") = " << result2 << endl;
if (result1 == result2) {
cout << "The results are exactly the same." << endl;
} else {
cout << "The results differ slightly due to floating-point precision." << endl;
cout << "Difference: " << abs(result1 - result2) << endl;
}
return 0;
}
Trigonometric Functions
6. Write a program to calculate the cosine and tangent of a given angle
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double angle_degrees;
cout << "Enter angle in degrees: ";
cin >> angle_degrees;
// Convert degrees to radians
double angle_radians = angle_degrees * M_PI / 180.0;
double cosine_value = cos(angle_radians);
double tangent_value = tan(angle_radians);
cout << "Cosine of " << angle_degrees << " degrees = " << cosine_value << endl;
cout << "Tangent of " << angle_degrees << " degrees = " << tangent_value << endl;
return 0;
}
7. Find the inverse sine of 0.5 and print the result in degrees
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double value = 0.5;
// Calculate inverse sine in radians
double angle_radians = asin(value);
// Convert to degrees
double angle_degrees = angle_radians * 180.0 / M_PI;
cout << "The inverse sine of " << value << " = " << angle_degrees << " degrees" << endl;
return 0;
}
8. Calculate atan2(y, x) for user input and explain the result
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x, y;
cout << "Enter x coordinate: ";
cin >> x;
cout << "Enter y coordinate: ";
cin >> y;
double angle_radians = atan2(y, x);
double angle_degrees = angle_radians * 180.0 / M_PI;
cout << "atan2(" << y << ", " << x << ") = " << angle_radians << " radians" << endl;
cout << "In degrees: " << angle_degrees << " degrees" << endl;
cout << "\nExplanation:" << endl;
cout << "atan2(y, x) gives the angle in radians between the positive x-axis" << endl;
cout << "and the point (x, y) in the Cartesian plane." << endl;
cout << "It returns values in the range [-π, π]." << endl;
return 0;
}
Logarithmic and Exponential
9. Find the natural logarithm of a number using log()
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num;
cout << "Enter a positive number: ";
cin >> num;
if (num <= 0) {
cout << "Error: Logarithm is defined only for positive numbers!" << endl;
} else {
double result = log(num);
cout << "Natural logarithm of " << num << " = " << result << endl;
}
return 0;
}
10. Find the base-10 logarithm of 100,000
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num = 100000.0;
double result = log10(num);
cout << "Base-10 logarithm of " << num << " = " << result << endl;
return 0;
}
11. Use exp(x) to compute e^x and compare it to pow(2.71828, x)
cpp
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double x;
cout << "Enter a value for x: ";
cin >> x;
double e = 2.71828;
double result1 = exp(x);
double result2 = pow(e, x);
cout << fixed << setprecision(10);
cout << "exp(" << x << ") = " << result1 << endl;
cout << "pow(2.71828, " << x << ") = " << result2 << endl;
cout << "Difference: " << abs(result1 - result2) << endl;
return 0;
}
12. Write a program that solves: Compound Interest = P × exp(r × t)
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double principal, rate, time;
cout << "Enter principal amount (P): ";
cin >> principal;
cout << "Enter annual interest rate (r) in decimal: ";
cin >> rate;
cout << "Enter time in years (t): ";
cin >> time;
double compoundInterest = principal * exp(rate * time);
cout << "Principal: $" << principal << endl;
cout << "Rate: " << (rate * 100) << "%" << endl;
cout << "Time: " << time << " years" << endl;
cout << "Compound Interest (continuous): $" << compoundInterest - principal << endl;
cout << "Final Amount: $" << compoundInterest << endl;
return 0;
}
Rounding and Absolute Value
13. Round a float up using ceil() and down using floor()
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num;
cout << "Enter a floating-point number: ";
cin >> num;
double ceilValue = ceil(num);
double floorValue = floor(num);
cout << "Original number: " << num << endl;
cout << "Rounded up (ceil): " << ceilValue << endl;
cout << "Rounded down (floor): " << floorValue << endl;
return 0;
}
14. Use round() to round multiple floating-point numbers and print the results
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num1, num2, num3;
cout << "Enter three floating-point numbers:" << endl;
cout << "Number 1: ";
cin >> num1;
cout << "Number 2: ";
cin >> num2;
cout << "Number 3: ";
cin >> num3;
cout << "\nResults after rounding:" << endl;
cout << num1 << " rounded = " << round(num1) << endl;
cout << num2 << " rounded = " << round(num2) << endl;
cout << num3 << " rounded = " << round(num3) << endl;
return 0;
}
15. Take a negative number and display its absolute value
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num;
cout << "Enter a negative number: ";
cin >> num;
double absValue = abs(num);
cout << "The absolute value of " << num << " is " << absValue << endl;
return 0;
}
16. Demonstrate the difference between trunc() and floor()
cpp
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double positiveNum, negativeNum;
cout << "Enter a positive decimal number: ";
cin >> positiveNum;
cout << "Enter a negative decimal number: ";
cin >> negativeNum;
cout << "\nFor positive number " << positiveNum << ":" << endl;
cout << "trunc(" << positiveNum << ") = " << trunc(positiveNum) << endl;
cout << "floor(" << positiveNum << ") = " << floor(positiveNum) << endl;
cout << "\nFor negative number " << negativeNum << ":" << endl;
cout << "trunc(" << negativeNum << ") = " << trunc(negativeNum) << endl;
cout << "floor(" << negativeNum << ") = " << floor(negativeNum) << endl;
cout << "\nExplanation:" << endl;
cout << "- trunc() simply removes the decimal part (truncates toward zero)" << endl;
cout << "- floor() rounds down to the nearest integer (toward negative infinity)" << endl;
cout << "For positive numbers, they often give the same result" << endl;
cout << "For negative numbers, they usually give different results" << endl;
return 0;
}