# How to round off numbers nearest 10 in C++

This tutorial will show you how to round off numbers to their nearest 10th multiple in C++. This means if we enter the number 21 the program will give an output 20. And if we give and input of 35.8 the program will give an output of 40. Now it should be noted that the program should be self-sufficient to run on any inserted number be it a whole number or a number having a decimal point.

Approach to solving this program :

If we take a whole number which is not a multiple of 10 and store it in an integer data type which is not capable of storing decimal values, we would then be able to divide that by 10 and then again multiply it by 10 and that would give us the required multiple of 10.

For example, 23 divided by 10 gives us 2.3 but the integer can only hold 2 and then we multiply it by 10 to give us 20. But this poses a problem as even 28 or 29 which is closer to 30 will give us 20 in this case. And also floating-point numbers will not be solved using this technique.

For example, if we enter 73.5 in a float variable and then divide it by 10 we will get 7.35 and again multiplying by 10 will give us the original number back (73.5). So we will have to have a different approach here such that it satisfies all the cases.

So for this, we will use the “floor” function to separate the decimal numbers from its integer part. And then we check if the last digit is less than or more than 5, in both cases the number is either degraded or incremented to the previous or next multiple of ten. Whereas if the last digit is 5 we will need to check if the decimal part is there or not. If the decimal part is more than 0 then the number shall be rounded off to the next multiple of 10, if there is no decimal or is 0 then the number shall be decremented to the previous multiple of 10.

## Algorithm: round off numbers nearest 10 in C++

1. Take input float n. ( You may Learn: Taking only integer input in C++ )
2. Find the integer part by floor(n)
3. Find the decimal part by dec=n-floor(n)
4. check if(n%10<5) then
5. n=(n/10)*10
6. else if(n%10==5)  then
7. check if(dec>0) then
8. n=(((n+10)/10)*10)
9. else n=(n/10)*10;
10. end if
11. else  n=(((n+10)/10)*10);
12. end if
13. print(n)

## C++ Program to round off numbers to their nearest 10th multiple

```#include <iostream>
#include<cmath>
using namespace std;
int main()
{
float num,dec,numcpy;
int n,last;
cout<<"Input number :";
cin>>num;
numcpy=num;
dec=num-floor(num);
num=numcpy;
n=floor(num);
if(n%10<5)
{
n=(n/10)*10;
}
else if(n%10==5)
{
if(dec>0)
n=(((n+10)/10)*10);
else
n=(n/10)*10;
}
else
{
n=(((n+10)/10)*10);
}

cout<<"The rounded off number is: "<<endl<<n;
}
```

#### The output of the above code: This is a very simple program. I tried my best to do it in the easiest way possible. Hope you like it. If you have any doubts, please comment below.