(Rounding Numbers) An application of function floor is rounding a value to the nearest integer. The statement y = floor( x + .5 ); rounds the number x to the nearest integer and assigns the result to y.
Write a program that reads several numbers and uses the preceding statement to round each of these numbers to the nearest integer. For each number processed, print both the original number and the rounded number.
Solution:
#include < iostream> using namespace std; double floorNumber( double ); int main() { double number; cout << "Enter number to floor rounding( -999 to end): "; cin >> number; while( number != -999 ) { cout << "Entered number " << number << " rounded to " << floorNumber( number ) << endl; cout << "Enter number to floor rounding( -999 to end): "; cin >> number; } //for pause system("PAUSE"); return 0; } //function of floor rounding double floorNumber( double number ) { return floor(number); }
Post A Comment:
0 comments: