C++ Number Definition with Examples

When we work with numbers in C++ programming, we use primitive data types such as int, short, long, float, and double.

The number of data types, their possible values, and number ranges are determined by the programming language we are using.

Let’s first go through the different types of numeric data that we’ll be working with throughout this C++ tutorial before we get started defining numbers.

The following table provides a rundown of the five different numeric data types supported by C++.

TypeKeywordExample
Short Integershort1, 2, 9, 10
Integerint50, 5, 43, 100
Long Integerlong150000, 26000, 12546
Floating Numberfloat12542.56, 56568.21
Doubledouble123547.11, 72998.123
Types of numbers in C++

Defining Numbers in C++

The code below shows how to define a number in different data types:

#include <iostream>
using namespace std;
 
int main () {
   short  ss;
   int    ii;
   long   ll;
   float  ff;
   double dd;
   
   ss = 14;      
   ii = 1998;    
   ll = 500500; 
   ff = 780.56;  
   dd = 72998.123;
   
   cout << "short ss :" << ss << endl;
   cout << "int ii :" << ii << endl;
   cout << "long ll :" << ll << endl;
   cout << "float ff :" << ff << endl;
   cout << "double dd :" << dd << endl;
 
   return 0;
}

To illustrate the following code, we are defining a number and then assigning a value to each number that we have defined while we are within our main function.

After we have finished assigning the data, we will now print our code.

The following result is produced when the code above is compiled and executed:

short ss :14
int ii :1998
long ll :500500
float ff :780.56
double dd :72998.1

Mathematical Operations in C++

C++ also includes some useful built-in functions available in standard C++ libraries.

These functions can be included in your program and used as needed.

C++ has a variety of built-in mathematical functions that can be performed on numbers.

The following table lists some of the more useful ones.

In order to use these functions, you must include the <cmath> math header file.

C++ Number FunctionDescription
double cos(double x);Returns the cosine of an angle of x radians.
double sin(double x);Returns the sine of an angle of x radians.
double tan(double x);Returns the tangent of an angle of x radians.
double log(double x);Returns the natural logarithm of x.
double pow(double x, double y);Returns base raised to the power exponent:
double hypot(double x, double y);Returns the hypotenuse of a right-angled triangle whose legs are x and y.
double sqrt(double x);Returns the square root of x.
int abs(int x);Returns the absolute value of x.
double fabs(double x);Returns the absolute value of x.
double floor(double x);Rounds x downward, returning the largest integral value that is not greater than x.
C++ Number Functions

If you want the full list of <cmath> functions you check out the cplusplus reference about the C++ numerics library.

Here’s a simple example that shows how a few of the math operations work:

#include <iostream>
#include <cmath>
using namespace std;

int main () {
   short  ss;
   int    ii;
   long   ll;
   float  ff;
   double dd;

   ss = 14;
   ii = -1998;
   ll = 500500;
   ff = 780.56;
   dd = 72998.123;

   cout << "sin(dd) :" << sin(dd) << endl;
   cout << "abs(ii)  :" << abs(ii) << endl;
   cout << "floor(dd) :" << floor(dd) << endl;
   cout << "sqrt(ff) :" << sqrt(ff) << endl;
   cout << "pow(dd, 2) :" << pow(dd, 2) << endl;

   return 0;
}

We are defining a number and assigning a value to it within our main function.

We will then print our code which will include mathematical operations.

The following result is produced when the code above is compiled and executed:

sin(dd) :0.0760278
abs(ii)  :1998
floor(dd) :72998
sqrt(ff) :27.9385
pow(dd, 2) :5.32873e+09

Random Numbers in C++

For generating random numbers, you should be familiar with the rand() and srand() methods.

The rand() function will only return a “pseudo-random” integer; hence, you will need to execute srand() before rand().

To generate random numbers, you can use the time() function to get the current number of seconds, then feed that into the rand() function.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main () {
   int x,y;

   srand((unsigned)time(NULL));

   for( x = 0; x < 10; x++ ) {
      y = rand();
      cout <<"Random Number: " << y << endl;
   }

   return 0;
}

To illustrate the following code, we set the seed used by rand() to generate a pseudo-number.

Next, a for loop is created to generate 10 random numbers. After completing everything, we will now print our code.

The following result is produced when the code above is compiled and executed:

Random Number: 28111
Random Number: 14926
Random Number: 5581
Random Number: 26385
Random Number: 5101
Random Number: 1935
Random Number: 27005
Random Number: 19176
Random Number: 6135
Random Number: 2053

Summary

This lesson went over how to define a number, the mathematical operators in C++, and how to make a random number using rand() and srand().

I hope you learned how to use numbers in C++ properly. If you missed any of our previous lessons, check out our list of C++ Programming Tutorials for Beginners.

Inquiries

If you have any questions or suggestions regarding the C++ Numbers, please do not hesitate to contact me.

Simply leave a comment on this page or send me a message using the contact page of our website.

Leave a Comment