Updated July 1, 2023
Introduction to C++ absolute value
The C++ absolute is one of the default functions. The abs() will return the integer number values because it needs the round-off value. so the absolute value is the integer data type. This function is defined by using the <cstdlib> header files, and it also overloaded the <cmath> directives. It does not accept floating point values. It returns the absolute values the data types are cast, and it is compatible with the other data types’ values.
Syntax:
In C++, each object, variables, keywords, and function have its own syntax and attributes for declaring in the programming codes. Based on the requirements, we will utilize the programming libraries’ special keywords, variables, data types, and functions. The abs() is one of the functions from the math library, and it is used to calculate the absolute values of the input numbers.
Code:
#include<iostream>
#include<cmath>
using namespace std;
data type main()
{
data type variable name;
abs(variable name);
----some C++ code logics---
}
The above codes are the basic syntax for calculating the absolute values of the user inputs. This function does not throw the Exceptions.
How absolute value works in C++?
C++ has n number of reserved keywords and functions that will provide some level of abstraction from the actual namespaces and the new libraries, which are already used for the programmers to allow it with more focus on the coding concepts. It also makes it easier to write the programming codes and clean them up using the same methods like destroy() or any other default methods belonging to the garbage collections, and it’s the main area for destroying the unwanted codes and cleaning up the memory space areas.
According to the abs() function, it will calculate the user inputs to the absolute values; it may be any data type range. The abs() function will take and pass as the single level of parameters, returning the int, long int, or long int. The valid integers will return the valid integers numbers else. The sequence does not occur at that time either the data type has empty or null values; it must contain only the whitespace characters. Also, the calculated integer value is out of range, which means the represented value is in the int data type, and it causes some undefined behavior and throws the exceptions.
Examples
Lets us discuss examples of C++ absolute value.
Example #1
Code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool demo(char a)
{
return (a >= '0' && a <= '9')
? true
: false;
}
int demo1(char* s)
{
if (*s == '\0')
return 0;
int b = 0;
int s1 = 1;
int p = 0;
if (s[0] == '-') {
s1 = -1;
p++;
}
for (; s[p] != '\0'; ++p) {
if (demo(s[p]) == false)
return 0;
b = b * 10 + s[p] - '0';
}
return s1 * b;
}
int main()
{
char c[] = "-987654";
int output = demo1(c);
printf("%d ", output);
int m = -3;
long n = -87645;
int l = abs(m);
long w = abs(n);
cout << "abs(" << m << ") = |" << m << "| = " << l << endl;
cout << "abs(" << n << ") = |" << n << "| = " << w << endl;
return 0;
}
Output:
Example #2
Code:
#include <iostream>
using namespace std;
struct demo {
virtual void one(int) { std::cout << "demo::one\n"; }
void two(char) { std::cout << "demo::two\n"; }
void three(int) { std::cout << "demo::three\n"; }
protected:
int a;
typedef int val;
};
struct demo1 : demo {
using demo::a;
using demo::val;
using demo::one;
void one(int) { std::cout << "demo1::one\n"; }
using demo::two;
void two(int) { std::cout << "demo1::two\n"; }
using demo::three;
void three(int) { std::cout << "demo1::three\n"; }
};
int main()
{
demo1 i;
demo& d = i;
int m;
i.a = 3;
i.one(3);
i.one(3);
i.two(2);
i.two('k');
i.three(3);
i.three(3);
cout<<"Please enter your number:";
cin>>m;
if(m>=0)
cout<<"Welcome User the absolute value of your input is:"<<m;
else
cout<<"Thank you User your absolute value of the input is"<<-(m);
return 0;
}
Output:
Example #3
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float m;
m = -67;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -676.5645;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = 7665.2456;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.67832;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.87892;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -6767.25245;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.6527;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
return 0;
}
Output:
Conclusion
The absolute() function is one of the most important functions to calculate the absolute set of user input values, and it will return the same as the outputs. It supports all types of integer data type values. If the input is in some other data types, it is also performed and converted into integer values.
Recommended Articles
This is a guide to C++ absolute value. Here we discuss the introduction and How C++ absolute value works in C++? along with a few examples respectively. You may also have a look at the following articles to learn more –