lmarkov

All Numbers From 1 To N Divisible By 3And 7

Dec 7th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. /*
  2.  * Write a program that prints all the numbers from 1 to N, that are not divisible by 3 and 7 at the same time.
  3. */
  4.  
  5. using System;
  6. using System.Diagnostics;
  7.  
  8. namespace AllNumbersFrom1ToNDivisibleBy3And7
  9. {
  10.     class AllNumbersFrom1ToNDivisibleBy3And7
  11.     {
  12.         static void Main()
  13.         {
  14.             while (true)
  15.             {
  16.                 Stopwatch stopWatch = new Stopwatch();
  17.                
  18.                 Console.WriteLine("Numbers from 1 to N that are not divisible by 3 and 7 at the same time");
  19.                 int endOfSeriesN = InputData();
  20.                 int start = 1;
  21.                 int end = endOfSeriesN;
  22.                 int currentNumber = 1;
  23.                 int counterNotDivisible = 0;
  24.                 int counterDivisible = 0;
  25.                 while (!(endOfSeriesN < 1))
  26.                 {
  27.                     stopWatch.Start();
  28.                     if (currentNumber % 3 != 0 || currentNumber % 7 != 0)
  29.                     {
  30.                         Console.WriteLine("{0}) {1}", (counterNotDivisible + 1), currentNumber);
  31.                         counterNotDivisible++;
  32.                     }
  33.                     else
  34.                     {
  35.                         counterDivisible++;
  36.                     }                    
  37.                     currentNumber++;
  38.                     endOfSeriesN--;
  39.                 }
  40.                 if (counterNotDivisible == 1)
  41.                 {
  42.                     Console.WriteLine("From {0} to {1} there is {2} number that is not divisible by 3 and 7 at the same time!" + Environment.NewLine, start , end, counterNotDivisible);
  43.                 }
  44.                 else
  45.                 {
  46.                     Console.WriteLine("From {0} to {1} there are {2} numbers that are not divisible by 3 and 7 at the same time!" + Environment.NewLine, start, end, counterNotDivisible);
  47.                 }
  48.                 if (counterDivisible == 1)
  49.                 {
  50.                     Console.WriteLine("From {0} to {1} there is {2} number that is divisible by 3 and 7 at the same time!" + Environment.NewLine, start, end, counterDivisible);
  51.                 }
  52.                 else
  53.                 {
  54.                     Console.WriteLine("From {0} to {1} there are {2} numbers that are divisible by 3 and 7 at the same time!" + Environment.NewLine, start, end, counterDivisible);
  55.                 }
  56.                 stopWatch.Stop();
  57.                 TimeSpan ts = stopWatch.Elapsed;                
  58.                 string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
  59.                 Console.WriteLine("runtime: " + elapsedTime + Environment.NewLine);
  60.             }
  61.         }
  62.  
  63.         static int InputData()
  64.         {
  65.             int number;
  66.             string invalidInput = "Please enter a value between 1 and " + int.MaxValue + Environment.NewLine;
  67.             Console.WriteLine("Enter a value for N: ");
  68.             while (!(int.TryParse(Console.ReadLine(), out number) && number >= 1))
  69.             {
  70.                 Console.WriteLine(invalidInput);
  71.                 Console.WriteLine("Enter a value for N: ");
  72.             }
  73.             return number;
  74.         }                
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment