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

前端 未结 6 1791
梦毁少年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.

    0 讨论(0)
  • 2020-12-28 14:13

    Use the modulus (%) operator:

    6 % 3 == 0
    7 % 3 == 1
    
    0 讨论(0)
  • 2020-12-28 14:16

    there are some syntax errors to your program heres a working code;

    #include<stdio.h>
    int main()
    {
    int a,b;
    printf("enter any two number\n");
    scanf("%d%d",&a,&b);
    if (a%b==0){
    printf("this is  multiple number");
    }
    else if (b%a==0){
    printf("this is multiple number");
    }
    else{
    printf("this is not multiple number");
    return 0;
    }
    

    }

    0 讨论(0)
  • 2020-12-28 14:18
    bool isMultiple = a % b == 0;
    

    This will be true if a is a multiple of b

    0 讨论(0)
  • 2020-12-28 14:22

    followings programs will execute,"one number is multiple of another" in

    #include<stdio.h>
    int main
    {
    int a,b;
    printf("enter any two number\n");
    scanf("%d%d",&a,&b);
    if (a%b==0)
    printf("this is  multiple number");
    else if (b%a==0);
    printf("this is multiple number");
    else
    printf("this is not multiple number");
    return 0;
    }
    
    0 讨论(0)
  • 2020-12-28 14:27

    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 )  ....
    
    0 讨论(0)
提交回复
热议问题