Home >Backend Development >C#.Net Tutorial >How to implement remainder in C language

How to implement remainder in C language

藏色散人
藏色散人Original
2020-04-26 10:46:328666browse

How to implement remainder in C language

Implementation method of remainder in C language

The remainder operator is "%", which is a binary operator in C language and requires two operands All are integers. The remainder is found to have the same sign as the dividend.

Recommended: "c Language Tutorial"

X%Y

1. It is very simple when X and Y are both positive numbers, such as: 12 %5==2;

2. When there are negative numbers,

(1) When they have different signs:

          if |x|>|y|
             ans:x+y
          else
            ans: x
eg:  -6%5==-1,6%(-5)==1,5%-6==5,-5%6==-5

(2) When they have the same sign, the two numbers will be Treated as a positive integer, but with a negative sign added to the result.

eg: -1%-5==-1,-6%-5==-1,-4%-5==-4,-5%-6==-5.

The above is the detailed content of How to implement remainder in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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