w3resource

C++ Exercises: Test the Type Casting


Test Type Casting in C++

Write a program in C++ to test Type Casting.

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file
#include <iomanip>  // Including the header file for input-output manipulators
using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    // Outputting a message indicating the purpose of the program
    cout << "\n\n Formatting the output using type casting:\n";
    cout << "----------------------------------------------\n"; 

    cout << "Print floating-point number in fixed format with 1 decimal place: "; // Outputting a message

    cout << fixed << setprecision(1); // Setting the floating-point number to display with 1 decimal place

    cout << "\nTest explicit type casting :\n"; // Outputting a message for explicit type casting

    int i1 = 4, i2 = 8; // Initializing two integer variables

    cout << i1 / i2 << endl;       // Outputting the result of integer division
    cout << (double)i1 / i2 << endl;  // Explicitly casting i1 to double before division
    cout << i1 / (double)i2 << endl;  // Explicitly casting i2 to double before division
    cout << (double)(i1 / i2) << endl; // Casting the result of the integer division to double

    double d1 = 5.5, d2 = 6.6; // Initializing two double variables

    cout << "\nTest implicit type casting :\n"; // Outputting a message for implicit type casting

    cout << (int)d1 / i2 << endl;    // Implicitly casting d1 to int before division with i2
    cout << (int)(d1 / i2) << endl;  // Casting the result of d1 divided by i2 to int

    cout << "\nint implicitly casts to double: \n";
    d1 = i1; // Implicitly casting integer i1 to double d1
    cout << d1 << endl;  // Outputting the value of d1 after casting (implicit type casting)

    cout << "double truncates to int!: \n";
    i2 = d2; // Implicitly casting double d2 to int i2
    cout << i2 << endl; // Outputting the value of i2 after casting (implicit type casting)

    return 0; // Returning 0 to indicate successful program execution
}

Sample Output:

Formatting the output using type casting:                             
----------------------------------------------                         
Print floating-point number in fixed format with 1 decimal place:      
Test explicit type casting :                                           
0                                                                      
0.5                                                                    
0.5                                                                    
0.0                                                                    
                                                                       
Test implict type casting :                                            
0                                                                      
0                                                                      
                                                                       
int implicitly casts to double:                                        
4.0                                                                    
double truncates to int!:                                              
6 

Flowchart:

Flowchart: Test the Type Casting

For more Practice: Solve these Related Problems:

  • Write a C++ program to demonstrate both implicit and explicit type casting between int, float, and double.
  • Write a C++ program that casts a floating-point number to an integer and then back to a float, displaying each conversion.
  • Write a C++ program to perform type casting in arithmetic operations and display how the results differ with each cast.
  • Write a C++ program that uses a user-defined function to cast between different numeric types and prints the results with precision formatting.

Go to:


PREV : Compute Specified Expression.
NEXT : Print Mystery Series 1 to 50.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.