How to tell if an integer is a multiple of 10 (Xcode)?
How can I tell whether an integer is a multiple of 1开发者_StackOverflow社区0 (i.e. 10, 20, 30, 40, etc) in Objective-C? Thanks.
BOOL isMultipleOfTen = !(someInt % 10);
if((int)myNumber % 10 == 0){
// it is
}
else {
// it isn't
}
its pretty much the same for all languages afaik
Purely for @Aurum Aquila's entertainment
disclaimer - DO NOT do this! lol... just a bit of fun
BOOL isDone = NO;
int multiple = 1;
while(!isDone){
for(int i = 1; i<11; i++){
if(i*multiple==(int)numberToCheck){
if(i==10){
//is multiple
}
else{
//isn't
}
isDone = YES;
}
}
multiple++;
}
An entry for Aurum's challenge:
NSString* aString = [NSString stringWithFormat: @"%d", (int) aNumber];
bool isMultipleOf10 = [aString hasSuffix: @"0"];
I was also going to write a version in which I reimplement -stringWithFormat:
for integers which would, of course, involve doing lots of explicit n % 10
operations, but I have just lost the will to live.
If the mod result is 0,
try C language code
if(no%10==0){ //number in multiple of 10 } else{//reminder is not 0 //number is not in multiple of 10 }
http://unrestrictedknowledge.blogspot.com/2009/07/modulo-using-bitwise-operators.html
精彩评论