tags:

views:

114

answers:

4

Without using string manipulation (checking for an occurrence of the . or , character) by casting the product of an int calculation to string.

or

without using try / catch scenarios relying on errors from data types.

How do you specifically check using c# if a number is a multiple of another?

for example 6 is a multiple of 3, but 7 is not.

+7  A: 

Try

public bool IsDivisble(int x, int n)
{
   return (x % n) == 0;
}

The modulus operator % returns the remainder after dividing x by n which will always be 0 if x is divisible by n.

Samuel Jack
% or mod in vb calculates what's left over after you do a devision, for example 25 % 6 = 1. So if it's 0, there's no left over so the first must be a multiple of the second.
Jouke van der Maas
Given a number n, it can be expressed in terms of another number m thusly: n = k*m + r. For example 6 = 1 * (4) + 2 or 6 = 2 * (3) + 0. x % y returns just the 'r' term in the aforementioned formula.
Rodrick Chapman
+3  A: 

Use the modulus (%) operator:

6 % 3 == 0
7 % 3 == 1
Joel Mueller
+3  A: 

I don't get that part about the string stuff, but why don't you use the modulo operator (%) to check if a number is dividable by another? If a number is dividable by another, the other is automatically a multiple of that number.

It goes like that:

   int a = 10; int b = 5;

   // is a a multiple of b 
   if ( a % b == 0 )  ....
Johannes Rudolph
Thanks, some example code I've seen on the net use string manipulation on the product to solve this issue.
JL
+5  A: 
bool isMultiple = a % b == 0;

This will be true if a is a multiple of b

Lee