Today I had an assignment that told me to only allow numbers through that are a multiplication of 5.
After looking around on the internet I found the % operator. I thought this operator only removed the decimals, for example 5 % 2 equals 2.
But I could use it in my program as if(iWithdraw % 5 == 0){do this and that}.
I'm glad it worked but I still don't know why it worked haha.
Can someone explain this to me?
Loading
Jignesh TrivediPosted Aug 12, 2013, 10:54 PM
hi,
% operator computes the remainder after dividing its first operand by second one.
For example 5%2
first of it perform 5/2 and find nearest devided value 2*2 = 4 4 is nearest value of 5 than it perform 5-4=1 so answer of 5%2 = 1
Please refer
http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx
hope this will help you.
Hemant SrivastavaPosted Aug 12, 2013, 4:59 PM
Hemant SrivastavaPosted Aug 9, 2013, 2:50 PM
For example: If you divide 15 by 5, it will give 0 since after dividing 15 by 5, we don't get any reaminder at the end.
similerly 3 divided by 2 wil give you 1 (as a remainder)
Here is the code for more explaination:
class MainClass6
{
static void Main()
{
Console.WriteLine(5 % 2); // int
Console.WriteLine(-5 % 2); // int
Console.WriteLine(5.0 % 2.2); // double
Console.WriteLine(5.0m % 2.2m); // decimal
Console.WriteLine(-5.2 % 2.0); // double
}
}
/*
Output:
1
-1
0.6
0.6
-1.2
*/