-
Hajipur, Bihar, 844101
Mathematical operations are an essential part of programming. In C++, you can perform arithmetic using operators like +, -, *, /, and %. However, for more advanced calculations like square roots, powers, or trigonometric functions, you can use the math library provided by C++.
To use these math functions, you must include the header at the beginning of your program:
#include <cmath>
This library provides a wide range of built-in functions to perform different types of mathematical computations easily.
C++ supports all basic arithmetic operations that you find in regular math.
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
cout << "Addition: " << a + b << endl;
cout << "Subtraction: " << a - b << endl;
cout << "Multiplication: " << a * b << endl;
cout << "Division: " << a / b << endl;
cout << "Modulus: " << a % b << endl;
return 0;
}
Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
The <cmath> library provides many built-in functions for mathematical calculations. Let’s look at the most commonly used ones.
sqrt() – Square RootThis function returns the square root of a number.
Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num = 25.0;
cout << "Square root of 25: " << sqrt(num);
return 0;
}
Output:
Square root of 25: 5
pow() – Power FunctionUsed to raise a number to a specific power.
Syntax:
pow(base, exponent);
Example:
cout << pow(2, 3); // 2³ = 8
abs() – Absolute ValueThe abs() function returns the positive value of any number.
Example:
cout << abs(-15); // Output: 15
round() – Rounds to Nearest IntegerIt rounds a floating-point number to the nearest integer.
Example:
cout << round(4.6); // Output: 5
cout << round(4.3); // Output: 4
ceil() and floor()ceil() rounds a number up to the nearest integer.
floor() rounds a number down to the nearest integer.
Example:
cout << ceil(4.1); // Output: 5
cout << floor(4.9); // Output: 4
fmax() and fmin()These functions return the larger or smaller of two numbers.
Example:
cout << fmax(10, 20); // Output: 20
cout << fmin(10, 20); // Output: 10
log() and log10()log() gives the natural logarithm (base e).
log10() gives the base-10 logarithm.
Example:
cout << log(2.71828); // Output: 1 (approx)
cout << log10(100); // Output: 2
C++ math library also supports trigonometric functions like sin(), cos(), and tan().
These functions take input in radians, not degrees. To convert degrees to radians, multiply by (π / 180).
Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double angle = 90.0;
double radians = angle * M_PI / 180.0;
cout << "sin(90°): " << sin(radians) << endl;
cout << "cos(90°): " << cos(radians) << endl;
cout << "tan(90°): " << tan(radians) << endl;
return 0;
}
Output:
sin(90°): 1
cos(90°): 6.12323e-17
tan(90°): 1.63312e+16
(Note: Some results appear as very small or large numbers due to floating-point precision.)
exp() – Exponential FunctionThe exp() function calculates e raised to a given power.
Example:
cout << exp(1); // Output: 2.71828
cbrt() – Cube RootThis function returns the cube root of a number.
Example:
cout << cbrt(27); // Output: 3
If you include <cmath>, you also get access to useful constants like:
M_PI → π (3.14159)
M_E → e (2.71828)
Example:
cout << "Value of PI: " << M_PI << endl;
cout << "Value of e: " << M_E << endl;
Here’s a small example that uses multiple math functions together.
Example:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double radius = 7.0;
double area = M_PI * pow(radius, 2);
double circumference = 2 * M_PI * radius;
cout << "Area: " << round(area) << endl;
cout << "Circumference: " << round(circumference) << endl;
return 0;
}
Output:
Area: 154
Circumference: 44
This program calculates the area and circumference of a circle using pow(), constants like M_PI, and rounding functions.
C++ follows operator precedence rules to determine the order in which expressions are evaluated:
Parentheses ()
Exponents (using pow())
Multiplication and Division
Addition and Subtraction
Example:
cout << 10 + 5 * 2; // Output: 20, not 30
Here, multiplication happens before addition.
C++ provides both simple arithmetic operators and advanced mathematical functions through the <cmath> library. You can calculate roots, powers, logarithms, and trigonometric values efficiently. By mastering these math operations, you can build programs that handle financial calculations, geometry problems, physics simulations, and more.
What is the purpose of the <cmath> header in C++?
Which function is used to find the square root of a number in C++?
What is the difference between ceil() and floor() functions?
Which function would you use to find the maximum and minimum between two numbers?
What is the output of pow(2, 4) in C++?
Write a program to calculate the area and circumference of a circle using M_PI.
Write a program that takes two numbers as input and prints their sum, difference, product, and quotient.
Write a program to round a decimal number entered by the user using the round() function.
Write a program to convert an angle from degrees to radians and then display its sine, cosine, and tangent values.
Write a program to find the cube root and logarithm of a number entered by the user.