Problem:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
My Solution:
Note: You can simplifies the coding :)
- static void Main(string[] args)
- {
- int num = 1;
- bool isDivisiblyByAll = false;
- bool isFound = false;
- while (isFound == false)
- {
- for (int i = 1; i <= 20; i++)
- {
- if (num % i == 0)
- {
- isDivisiblyByAll = true;
- }
- else
- {
- isDivisiblyByAll = false;
- break;
- }
- }
- if (isDivisiblyByAll)
- {
- isFound = true;
- Console.WriteLine("Smallest positive number that is evenly divisible by all of the numbers from 1 to 20 is : " + num);
- }
- num++;
- }
- Console.ReadLine();
- }
No comments:
Post a Comment