1

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
AaronParkes
  • 313
  • 5
  • 15
  • call to string on the number, if it has >=3 chars in its char array check if the character is 7 - unless the homework dictates you must use mod – owenrumney Feb 13 '14 at 16:11

4 Answers4

3

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.

Tarec
  • 3,268
  • 4
  • 30
  • 47
  • 1
    C# doesn't have a modulus operator, it has a *remainder* operator. [What's the difference?](http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx) – Servy Feb 13 '14 at 16:20
  • Another educational day on Stackoverflow. Thank you :) – Tarec Feb 13 '14 at 16:22
  • @Tarec I knew I missed something obvious and stupid, thank you, I knew integers don't have decimal values and I missed that. Modulus makes a little more sense now as well :) – AaronParkes Feb 13 '14 at 16:28
  • @Servy I had no idea there was a difference, I'll read that web page now, thank you. – AaronParkes Feb 13 '14 at 16:29
1

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
Dmitriy Khaykin
  • 5,238
  • 1
  • 20
  • 32
  • 1
    C# doesn't have a modulus operator, it has a *remainder* operator. [What's the difference?](http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx) – Servy Feb 13 '14 at 16:21
  • Ok I took the word mod out of there. I thought % was always referred to as the mod operator. Either way the gist of my answer explains what the OP is seeing and asking about. – Dmitriy Khaykin Feb 13 '14 at 16:28
0

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

Community
  • 1
  • 1
tigerswithguitars
  • 2,497
  • 1
  • 31
  • 53
  • division always results in a rational number too, assuming the dividend and the divisor are rational. (and of course, none of the numeric types on C# are *capable* of representing an irrational number.) – Servy Feb 13 '14 at 16:22
  • No, it creates a *rational* number. It sounds like you don't know what the definition of a rational number is. An number that can be represented as the division of one integer by another is a rational number. An irrational number is one that *cannot* be represented in that form. On top of that, in C# `10/3` is actually an *integer* number. It's `3`. It's not even a rational non-integer number. – Servy Feb 13 '14 at 16:26
0

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
Håkan Fahlstedt
  • 2,040
  • 13
  • 17