abs in c++

The abs() function in C++ is used to calculate the absolute value of a given number. It returns the absolute value of the argument passed to it.

Here is the syntax of the abs() function:

int abs(int n);

The abs() function takes a single argument of type int and returns an int value representing the absolute value of the given number.

Note: The abs() function is part of the <cmath> library, so you need to include this header file at the beginning of your program in order to use the abs() function.

Example Usage:

#include <iostream>
#include <cmath>

int main() {
   int num = -5;
   int absNum = abs(num);

   std::cout << "Absolute value of " << num << " is " << absNum << std::endl;

   return 0;
}

In this example, we include the necessary headers <iostream> and <cmath>. We declare an integer variable num and assign it a value of -5. We then use the abs() function to calculate the absolute value of num and store the result in the absNum variable. Finally, we print the result using std::cout.

The output of this program will be:

Absolute value of -5 is 5

This demonstrates how the abs() function can be used to calculate the absolute value of a number in C++.