Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Write a program that prints all the numbers from 1 to N, that are not divisible by 3 and 7 at the same time.
- */
- using System;
- using System.Diagnostics;
- namespace AllNumbersFrom1ToNDivisibleBy3And7
- {
- class AllNumbersFrom1ToNDivisibleBy3And7
- {
- static void Main()
- {
- while (true)
- {
- Stopwatch stopWatch = new Stopwatch();
- Console.WriteLine("Numbers from 1 to N that are not divisible by 3 and 7 at the same time");
- int endOfSeriesN = InputData();
- int start = 1;
- int end = endOfSeriesN;
- int currentNumber = 1;
- int counterNotDivisible = 0;
- int counterDivisible = 0;
- while (!(endOfSeriesN < 1))
- {
- stopWatch.Start();
- if (currentNumber % 3 != 0 || currentNumber % 7 != 0)
- {
- Console.WriteLine("{0}) {1}", (counterNotDivisible + 1), currentNumber);
- counterNotDivisible++;
- }
- else
- {
- counterDivisible++;
- }
- currentNumber++;
- endOfSeriesN--;
- }
- if (counterNotDivisible == 1)
- {
- 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);
- }
- else
- {
- 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);
- }
- if (counterDivisible == 1)
- {
- 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);
- }
- else
- {
- 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);
- }
- stopWatch.Stop();
- TimeSpan ts = stopWatch.Elapsed;
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
- Console.WriteLine("runtime: " + elapsedTime + Environment.NewLine);
- }
- }
- static int InputData()
- {
- int number;
- string invalidInput = "Please enter a value between 1 and " + int.MaxValue + Environment.NewLine;
- Console.WriteLine("Enter a value for N: ");
- while (!(int.TryParse(Console.ReadLine(), out number) && number >= 1))
- {
- Console.WriteLine(invalidInput);
- Console.WriteLine("Enter a value for N: ");
- }
- return number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment