Question

Really been racking my brain trying to understand Modulus.

This is an exercise question and the guidelines for it. here is my exercise question:

Write an expression that looks for a given integer if its third digit (right to left) is 7.

The guidelines say this:

Divide the number by 100 and save it in a new variable, which then divide by 10 and take the remainder. The remainder of the division by 10 is the third digit of the original number. Check if it is equal to 7.

So I followed the guidelines and wrote the code. Here is my code.

Console.WriteLine("Input 3 digit Number");
int input = int.Parse(Console.ReadLine());
input /= 100;
input %= 10;
if (input == 7)
{
Console.WriteLine("number is 7!");
}
else
{
Console.WriteLine("number is not 7");
}

Problem is I don't understand how it's working, lets say I input 712 as a number, it's 3rd digit to the left is 7, so it should work, and it does the console will tell me it's 7.

However if I was to calculate 712 / 100 that's 7.12. The to calculate a modulus I take the input (now 7.12) divide by 10, subtract the whole number (this is decimal so I subtract 0.71 and multiply by 10 again, so I'm left with 0.02.

If I run 7.12 % 10 into C# however I get 7.12 as the output. How?

What am I missing? What am I doing wrong? This is driving me crazy really need help on this. Why is modulus being used and how is it getting the output it's getting?

P.S. I do understand Integers don't take decimals so it would output as 7 not 7.12 (hence the code says its 7) So I understand that bit.

Était-ce utile?

La solution

712 / 100 IS NOT 7.12. It's 7. If you'll divide 7 by 10, then you'll get 0. If you'll take modulus of 7 % 10, you'll get 7, because it's the rest of dividing 7/10. Dividing ints always rounds down to the full decimal value, there is no rest. That's what the modulus operator is for and you need to evaluate them separately.

Example:

Your input is 12345. 12345/100 gives you 123, because you have 123 full hundrets in the input. Then 123/10 would give you 12, because there are 12 full tens in 123 and 123%10 would give you 3 (which is the number you're looking for), because it's the rest from dividing 123/10.

And if you'll ever need division in the school-math type, use floating point types, like float or double. F.e. 12345f/100 would give you approximately 123.45f.

Autres conseils

The issue here actually has nothing to do with C# % operator.

It is because you are using int vs. double

  • int will always be an integer, so it rounds down as others have stated.
  • double for example will keep decimal places.
  • The C# % operator doesn't care and doesn't round down - it just gets you the remainder in the same type used for the input

Here's a quick test:

int inputAsInt = 712;
inputAsInt /= 100;
inputAsInt %= 10;

Console.WriteLine(inputAsInt);
// prints 7

double inputAsDouble = 712;
inputAsDouble /= 100;
inputAsDouble %= 10;

Console.WriteLine(inputAsDouble);
// prints 7.12

N / 100 "removes" digit one and two from the number and then (N / 100) % 10 removes all digits after the third digit:

N = ABCDE

N / 100 = ABC 

ABC % 10 = C

Modulo is a very different operation to divide.

Another way you can think of it is the remainder of an integer division.

See this SO question and answer

27 / 16 = 1, remainder 11
=> 27 mod 16 = 11 

Wiki Page on Modulo Operations

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top