To take mod of negative numbers

how to take mod of negative numbers?

I assume you mean how to compute the mod of a negative number which is produced as a result of some subtraction in code. So, in that case, you just take mod of number and then check if the result is less than 0, if it is less than 0 then just add the modular number.

Ex:-
int res = a % b;
if(res < 0)
res += b;

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.