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.