Meaning of expression n% = 100 [duplicate]

0

I have studied C and I came across a very difficult problem that after much trying, I gave up and went to get an algorithm ready on the internet to study it, but the expression n% = 100 I could not understand at all. I looked at various programming and math sites but all I found was that the percentage symbol in the middle of two integers returned the rest of the division, and I already knew that, but when does it have an equality next?

If the problem is the 1018 of the URI startup module.

Here is the code I am studying:

#include <stdio.h>
int main()
{
    int n;
    scanf("%d", &n);
    printf("%d\n", n);
    printf("%d nota(s) de R$ 100,00\n", n/100);
    n %= 100;
    printf("%d nota(s) de R$ 50,00\n", n/50);
    n %= 50;
    printf("%d nota(s) de R$ 20,00\n", n/20);
    n %= 20;
    printf("%d nota(s) de R$ 10,00\n", n/10);
    n %= 10;
    printf("%d nota(s) de R$ 5,00\n", n/5);
    n %= 5;
    printf("%d nota(s) de R$ 2,00\n", n/2);
    n %= 2;
    printf("%d nota(s) de R$ 1,00\n", n);
    return 0;
}
    
asked by anonymous 04.01.2018 / 04:12

1 answer

0

The % ( module ) operator returns the remainder of the division between two integers. When we find an expression such as x = 5 % 2; , this means that x will receive the rest of the division of 5 by 2 , that is, x will store 1 . Mathematically, 5x mod 2 .

It is important to note that % only works on integer numbers, so any attempt to use it with operands of another type, float for example, will result in an error.

The expression n %= 100; is a form of syntactic sugar equivalent to n = n % 100; and, among other ways, can be rewritten as:

aux = n % 100;
n = aux;

Some other examples of syntatic sugar in C-based languages are:

a += 5;    // a = a + 5
b -= 4;    // b = b - 4
c *= 3;    // c = c * 3
d /= 2;    // d = d / 2
    
04.01.2018 / 04:51