C++ abs()

Anonymous contributor's avatar
Anonymous contributor
Published Jun 18, 2021Updated Jul 20, 2023
Contribute to Docs

The abs() function returns the absolute value of the argument.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours

Syntax

abs(n)

Example 1

Use abs() to return the absolute value of -6.5:

#include <iostream>
#include <cmath>
int main() {
std::cout << std::abs(-6.5) << "\n";
// Output: 6.5
}

Codebyte Example

The following example is runnable and outputs the absolute value of -5.1:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
    • Beginner Friendly.
      11 hours