How to round up the result of integer division in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

int main() {
    int a = 247, b = 9;
    
    double result = static_cast<double>(a) / b;
    int result_round_up = static_cast<int>(std::ceil(static_cast<double>(a) / b));
    
    std::cout << result << std::endl;
    std::cout << result_round_up << std::endl;
}

  
  
/*
run:
     
27.4444
28
              
*/

 



answered Dec 4, 2024 by avibootz

Related questions

1 answer 121 views
1 answer 172 views
1 answer 131 views
1 answer 160 views
1 answer 156 views
1 answer 89 views
1 answer 137 views
...