Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Elvenware

Elvenware

Here is an alternate syntax:

Console.Write("Enter a number between 1 and 5,000: ");
int input = int.Parse(Console.ReadLine());
for (int i = 1; i < input; i++)
{
    if (i <= 100)
    {
        if (i % 10 == 0) Console.WriteLine("Evenly divisible by 10: {0}", i);
    }
    else if (i <= 1000)
    {
        if (i % 150 == 0) Console.WriteLine("Evenly divisible by 150: {0}", i);
    }
    else
    {
        if (i % 750 == 0) Console.WriteLine("Evenly divisible by 750: {0}", i);
    }
}

In this solution, each clause ensures that numbers of a certain range can only be tested in a certain location: numbers under a hundred in the top third of the loop, numbers between 100 and 1000 in the second third, and higher numbers in the last third. That means you don’t have to spend so much testing for certain conditions.