settingsAccountsettings
Menusettings

Q: Print numbers in interval dividable by given number (C# task)

+2 votes

Write a program that reads two positive integer numbers and prints how many numbers p exist between them such that the reminder of the division by 5 is 0.

Examples:

start

end

p

comments

17

25

2

20, 25

5

30

6

5, 10, 15, 20, 25, 30

3

33

6

5, 10, 15, 20, 25, 30

3

4

0

-

99

120

5

100, 105, 110, 115, 120

107

196

18

110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195


asked in C# category by user ak47seo
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Indeed this task is interesting! ;-P Instead of:

(i % 5 == 0)

another number can be used: 7,2, etc.

Here is my solution:

using System;

class NumbersInIntervalDividableByGivenNumber
{
    static void Main()
    {
        Console.WriteLine("Please enter your 1st number:");
        int start = int.Parse(Console.ReadLine());

        Console.WriteLine("Please enter your 2nd number:");
        int finish = int.Parse(Console.ReadLine());

        int p = 0;
        for (int i = start; i <= finish; i++)
        {
            if (i % 5 == 0)
            {
                Console.WriteLine(i);
                p++;
            }
        }
        Console.WriteLine("{0} numbers are dividible by 5: from {1} to {2}.", p, start, finish);
    }
}
answered by user Jolie Ann
selected by user golearnweb
...