Can someone explain to me how the operator works %
: What is it? What is the logic behind that operator? What does it do and what does it return?
For example, the following code in Java (although it would work in other languages with similar notation):
char calculaLetra(int dni)
{
String juegoCaracteres="TRWAGMYFPDXBNJZSQVHLCKE";
int modulo= dni % 23;
char letra = juegoCaracteres.charAt(modulo);
return letra;
}
[Question raised in SOes chat .]
The operator
%
is called the modulus operator.A nice simple definition can be found in the documentation for this operator in C# :
For example, let's say you're calculating the following division:
Obviously, the result is
3.333333....
or in other words,3
with a remainder of1
. That remainder is the result of using the modulus operator:Although I don't know exactly how the different languages execute this operator internally, an equivalent way to express it in these languages is:
When is it useful to use this operator?
This operator has many uses.
It can be useful when you have any integer and you need to transform it into an integer within a smaller interval. Hash tables use this technique internally. For example, starting from a positive integer
n
, you need to return an integer between1
and5
. This can be achieved like this:Another typical example is when you want to find out if an integer is even or odd:
As you have been told in the other answers, it returns the remainder of a division.
I wanted to tell you that a very common use for this operator is dividing arrays into equal groups using for loops. One case is the one already mentioned in other even/odd answers, that is, in groups of two. But this can be extended to groups of 3, 4... For example, we have to make a deck of 40 cards and initialize all their values. In a loop from 0 to 39, dividing i / 4 will give us a series like this: 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3... up to 9. And the modulus i % 4 gives us will give a series like this: 0 1 2 3 0 1 2 3 0 1 2 3... These values allow us to create cards(value, suit):
Here is the code example in C#:
The % operator returns the remainder of the division between the dividend (the one to the left of the operator) and the divisor (the one to the right).
The difference with the / operator that returns the quotient
In your example, it divides the ID number by 23 and gives you the rest, which remains until the next exact division by 23 (example 24% 23 = 1).
With that number pick the letter that corresponds to the
array
onejuegoCaracteres
you have.The operator
%
is an arithmetic operator and represents division by integers.For example if you divide
10/3
this normally it is = 3.33333334, with the help of%
programming you will get a 1 as the answer to that division, why?Your remaining 1 as opposed to the result of normal division. What this operator returns is the remainder of this division! There are cases in which we would like to know, for example, even numbers, we are not interested in knowing how many times one number can be divided by another, but rather that exact result.
Our remainder is
0
therefore if it is an even number since there is no remainder which we cannot handle.In your particular case you need to know the remainder of a division to get the char inside your string.
/
with the divide operator : for example6/4=1
, unlike the operator%
, you get the remainder of the division, for example6%4=2
It may not be completely correct on paper and pencil. Look closely at the mathematical operation, it gives you 0 as an answer.
Easier if you view it as fractionals. Being
n1
the dividend,(n1/n2)
the quotient andn2
the divisor.You cancel
n2
and you are leftDrive
Therefore, it is NOT equivalent with the floating or pencil and paper data type. The module function occurs when an integer is found in the quotient, in the case of the language, when it is formatted to an integer data type as sstan does, that is, at that point of the division, the remainder is returned . If all the finite or infinite decimals of the quotient are included, the modulus will always be equal to 0.
But it is correct. I understand sstan and shadow. My module result would be rounding the quotient to an integer value. An example equal to or similar to sstan that works would be: