Page 289 - Computer_Science_F5
P. 289

Computer Science  Table 4.15: Built-in function        <cmath>         Example
                                                               Header
            Built-function
                             Application
                                                               <cmath>
                                                                              sqrt (x)
                             Find the square root of  x
            sqrt (x)
            abs (x)
                             Find absolute of x
                                                                              abs (x)
          FOR ONLINE READING ONLY
            exp (x)
                                                                              pow(x, y)
                                                              <cmath>
                             Find x power y
            pow(x, y)        Find the natural exponent of x   <cmath>         exp (x)
            tolower(x)       Convert character to lower case  <cctype>        tolower(x)
            toupper(x)       Convert character to upper case. <cctype>        toupper(x)
                             Returns  true  if  x is uppercase
            isupper(x)                                        <cctype>        isupper(x)
                             letter, otherwise it returns false.
                             Returns  true  if  x is lowercase
            islower(x)                                        <cctype>        islower(x)
                             letter, otherwise it returns false.
            isPrime          Returns true if x is prime numbr,   <cmath>      isPrime
                             otherwise it returns false.

           Casting
           C++  uses casting technique to avoid implicit type conversion. Casting is used to
           convert data type from one data type to another through the use of a cast operator.
           The cast operator is also called type conversion or type casting. The following C++
           Program Example 4.31 demonstrates the use of casting. The output is shown in
           Figure 4.60.

                   Program Example 4.31:
              Casting


              #include<iostream>
              using namespace std;
              int main()
              {
                    float l, w; //Declaration of length and width as integers data type
                    double area; //Declaration of area as double data type
                    /* This program asks you to enter length and with in float,and
                    displays the results in (i) float and (ii) integer
                    */
                    cout<<”Enter a length in metres using float : “;
                    cin>>l; //Read value of length entered by the user
                    cout<<”Enter a width in metres using float: “;
                    cin>>w; //Read value of length entered by the user
                    area = l * w;
                    cout<<”The area of the rectangle in float (before casting) is “<<area<<” square metres”<<endl;

                    //use casting int(area) to get area in integer
                    cout<<”The area of the rectangle (after casting) is “<<int(area)<<” square metres”<<endl;
                    return 0;
              }


                                                 280
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   280                                                     23/07/2024   12:34
   284   285   286   287   288   289   290   291   292   293   294