232k views
0 votes
(Rounding Numbers) An application of function floor is rounding a value to the nearest

integer. The statement
y = floor( x + .5 );
will round the number x to the nearest integer and assign 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.

1 Answer

5 votes

Answer:

Following are the program in the C++ Programming Language.

//header file

#include <iostream>

//header file for floor function

#include<math.h>

//using namespace

using namespace std;

//define main function

int main() {

//set and initialize float type variable and value

float a=12.3, c=1.45, d=165.345;

//initialize it in other variable

float x=a;

//set float variable to store floor value

float y=floor(x+5);

//display output with message

cout<<"before "<<x<<" and after the floor "<<y<<endl;

//set float variable to store floor value

y=floor(c+5);

//display output with message

cout<<"before "<<c<<" and after the floor "<<y<<endl;

//set float variable to store floor value

y=floor(d+5);

//display output with message

cout<<"before "<<d<<" and after the floor "<<y;

}

Output:

before 12.3 and after the floor 17

before 1.45 and after the floor 6

before 165.345 and after the floor 170

Step-by-step explanation:

Here, we define the "main()" function and inside the main function.

  • Set and initialize three float data type variables "a", "c", "d" to 12.3, 1.45, 165.345.
  • Set the float data type variable "x" to store the value of the variable "a"
  • Again set float type variable "y" to store the floor value.
  • Then, Print message and output of the variable.
  • Again we repeat point 3 and 4 two times.
User VAr
by
6.1k points