Determining if a number is either a multiple of ten or within a particular set of ranges

后端 未结 13 1440
Happy的楠姐
Happy的楠姐 2021-01-31 07:01

I have a few loops that I need in my program. I can write out the pseudo code, but I\'m not entirely sure how to write them logically.

I need -

if (num is          


        
13条回答
  •  无人及你
    2021-01-31 07:23

    For the first one, to check if a number is a multiple of use:

    if (num % 10 == 0) // It's divisible by 10
    

    For the second one:

    if(((num - 1) / 10) % 2 == 1 && num <= 100)
    

    But that's rather dense, and you might be better off just listing the options explicitly.


    Now that you've given a better idea of what you are doing, I'd write the second one as:

       int getRow(int num) {
          return (num - 1) / 10;
       }
    
       if (getRow(num) % 2 == 0) {
       }
    

    It's the same logic, but by using the function we get a clearer idea of what it means.

提交回复
热议问题