C# How to determine if a number is a multiple of another?

前端 未结 6 1790
梦毁少年i
梦毁少年i 2020-12-28 13:50

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

6条回答
  •  孤城傲影
    2020-12-28 14:08

    Try

    public bool IsDivisible(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.

    For more information, see the % operator on MSDN.

提交回复
热议问题