How to round a number to a multiple of 5 in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>
 
unsigned int roundToMultipleOf(unsigned int number, unsigned int multipleOf) {
    return multipleOf * floor(number / multipleOf);
}

int main() {
    std::cout << roundToMultipleOf(9, 5) << "\n";
    std::cout << roundToMultipleOf(19, 5) << "\n";
    std::cout << roundToMultipleOf(71, 5) << "\n";
}
  
  
  
/*
run:
  
5
15
70
  
*/

 



answered Jun 9, 2024 by avibootz

Related questions

1 answer 93 views
1 answer 74 views
1 answer 94 views
1 answer 84 views
2 answers 101 views
1 answer 111 views
1 answer 107 views
...