← Back

28. Absolute Value of Integer

Easy

📝 Problem

Given an integer input, print the Absolute value of the input number

Input Format:
Accept an integer as an input.
Output Format:
Positive Integer
Constraints:
-10^5 ≤ input ≤ 10^5
Sample Input 1:
85
Sample Output 1:
85
Sample Input 2:
-5
Sample Output 2:
5
📚
Related Topic
For detailed explanation of abs() function and cmath library, check out Question #21!
🎯 Understanding Absolute Value: Distance from Zero!

What is Absolute Value?

The absolute value of a number is its distance from zero, regardless of direction. Think of it as "making any number positive" - it removes the negative sign if present!

💡 Quick Concept
Absolute value = Distance from zero

• |5| = 5 (positive stays positive)
• |-5| = 5 (negative becomes positive)
• |0| = 0 (zero stays zero)

1️⃣ The abs() Function in C++

Simple and Elegant:

📌 Using abs() - No #include needed for integers!
#include <iostream>
using namespace std;

int main() {
    long long a;
    cin >> a;
    cout << abs(a);  // That's it! ✨
    return 0;
}
⚠️ Important Note
For integer types (int, long, long long), abs() is available in <iostream> automatically! You don't need <cmath>.

However, for floating-point types (float, double), you need #include <cmath> and use fabs() or abs().

2️⃣ Visual Understanding

📌 Number Line Visualization:
// Imagine a number line:

    -5  -4  -3  -2  -1   0   1   2   3   4   5
    ◄────────────────┼────────────────►
                     
    // Distance of -5 from 0 = 5 steps
    // Distance of +5 from 0 = 5 steps
    
    abs(-5) = 5  // Same distance!
    abs(5)  = 5

3️⃣ How abs() Works Internally

The Logic Behind abs():

📌 Conceptual Implementation:
// If you were to implement abs() yourself:

int my_abs(int x) {
    if (x < 0)
        return -x;  // Flip the sign
    else
        return x;   // Keep as is
}

// Examples:
// my_abs(-5) → -(-5) → 5 ✓
// my_abs(5)  → 5 ✓
// my_abs(0)  → 0 ✓
💡 Fun Fact
The actual C++ implementation uses bit manipulation for efficiency! But conceptually, it's just checking if negative and flipping the sign.

4️⃣ Different Types, Different Functions

Data Type Function Header Required Example
int abs() <iostream> abs(-42) → 42
long labs() or abs() <iostream> abs(-1000L) → 1000
long long llabs() or abs() <iostream> abs(-5LL) → 5
float fabs() or abs() <cmath> fabs(-3.14f) → 3.14
double fabs() or abs() <cmath> abs(-2.718) → 2.718

5️⃣ Common Use Cases

Where You'll Use Absolute Values:

  • Distance Calculations: Finding distance between two points
  • Error Margins: Calculating deviation from expected value
  • Sorting: Sorting by magnitude, ignoring sign
  • Game Development: Collision detection, health calculations
  • Physics Simulations: Speed (magnitude of velocity)
  • Statistics: Mean absolute deviation, error analysis
📌 Real-World Example - Temperature Difference:
// Finding temperature change magnitude
int morning_temp = -5;  // -5°C
int evening_temp = 10;  // 10°C

int change = abs(evening_temp - morning_temp);
// change = abs(10 - (-5)) = abs(15) = 15°C increase

// Or if it drops:
int night_temp = 2;
change = abs(night_temp - evening_temp);
// change = abs(2 - 10) = abs(-8) = 8°C decrease

6️⃣ Quick Examples

📌 Testing Different Values:
abs(85) = 85      // Positive stays positive
abs(-5) = 5       // Negative becomes positive
abs(0) = 0        // Zero stays zero
abs(-100000) = 100000  // Large negative
abs(42) = 42      // Already positive
🎓 Key Takeaway: The abs() function is your go-to tool for finding absolute values. It's simple, efficient, and handles all the edge cases for you. For detailed exploration of the cmath library and more mathematical functions, check out Question #21!
💡 Pro Tip
Modern C++ (C++11 and later) overloads abs() for all types, so you can just use abs() instead of remembering labs(), llabs(), or fabs()!
💻 Solution Code
#include <iostream>

using namespace std;

int main() {
    long long a;
    cin >> a;
    cout << abs(a);
    return 0;
}