Number in C ++
Typically, when we work with Number (numeric types), we use the original data types such as int, short, long, float and double, . Numeric data types, possible values and their range of values, discussed in the Data Type chapter in C ++.
Defining Number in C ++
You have seen the definition of numbers in various examples in the previous chapters. Here is a general example to define diverse numeric types in C ++:
#include using namespace std ; int main () { // phan dinh nghia cac so: short s ; int i ; long l ; float f ; double d ; // phep gan cho cac so; s = 10 ; i = 1000 ; l = 1000000 ; f = 230.47 ; d = 30949.374 ; // in cac so; cout << "short s la: " << s << endl ; cout << "int i la: " << i << endl ; cout << "long l la: " << l << endl ; cout << "float f la: " << f << endl ; cout << "double d la: " << d << endl ; return 0 ; }
When the above code is compiled and executed, it gives the following result:
short s la : 10 int i la : 1000 long l la : 1000000 float f la : 230.47 double d la : 30949.4
Math function in C ++
Besides the various functions you can create, C ++ also includes some useful math functions for you to use. These functions are available in standard C and C ++ libraries, and are called built-in functions. These are functions that can be included in your program and then used.
C ++ has a diverse set of mathematical functions that can be implemented on different types of numbers. The table below lists some of the useful math functions available in C ++.
To use these functions, you need to include the header file .
STTHàm & Purpose1 double cos (double);This function returns the cosine of an angle (form a double)
2 double sin (double);This function returns the sine of an angle (form a double)
3 double tan (double);This function returns the tangent of an angle (form a double)
4 double log (double);This function returns the natural logarithm (ln) of that number
5 double pow (double, double);The exponential function with the base is the first double number and the exponent is the second double
6 double hypot (double, double);If you pass the length of the two sides of the triangle (ie, the double numbers), it will return the length of the hypotenuse
7 double sqrt (double);Returns the square root of the double
8 int abs (int);Returns the absolute value of int
9 double fabs (double);Returns the absolute value of any double
10 double floor (double);Find the integer number that is less than or equal to the parameter passed to it
The following example illustrates some math functions in C ++:
#include #include using namespace std ; int main () { // phan dinh nghia cac so: short s = 10 ; int i = - 1000 ; long l = 100000 ; float f = 230.47 ; double d = 200.374 ; // cac hoat dong toan hoc; cout << "sin(d) co gia tri la: " << sin ( d ) << endl ; cout << "abs(i) co gia tri la: " << abs ( i ) << endl ; cout << "floor(d) co gia tri la: " << floor ( d ) << endl ; cout << "sqrt(f) co gia tri la: " << sqrt ( f ) << endl ; cout << "pow( d, 2) co gia tri la: " << pow ( d , 2 ) << endl ; return 0 ; }
Running the above C ++ program will produce the following results:
Random Number in C ++
In some cases, you want to create a random number. There are two functions that can help you do this. The first function is rand () defined in the library: will only return a pseudo random number, which may not change through the program runs. To solve this anomaly, we will use the srand () function in the library.
The following example will generate some random numbers in C ++. For example, use the time () function to get the number of seconds on your System time. The value returned from time is through scrand, you note that the random number is generated before the rand call.
#include #include #include using namespace std ; int main () { int i , j ; srand ( ( unsigned ) time ( NULL ) ); /* tao 10 so ngau nhien. */ for ( i = 0 ; i < 10 ; i ++ ) { j = rand (); cout << " So ngau nhien la : " << j << endl ; } return 0 ; }
Running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Function in C / C ++
Next post: Array (Array) in C / C ++
You should read it
- PERMUT function - The function returns the number of permutations of a given number of objects in Excel
- Why is the number 0 on the keypad to the right of the number 9?
- The INT (Function rounds a number down to the nearest integer of a number) in Excel
- The round () function in Python
- SIGN functions in SQL Server
- ROUNDUP function in Excel - Usage and examples
- Fees and how to switch networks keep the number of the network
- How to automatically turn off the strange number calling on iPhone
May be interested
- How to Change Your Phone Number on iMessagethis wikihow teaches you how to prompt your iphone's number to appear in imessage if you have a new number, as well as how to select an email address as the point from which to send your messages instead of using your phone number....
- Instructions on how to view SIM / ICCID numbers on Android phoneseveryone knows their mobile phone number, but not many people know the sim number or iccid number of the sim card. this unique sim number (iccid) is usually 20 characters long and is stored inside the sim card memory.
- If you have the number 9 in Numerology, you may possess the following traitsdo you have the number 9 in your personal numerology? you may have some or all of these numerology number 9 traits.
- What is serial number?serial number appears in many places like on goods, on computers. so what does serial number stand for?
- Round a number in Excel (ROUND function)instructions on how to round numbers in excel, help calculate faster depending on how to round the number of digits after and before the comma, there are many number rounded functions in excel. 1. round a number using the round () function - meaning: rounds to the nearest number
- Quiz: Only a genius can find the number 20 in this 'sea' of number 2 in less than 6 secondsthis brain teaser may seem easy at first, but it's actually harder than you think! hidden behind a number 02 is the number 20 waiting to be discovered.
- How to number Excel pages from any numberto number excel pages from any number, users only need to adjust the part number of pages to be imported.
- How to Use a Symbol When You Have a Laptopthere are a number of symbols included with standard character sets on your computer, but which not show on your laptop keyboard. these symbols are accessed through the number pad, but laptops don't always have number pads. in most cases,...
- How to automatically turn off the strange number calling on iPhonethe function of turning off the ringtone when a strange number comes in on iphone will help you know whether the call to your phone is a strange number or the numbers you have saved and contacted recently.
- Is your ID card number, insurance book number and salary required to be secure?so when leaking id number, insurance book number, salary ... what dangers will you face? how we need to be secure. let's find out through the article below.