Howdy readers, today you will learn **how to write a C++ program to find the absolute value of a number**.

The **absolute value** of a number refers to the distance of a number from the origin of a number line. It is represented as **|a|**, which defines the magnitude of any integer **‘a’**.

The below program prompts the user to enter an integer, then it finds the absolute value of the entered integer using the following methods:

- Using If-Else Statement
- Using abs() Function

So, without any delay, let’s begin this tutorial.

**C++ Program to Find the Absolute Value of a Number**

**C++ Program**

// C++ Program to Find the Absolute Value of a Number #include <iostream> using namespace std; int main(){ int num; // Taking input cout << "Enter a number: "; cin >> num; if (num > 0){ cout << "The absolute value of the entered integer is: " << num; } else { cout << "The absolute value of the entered integer is: " << -(num); } return 0; }

**Output 1**

Enter a number: -15 The absolute value of the entered integer is: 15

**Output 2**

Enter a number: 47 The absolute value of the entered integer is: 47

**Explanation**

int num;

In this program, we have declared an int data type variable named ** num**.

cout << "Enter a number: "; cin >> num;

Then, this program prompts the user to enter an integer. The entered number gets stored in the ** num** named variable.

if (num > 0){ cout << "The absolute value of the entered integer is: " << num; } else { cout << "The absolute value of the entered integer is: " << -(num); }

If the entered number is positive or greater than zero, then we simply print the number as it is.

In other cases, if the entered number is negative, then we print the magnitude of the number by changing the sign. For example: **(-) x (-) = (+)**.

**C++ Program to Find the Absolute Value of a Number Using abs() Function**

**C++ Program**

// C++ Program to Find the Absolute Value of a Number Using abs() Function #include <iostream> #include <cstdlib> using namespace std; int main(){ int num, absolute; // Taking input cout << "Enter an integer: "; cin >> num; absolute = abs(num); // Display result cout << "The absolute value of the entered integer is: " << absolute; return 0; }

**Output 1**

Enter an integer: -27 The absolute value of the entered integer is: 27

**Output 2**

Enter an integer: -72 The absolute value of the entered integer is: 72

**Explanation**

absolute = abs(num);

The **abs()** function returns the absolute value of an integer. This function is defined in the **cstdlib** header file.

**abs(num) = |num|**

**Conclusion**

Today you learned **how to write a C++ program to find the absolute value of a number**.

If you have any questions regarding the tutorial, comment down your queries in the comment section.

Thanks for reading.

Happy Coding!!

**Also Read:**

**C++ Hello World Program****C++ Program to Print Number Entered by User****C++ Program to Generate Multiplication Table****C++ Program to Find the Square Root of a Number****C++ Program to Find Sum and Average of Two Numbers**