Unsigned modulos: alternative approach? [duplicate] January 7, 2024 by Tarik This avoids looping: int tmp = a % b; if (tmp < 0) tmp += b; Notice that both a and b need to be signed.