Advertisement
coasterka

NumsInIntervalDivisibleByGivenNum

Mar 18th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _11NumsInIntervalDivisibleByGivenNum
  4. {
  5.     class NumsInIntervalDivisibleByGivenNum
  6.     {
  7.         static void Main()
  8.         {
  9.             Console.WriteLine("Enter first number (must be positive).");
  10.             uint firstN = uint.Parse(Console.ReadLine());
  11.             while (firstN <= 0)
  12.             {              
  13.                 Console.WriteLine("First number must be positive! Try again.");
  14.                 firstN = uint.Parse(Console.ReadLine());
  15.             }
  16.             Console.WriteLine("Enter second number (must be positive).");
  17.             uint secondN = uint.Parse(Console.ReadLine());
  18.             while (secondN <=0)
  19.             {
  20.                 Console.WriteLine("Second number must be positive! Try again.");
  21.                 secondN = uint.Parse(Console.ReadLine());
  22.             }
  23.             uint numsBetweenFirstAndSecond = secondN - firstN;
  24.             Console.WriteLine("Numbers between the first and the second number you entered: {0}", numsBetweenFirstAndSecond);
  25.             uint p = 0;
  26.             p = numsBetweenFirstAndSecond / 5;
  27.             bool existingDivisibleNums = p == 0;
  28.             if (existingDivisibleNums)
  29.             {
  30.                 Console.WriteLine("There are no numbers divisible by 5 in this interval!");                
  31.             }
  32.             else if (secondN % 5 == 0)
  33.             {
  34.                 Console.WriteLine("Numbers divisible by 5 without remainder in this interval: {0}", (p + 1));
  35.             }
  36.             else
  37.             {
  38.                 Console.WriteLine("Numbers divisible by 5 without remainder in this interval: {0}", p);                
  39.             }
  40.             Console.Write("Numbers whose remainder of the division by 5 is 0: ");
  41.             string result = string.Empty;
  42.             for (uint i = firstN; i <= secondN; i++)
  43.             {                
  44.                 if (i % 5 == 0)
  45.                 {                    
  46.                     result += i + ", ";                    
  47.                 }                
  48.             }
  49.             if (!string.IsNullOrEmpty(result))
  50.             {
  51.                 result = result.Substring(0, result.Length - 2);
  52.             }
  53.             Console.WriteLine(result);            
  54.         }            
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement