Write a C++ program that
1) prompts the user to input 5 decimal numbers (with a fraction part)
2) Prints the three decimal numbers.
3) Converts each decimal number to the nearest integer.
4) Prints the three converted numbers.
#include <iostream>
using namespace std;
int main() {
double d1, d2, d3, d4, d5;
cout << "Enter five decimal numbers: ";
cin >> d1 >> d2 >> d3;
cout << "First three numbers are " << d1 << " "
<< d2 << " " << d3 << endl;
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 << endl;
return 0;
}
Comments
Leave a comment