division in C++

Hello friends,

I have a small question.

why does cout << 11 % 3 << '\n'; gives 2 as an output?

I am really confused because the answer from the calculator is equal 3.6

However the compiler does not even give 3 as an answer. It gives 2. How is this possible? and why does it happens.

Thanks you in advance :)
Last edited on
The % operator gives you the remainder.

          (3%11)
            ↓             
            ↓             
 11         2
──── = 3 + ───
  3    ↑    3 
       ↑
     (3/11)
aha ok. Now it makes sense :)

Thanks a lot Peter87.
% is the modulo operator.

/ is the division operator.

http://www.cplusplus.com/doc/tutorial/operators/
and you need floating point to see decimals.
try

cout << 11.0/3.0 << endl;
Remember, floating-point constants are type double by default. In Listing 3.11, the division operator represents three distinct operations: int division, float division, and double division. C++ uses the context—in this case the type of operands—to determine which operator is meant.
Getting the <a href="http://www.cetpainfotech.com/technology/c-language-training">C language Training in Noida</a> click here !!
Topic archived. No new replies allowed.