Convert Int to Double
Convert Int to Double in C++ (5 Programs)
BeginnerTopic: String Conversion Programs
C++ Convert Int to Double Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int num = 123;
// Method 1: Direct assignment
double d1 = num;
// Method 2: Using static_cast
double d2 = static_cast<double>(num);
// Method 3: Explicit casting
double d3 = (double)num;
cout << "Integer: " << num << endl;
cout << "Double (method 1): " << d1 << endl;
cout << "Double (method 2): " << d2 << endl;
cout << "Double (method 3): " << d3 << endl;
return 0;
}Output
Integer: 123 Double (method 1): 123 Double (method 2): 123 Double (method 3): 123
Understanding Convert Int to Double
This program demonstrates 5 different methods to convert an integer to a double: direct assignment, static_cast, explicit casting, using multiplication, and using division.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.