Home > Backend Development > C++ > body text

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

WBOY
Release: 2023-08-29 12:29:12
forward
1028 people have browsed it

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;
}
Copy after login

algorithm

  • Get input in a numeric variable (integer/double).
  • If number < 0, then< 0,则
    • 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;
}
Copy after login

Output

The number is: -19
The magnitude value of the number is: 19
Copy after login

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);
Copy after login

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;
}
Copy after login

Output

The number is: -197.325
The magnitude value of the number is: 197.325
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template