Reputation: 8171
I want to make a range number(for example: 1-10), according to user input in C#
.
I have a asp text-box, button. User can enter a number in text-box which is less than 10,000.
I want, when user click on button user get the range of his input value.
For Example - If user enter "35"
in text-box, he get "Number Range: (31-40)"
.
but, I have some special condition with making range -
If enter number between -
a) 1 to 50 the length of range is 10 - for example enter number is - 49
, range is (41-50)
.
b) 51 to 100 the length of range is 25 - for example enter number is - 78
, range is (76-100)
.
c) 101 to 200 the length of range is 50 - for example enter number is - 135
, range is (101-150)
.
d) 201 to 400 the length of range is 100 - for example enter number is - 245
, range is (201-300)
.
and so on........
I used Switch Case
for this but, I didn't get success in create correct logic to get this functionality.
Thanks for help...Any help will be appreciated.
Upvotes: 0
Views: 3025
Reputation: 4703
I guess only the numbers below 101 are specialized, otherwise it seems to meet a particular formula, and one possible solution is like the following:
public static int[] GetRagneOf(int value) {
var x=value>0?(value-1)/50:0;
var power=Math.Log(x, 2);
var count=25*(int)Math.Pow(2, power);
var start=1+25*(int)Math.Pow(2, 1+power);
if(value<101) {
if(value<76) {
if(value<51) {
count=10;
start=1+value-(value%10);
}
}
else {
start+=count;
}
}
return Enumerable.Range(start, count).ToArray();
}
Upvotes: 0
Reputation: 5337
try this, works without any if/else/switch, just good old math:
int x = yourNumber;
int maxRange = 50;
while (x > maxRange) maxRange *= 2;
int range = maxRange / 4 / 5 * 5;
int begin = ((x-1) / range * range) + 1;
int end = begin + range - 1;
begin
and end
are the beginning and end of your range, i think you can do the rest yourself.
Upvotes: 1
Reputation: 64229
Here an example of how I'd do it, since from the initial question it's not clear if the result should be a string, an array of numbers or whatever.
class Program {
static void Main(string[] args) {
int[] numbers = { 49, 78, 135, 245, 0, 1, 50, 51, 100, 101, 150, 151, 200, 201, 300, 301, 400};
foreach(var number in numbers) {
string result = GetRange(number);
Console.WriteLine(string.Format("Input: {0}\tResult: {1}",number, result));
}
Console.ReadKey();
}
private static string GetRange(int number) {
if(number <= 50) {
return RangeByQuotient(number, 10);
} else if(number <= 100) {
return RangeByQuotient(number, 25);
} else if(number <= 200) {
return RangeByQuotient(number, 50);
} else if(number <= 400) {
return RangeByQuotient(number, 100);
}
return "Invalid Range";
}
private static string RangeByQuotient(int number, int quotient) {
var lower = ((number-1) / quotient) * quotient + 1;
var upper = (((number-1) / quotient)+1) * quotient;
return string.Format("({0}-{1})", lower, upper);
}
}
Results in
Input: 49 Result: (41-50)
Input: 78 Result: (76-100)
Input: 135 Result: (101-150)
Input: 245 Result: (201-300)
Input: 0 Result: (1-10)
Input: 1 Result: (1-10)
Input: 50 Result: (41-50)
Input: 51 Result: (51-75)
Input: 100 Result: (76-100)
Input: 101 Result: (101-150)
Input: 150 Result: (101-150)
Input: 151 Result: (151-200)
Input: 200 Result: (151-200)
Input: 201 Result: (201-300)
Input: 300 Result: (201-300)
Input: 301 Result: (301-400)
Input: 400 Result: (301-400)
Update: Updated to correctly handle border cases, like 50, 51. etc.
Upvotes: 0
Reputation: 73492
I think this is what you're looking for Enumerable.Range
int num = 35;
if (num <= 50)
{
int divisor = 10;
int quotient = num / divisor;
var result = Enumerable.Range((quotient * divisor) + 1, divisor).ToArray();
}
else if (num <= 100 )
{
//I assume you can expand yourself
}
Upvotes: 0
Reputation: 597
one simple method would be to use if and else-if like:
if(number < 0)
{
// number below zero
}
else if(number < 10)
{
// range 0 - 9
}
else if(number < 40)
{
// range 10 - 39
}
else if(number < 200)
{
// range 40 - 199
}
else if(number < 1000)
{
// range 200 - 999
}
else
{
// range 1000 -
}
Upvotes: 1