How to round a number up to the nearest 10 in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>
 
int roundUpToNearest10(double num) {
    return (int)ceil(num / 10) * 10;
}
 
int main() {
    std::cout << roundUpToNearest10(33) << "\n";
    std::cout << roundUpToNearest10(59) << "\n";
    std::cout << roundUpToNearest10(599.99) << "\n";
    std::cout << roundUpToNearest10(3.14) << "\n";
    std::cout << roundUpToNearest10(2) << "\n";
    std::cout << roundUpToNearest10(19) << "\n";
    std::cout << roundUpToNearest10(-12) << "\n";
    std::cout << roundUpToNearest10(-101) << "\n";
    std::cout << roundUpToNearest10(-109) << "\n";
}
 
 
 
 
/*
run:
 
40
60
600
10
10
20
-10
-100
-100
 
*/

 



answered Jun 9, 2022 by avibootz

Related questions

1 answer 96 views
1 answer 121 views
1 answer 119 views
1 answer 119 views
1 answer 106 views
1 answer 125 views
1 answer 114 views
...