When dealing with Numbers, we utilize primitive data types like int, short, long, float, and double, among others. To delve into the intricacies of C++ Numbers, follow this C++ tutorial by Mytour.
Exploring C++ Numbers
Article Table of Contents
1. Examples of Numbers in C++
2. Mathematical Functions in C++
3. Random Number Generation in C++
Examples of Numbers in C++
Here is an illustration of Numbers in C++:
#include
using namespace std;
int main () {
// defining a number:
short s;
int i;
long l;
float f;
double d;
// assigning a number;
s is assigned the value 10;
i is assigned the value 1000;
l is assigned the value 1000000;
f is assigned the value 230.47;
d is set to 30949.374;
// Print numbers;
cout << 'short s :' << s << endl;
cout << 'int i :' << i << endl;
cout << 'long l :' << l << endl;
cout << 'float f :'
cout << 'double d :' << d << endl;
return 0;
}
When the above code is compiled and executed, it will return results in a format similar to the following:
Mathematical Functions in C++
In addition to functions we can create, C++ also includes various useful functions available in standard C and C++ libraries, known as built-in functions. Here is a table listing some built-in functions in C++ that you can use:
Note: To use the functions below, we need to include a header file named
Function Description
double cos(double) This function takes and returns the cosine of an angle (double form).
double sin(double) This function takes and returns the sine of an angle (double form).
double tan(double) This function takes and returns the tangent of an angle (double form).
double log(double) This function takes a number and returns the natural logarithm (ln) of that number.
double pow(double, double) The power function takes the first double as the base and the second double as the exponent.
double hypot(double, double) If you pass the lengths of two sides of a triangle (both as double values), it will return the length of the hypotenuse.
double sqrt(double) If you pass a number to this function, it will return the square root of the double value.
int abs(int) This function returns the absolute value of the integer passed to it.
double fabs(double) This function returns the absolute value of the decimal number passed to it.
double floor(double) Find the integer less than or equal to the parameter passed to it.
The example below illustrates some mathematical functions in C++:
#include
#include
using std;
int main () {
// defining numbers:
short s = 10;
int i = -1000;
long l = 100000;
float f = 230.47;
double d = 200.374;
// mathematical functions;
cout << 'sin(d) :' << sin(d) << endl;
cout << 'abs(i) :' << abs(i) << endl;
cout << 'floor(d) :' << floor(d) << endl;
cout << 'sqrt(f) :' << sqrt(f) << endl;
cout << 'pow(d, 2) :' << pow(d, 2) << endl;
return 0;
}
When the above code is compiled and executed, it will return results in a format similar to the following:
Random Number Generation in C++
In many scenarios, there arises a need to generate random numbers. To achieve this in C++, two essential functions are the rand() and srand() functions. The rand() function returns a pseudo-random number. To address this, we first invoke the srand() function.
The following example demonstrates how to create random numbers in C++. In this instance, we utilize the time() function to obtain the system time in seconds and use the rand() function for random selection.
#include
#include
#include
using namespace std;
int main () {
int i, j;
// set up the seconds
srand((unsigned)time(NULL));
/* generate 10 random numbers. */
for (i = 0; i < 10; i++)
// generate actual random number
j = rand();
cout << 'random number := ' << j << '\n';
}
return 0;
}
When the above code is compiled and executed, it will return a result like the following:
Random Number: 1748144778
Random Number: 630873888
Random Number: 2134540646
Random Number: 219404170
Random Number: 902129458
Random Number: 920445370
Random Number: 1319072661
Random Number: 257938873
Random Number: 1256201101
Random Number: 580322989
This article has introduced you to the concept of random numbers in C++. Additionally, readers can explore more articles on Mytour to delve deeper into conditional statements in C++.