Write a C++ program that
1) prompts the user to input 3 decimal numbers (with a fraction part)
2) Prints the five decimal numbers.
3) Converts each decimal number to the nearest integer.
4) Prints the five converted numbers.
#include <iostream>
using namespace std;
int main() {
double d1, d2, d3, d4, d5;
cout << "Enter 3 decimal numbers: ";
cin >> d1 >> d2 >> d3;
d4 = 3.1415926;
d5 = 2.718281828;
int i1, i2, i3, i4, i5;
i1 = static_cast<int>(d1+0.5);
i2 = static_cast<int>(d2+0.5);
i3 = static_cast<int>(d3+0.5);
i4 = static_cast<int>(d4+0.5);
i5 = static_cast<int>(d5+0.5);
cout << i1 << " " << i2 << " " << i3
<< " " << i4 << " " << i5 << endl;
return 0;
}
Comments
Leave a comment