ProgrammingHard
A program uses a modulus of 10^9 + 7 and computes (a - b + MOD) % MOD for a and b in [0, MOD). Why add MOD before the %?
- A. To make the result divisible by 2
- B. To keep the value non-negative, since a - b can be negative and C's % keeps the dividend's sign
- C. To avoid all overflow for any values
- D. Because % is undefined for negative numbers in C
Correct answer: B. To keep the value non-negative, since a - b can be negative and C's % keeps the dividend's sign
a - b can be negative, and C99 % then gives a negative remainder. Adding MOD first makes the value non-negative and the % correct.
More C Programming questions
- What is the value of 5 & 6 | 3 in C?
- What is the value of 0x80 | 0x01 | 0x10 in decimal?
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t a = 0xB6; uint8t b = (uint8t)(a << 3); uint8t c = a >>…
- Assume uint16t is 16 bits, unsigned int is 32 bits and int is 32 bits. What is printed? c #include <stdint.h> #include <stdio.h> int…
- What is printed? c #include <stdio.h> int calls = 0; int f(int n) { calls++; if (n < 2) return n; return f(n - 1) + f(n - 2); } int…
- A struct R has int16t score and uint8t id. After qsort with cmp below, what is printed? c #include <stdint.h> #include <stdio.h> #include…