Home > Backend Development > C++ > In C language, the absolute value of a negative number is a positive number

In C language, the absolute value of a negative number is a positive number

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-30 10:41:05
forward
1125 people have browsed it

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.

Example

#include<stdio.h>
int main() {
   int a = 7, b = -10, c = 2;
   printf("Result: %d", a % b / c);
}
Copy after login

Output

Result: 3
Copy after login

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.

Example

#include<stdio.h>
int main() {
   int a = 7, b = -10;
   printf("Result: %d", a % b);
}
Copy after login

Output

Result: 7
Copy after login

If we swap the signs of a and b, then it will become the following.

Example

#include<stdio.h>
int main() {
   int a = -7, b = 10;
   printf("Result: %d", a % b);
}
Copy after login

Output

Result: -7
Copy after login
Copy after login

Similarly, if both are negative, the result will also be negative.

Example

#include<stdio.h>
int main() {
   int a = -7, b = -10;
   printf("Result: %d", a % b);
}
Copy after login

Output

Result: -7
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template