Home > Article > Backend Development > C++ program to get the size of a given number
The magnitude of a given number means the difference between that particular number and zero. It can also represent the size of a mathematical object relative to other objects within the mathematical object Same species. We're going to follow the first definition here, and the size or absolute value Numbers are represented as |x|, where x is a real number. We explore ways to show The absolute value or magnitude of a given real number.
We can write a program ourselves to find the size of a given real number. this Examples are explained below.
int value; if (value < 0) { value = (-1) * value; }
#include <iostream> using namespace std; // finds the magnitude value of a given number int solve(int value){ // if smaller than zero, then multiply by -1; otherwise return the number itself if (value < 0) { value = (-1) * value; } // return the magnitude value return value; } int main(){ int n = -19; //display the number and its magnitude cout << "The number is: " << n << endl; cout << "The magnitude value of the number is: " << solve(n) << endl; return 0; }
The number is: -19 The magnitude value of the number is: 19
abs() function returns the absolute value or magnitude of a given number. it All numeric types are supported and can be used in the C STL.
double value; double absValue = abs(value);
Get the numeric input in a variable named value. (name can be anything)
Use the abs() function to convert to the absolute/magnitude value of a given variable.
Display/use magnitude values.
#include <iostream> using namespace std; // finds the magnitude value of a given number double solve(double value){ // return the magnitude value using the abs function return abs(value); } int main(){ double n = -197.325; //display the number and its magnitude cout << "The number is: " << n << endl; cout << "The magnitude value of the number is: " << solve(n) << endl; return 0; }
The number is: -197.325 The magnitude value of the number is: 197.325
Various mathematical operations are required to determine the magnitude value. because Among them, we have to design methods to find the magnitude value and learn Outputs the same various built-in functions. In the given article we have discussed Both methods are suitable for C programming language.
The above is the detailed content of C++ program to get the size of a given number. For more information, please follow other related articles on the PHP Chinese website!