Problem: A Simple Program To Find The Absolute Value Of An Integer.
Method 1:
#include <iostream>
using namespace std;
int main() {
int number;
int abs_number;
cout << "Enter an integer
(positive or negative): " << endl;
cin >> number;
// Find the absolute value
if(number >= 0) {
abs_number = number;
}
else
abs_number = -number;
// Print out output
cout << "The absolute value
of " << number << " is " << abs_number;
cout << endl;
return 0;
}
Output:
Enter an integer (positive or negative):
-11
The absolute value of -11 is 11
#include <iostream>
using namespace std;
int Abs(int i)
{
if( i >= 0)
return i;
else
return -i;
}
int main() {
int number;
int abs_number;
cout << "Enter an integer
(positive or negative): " << endl;
cin >> number;
// calling the function Abs()
abs_number = Abs(number);
cout << "The absolute value
of " << number << " is " << abs_number;
cout << endl;
return 0;
}
Output:
Enter an integer (positive or negative):
-17
The absolute value of -17 is 17