C++ Math


What Is Math in C++?

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.

Basic Arithmetic Operations in C++

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

Using the C++ Math Library

The <cmath> library provides many built-in functions for mathematical calculations. Let’s look at the most commonly used ones.

1. sqrt() – Square Root

This 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

2. pow() – Power Function

Used to raise a number to a specific power.

Syntax:

pow(base, exponent);

Example:

cout << pow(2, 3);  // 2³ = 8

3. abs() – Absolute Value

The abs() function returns the positive value of any number.

Example:

cout << abs(-15);  // Output: 15

4. round() – Rounds to Nearest Integer

It rounds a floating-point number to the nearest integer.

Example:

cout << round(4.6);  // Output: 5
cout << round(4.3);  // Output: 4

5. 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

6. 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

7. 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

8. Trigonometric Functions

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.)

9. exp() – Exponential Function

The exp() function calculates e raised to a given power.

Example:

cout << exp(1); // Output: 2.71828

10. cbrt() – Cube Root

This function returns the cube root of a number.

Example:

cout << cbrt(27); // Output: 3

Mathematical Constants in C++

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;

Combining Math Functions in a Program

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.

Order of Mathematical Operations

C++ follows operator precedence rules to determine the order in which expressions are evaluated:

  1. Parentheses ()

  2. Exponents (using pow())

  3. Multiplication and Division

  4. Addition and Subtraction

Example:

cout << 10 + 5 * 2; // Output: 20, not 30

Here, multiplication happens before addition.

Summary of C++ Math

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.


Practice Questions

  1. What is the purpose of the <cmath> header in C++?

  2. Which function is used to find the square root of a number in C++?

  3. What is the difference between ceil() and floor() functions?

  4. Which function would you use to find the maximum and minimum between two numbers?

  5. What is the output of pow(2, 4) in C++?

  6. Write a program to calculate the area and circumference of a circle using M_PI.

  7. Write a program that takes two numbers as input and prints their sum, difference, product, and quotient.

  8. Write a program to round a decimal number entered by the user using the round() function.

  9. Write a program to convert an angle from degrees to radians and then display its sine, cosine, and tangent values.

  10. Write a program to find the cube root and logarithm of a number entered by the user.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top