开发者

How do you calculate if a number is a multiple of another number(well sort of)

pls,开发者_高级运维 I HAVE A NUMBER say 9 and i want to find how to create a program to check if a number b is maybe 21(ie 9+12) or 33(ie 9 + 24) or 45(9 + 36) and so on. Can i get it in C# or SQL


With the clarification, it looks like you want to find whether there is an integer x for which (in math terms, not code)

b = 9 + 12x

is true; so you want to know whether b-9 is some multiple of 12; which is easy:

bool isMatch = ((b - 9) % 12) == 0;

and if you want to know which x:

int x = (b - 9) / 12;


It's not entirely clear from your question, but I think you're looking for the modulo operator. 21 % 12 = 9, 33 % 12 = 9, 45 % 12 = 9.

In C# and SQL this is just %, and it is used like an arithmetic operator (+, -, etc)


I think you've got three variables and than the solution will be like this:

var a = 9;
var b = 12;
var c = 21;

var isInRange = IsInRange(c, a, b);

private bool IsInRange(int input, int offset, int multiple){
    return ((input - offset) % multiple) == 0;
}


Subtract your original number (in this case 9) from the number B, and then see if B % 12 is different from zero.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜