operands (modulo)

Post on 15-Apr-2017

38 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

9% 4 is 1cout yield to display modulu1:

the following statement a string and a value, to cout:

17 % 3 is 2 (the remainder when 17 is divided by 3 is 2)

14 % 2 is 0 (the remainder when 14 is divided by 2 is 0)

Integer Division

The division of two integer values can produce rather strange results for the unwary. For example, the expression 15/2 yields the integer result 7.

Because integers can’t contain a fractional part, the value 7.5 can’t be obtained. The fractional part resulting when two integers are divided—the remainder—is always dropped (truncated).

Therefore 9/4 is 2.And 20/3 is 6.

You need to retain the remainder of an integer division.

C++ provides the modulus operator (also referred to as the “remainder operator”),

This operator captures the remainder when an integer is divided by an integer; using a non-integer value with the modulus operator results in a compiler error.

top related