Sukhmeet Hora
Sukhmeet Hora

Reputation: 80

To check the range of given number

I want to check the number is in range of 1 to 100, 100 to 200 and so on but without using if conditions. if it is in range of 1 to 100 return 100.

Upvotes: 1

Views: 111

Answers (4)

Manoj Singhal
Manoj Singhal

Reputation: 109

you can use the following function

-(int)getOutput:(int)input{
int temp = (input+99)/100;  //(eg :- temp=(99+99)/100=1;

int output=temp*100  //(eg :- 1*100 =100);
return output;
}

Upvotes: 0

TamilKing
TamilKing

Reputation: 1643

- (int)getRange:(int)value
{
    int range = value / 100;
    range = (range * 100) + ((value % 100) > 0 ? 100 : 0);
    return range;
}

Try this hope it ll help you..

Upvotes: 0

Devanshu Saini
Devanshu Saini

Reputation: 775

/* we need to get 700 in result */

NSInteger myNumber = 619;

/* outputNumber will give 700 in result */

NSInteger outputNumber = (NSInteger)(ceil(myNumber/100) * 100);

Upvotes: 0

Leo
Leo

Reputation: 24714

If the type is int

For 1-100,101-200,

 int x = 99;
 x = ((x-1)/100 + 1)*100;

For 1-50,51-100

int x= 51;
x = ((x-1)/50 + 1)*50;

Upvotes: 7

Related Questions