Numbers in C++

    In C++ we have various primitive data types to represent the numeric, such as int, short, long, float, and double. All the primitive data types which can store digits are known as numeric data types, and the rage, size and possible value of these data types vary from each other.

    Defining Numbers in C++

    In the C++ data types, we have covered all the basic and primitive data types of C++ which also includes, numeric type. Here we have provided another example for the numeric data types. Example:

    #include <iostream>
    using namespace std;
    
    int main()
        {              // defining Numeric data types in C++
    
           short sh;
           int integer;
           long lng;
           float flt;
           double dbl;
             
           sh = 12;
           integer = 1200;
           lng = 1200000;
           flt = 12000.12;
           dbl = 12000.12;         
    
           cout<<"Short is: "<<sh<<endl;
           cout<<"Integer is: "<<integer<<endl;
           cout<<"Long is: "<<lng<<endl;
           cout<<"Float is: "<<flt<<endl;
           cout<<"Double is: "<<dbl<<endl;                              
    
           return 0;    
        }

    Output:

    Short is: 12
    Integer is: 1200
    Long is: 1200000
    Float is: 12000.12
    Double is: 12000.12

    Math Operations on Numeric Data Types

    Apart from the arithmetic operations on numeric data types, in C++ we have a C++ library known as math which can perform operations on numeric data types. This library math contains many built-in functions and using these functions we can perform various operations on our numeric data types. To use the built-in function of C++ math library we need to import it in our program by writing the header file name <#include math.h>

    Math functions Description
    cos(angle) This function takes an angle value as an argument and returns the cosine value of that angle.
    sin(angle) This function takes an angle value as an argument and returns the sine value of that angle.
    tan(angle) This function takes an angle value as an argument and returns the tangent value of that angle.
    log(value) This function takes a number and returns its natural log value.
    pow(value , raise) It takes two arguments, first is the value and second is the power up to you wish to raise the value.
    hypot(base, perpendicular) It takes two arguments, as base and perpendicular length of the triangle and returns the hypotonus value of the triangle.
    sqrt(value) It returns the square root of the value
    abs(value) It returns the absolute value of any integer number.
    fabs(value) It returns the absolute value of any float numbers.
    floor(value) Finds the integer which is less than or equal to the argument passed to it.

    Example Use <#include math.h> header file to use its functions.

    #include <iostream>
    #include<math.h>
    using namespace std;
    
    int main()
      {            
    
          int value, angle, neg_value, base, perpendicular;
    
          neg_value = -30;
          value =100;
          angle = 90;
          base=3;
          perpendicular=4;
    
          cout<<"cos function: "<<cos(angle)<<endl;
          cout<<"sin function: "<<sin(angle)<<endl;
          cout<<"tan function: "<<tan(angle)<<endl;
          cout<<"log function: "<<log(value)<<endl;
          cout<<"power function: "<<pow(value,2)<<endl;
          cout<<"hypotonus function: "<<pow(base,perpendicular)<<endl;
          cout<<"Square root function: "<<sqrt(value)<<endl;
       }

    Output

    cos function: -0.448074
    sin function: 0.893997
    tan function: -1.9952
    log function: 4.60517
    power function: 10000
    hypotonus function: 81
    Square root function: 10

    Creating Random Number in C++

    There may some situation arise when you want from your program to generate a random number. In C++ we have a function rand() function which is used to generate a random number. To use the rand() function we need to specify its header file which is #include<stdlib.h> Example

    #include <iostream>
    #include<stdlib.h>
    
    using namespace std;
    
    int main()
    {             
          
         for (int i =0; i<10 ; i++)
               {
                  cout<<"Random Number: "<<rand();
                  cout<<endl;              
                }
        return 0;
    
    }

    Output

    Random Number: 41
    Random Number: 18467
    Random Number: 6334
    Random Number: 26500
    Random Number: 19169
    Random Number: 15724
    Random Number: 11478
    Random Number: 29358
    Random Number: 26962
    Random Number: 24464

    Behind the Code Here we have used the C++ rand() in-built function to generate a random number. But to use the rand() function we need to mention its header file #include<stdlib.h>. People are also reading: