Home >Backend Development >C++ >In C language, the absolute value of a negative number is a positive number
Here we will see what we get if we use negative numbers to get the modulus. Let us look at the following program and its output to understand this concept.
#include<stdio.h> int main() { int a = 7, b = -10, c = 2; printf("Result: %d", a % b / c); }
Result: 3
Here the precedence of % and / are same. So % is working at first, so a % b is generating 7, now after dividing it by c, it is generating 3. Here for a % b, the sign of left operand is appended to the result. Let us see it more clearly.
#include<stdio.h> int main() { int a = 7, b = -10; printf("Result: %d", a % b); }
Result: 7
If we swap the signs of a and b, then it will become the following.
#include<stdio.h> int main() { int a = -7, b = 10; printf("Result: %d", a % b); }
Result: -7
Similarly, if both are negative, the result will also be negative.
#include<stdio.h> int main() { int a = -7, b = -10; printf("Result: %d", a % b); }
Result: -7
The above is the detailed content of In C language, the absolute value of a negative number is a positive number. For more information, please follow other related articles on the PHP Chinese website!