C++ code to count number of dice rolls to get target x

Suppose we have a number x. We have a six-faced dice and its faces are numbered from 2 to 7. We want exactly x points from the dice. When we throw the dice the face number will be added up to reach our target. We do not really care about the number of dice rolls, so we just want to know any number of rolls we can make to be able to get exactly x points for them. We are very lucky, so if the probability to get x points with chosen number of rolls is non-zero, we will be able to roll the dice in such a way. We have to find the number.

So, if the input is like x = 100, then the output will be 27, because we get 2, 11 times, 3, six times and 6, 10 times. (Other answers are also possible)


To solve this, we will follow these steps −

return floor of (x / 2)


Let us see the following implementation to get better understanding −

using namespace std;
int solve(int x){
   return x/2;
int main(){
   int x = 100;
   cout << solve(x) << endl;





Updated on: 15-Mar-2022


Kickstart Your Career

Get certified by completing the course

Get Started