Division Operations

The mod operator % finds the remainder when two ints are divided.

For example:   20 % 8 = 4 , because  20 / 8 = 2 with remainder = 4.

Trace this algorithm and state the output.

    int  num  =  39 ;
    int  d   =   num / 8 ;
    int  r    =   num % 8 ;

    output("Div = " + d);
    output("Rem = " + r);
    output( 8*d + r );

Solution

   Div = 4

   Rem = 7

   39