Home  >  Article  >  Backend Development  >  C++ program to get the size of a given number

C++ program to get the size of a given number

WBOY
WBOYforward
2023-08-29 12:29:121029browse

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.

Simple method

We can write a program ourselves to find the size of a given real number. this Examples are explained below.

grammar

int value;
if (value < 0) {
   value = (-1) * value;
}

algorithm

  • Get input in a numeric variable (integer/double).
  • If number
    • Number := number * (-1)
  • otherwise,
    • Return the number as is.

Example

#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;
}

Output

The number is: -19
The magnitude value of the number is: 19

Use abs() function

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.

grammar

double value;
double absValue = abs(value);

algorithm

  • 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.

Example

#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;
}

Output

The number is: -197.325
The magnitude value of the number is: 197.325

in conclusion

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete