Search code examples
c#if-statementmultiplication

How do I determine whether a randomly generated number is a multiple of 150?


I'm creating a basic mystery box program using randomly generated numbers to determine what rarity item they get. However, if the number they get is a multiple of 150, I want them to get a particular item. I'm not sure how to determine whether the random number is a multiple of 150 though.

else if (num is in 150 times tables)
{
    code...
}

Solution

  • Use the Modulo Operator '%' and check the remainder.

    if (num % 150 == 0)
    {
        // It is divisable, do something
    }
    else
    {
        // Not divisable, do something else
    }
    

    The modulo operator basically says "How many times can I divide the first number by the second, and what do I have left over?". In this example if num divided by 150 has 0 left over then it fits perfectly and you know it's a multiple.