C format in notepad or codeblocks Write a function Absolute
C++ format in notepad or codeblocks
Write a function Absolute Value (x) that returns the absolute value of x. (The absolute value is the magnitude of x with no sign. Example: the absolute value of -5 is 5 and the absolute value of 7.38 is 7.38). The function and its argument should both have a type float. Write a small program to demonstrate your function Write your own function. Do NOT #include or . Do not use the standard library function abs (). All input and output should be in main () not in the function.Solution
#include <iostream>
using namespace std;
float AbsoluteValue(float x){
if(x < 0){
x = -x;
}
return x;
}
int main()
{
float x;
cout << \"Enter the value: \";
cin >> x;
float result = AbsoluteValue(x);
cout<<\"Absolute value is \"<<result<<endl;
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the value: -5
Absolute value is 5
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the value: 7.38
Absolute value is 7.38
