- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given a number let’s say, num and the task is to calculate the count of numbers in the range 1 to num that are divisible by 2, 3, 4, 5, 6, 7, 8, 9 and 10.

**Input** − int num = 10000

**Output** − Count numbers which are divisible by all the numbers from 2 to 10 are: 3

**Explanation** − There are 3 numbers from 1 to 10000 that are divisible by all the numbers starting from 2 till 10 and those are −

2520-: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 20, 21, 24, 28, 30, 35, 36, 40, 42, 45, 56, 60, 63, 70, 72, 84, 90, 105, 120, 126, 140, 168, 180, 210, 252, 280, 315, 360, 420, 504, 630, 840, 1260, 2520. 5040-: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 28, 30, 35, 36, 40, 42, 45, 48, 56, 60, 63, 70, 72, 80, 84, 90, 105, 112, 120, 126, 140, 144, 168, 180, 210, 240, 252, 280, 315, 336, 360, 420, 504, 560, 630, 720, 840, 1008, 1260, 1680, 2520, 5040 7560-: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 18, 20, 21, 24, 27, 28, 30, 35, 36, 40, 42, 45, 54, 56, 60, 63, 70, 72, 84, 90, 105, 108, 120, 126, 135, 140, 168, 180, 189, 210, 216, 252, 270, 280, 315, 360, 378, 420, 504, 540, 630, 756, 840, 945, 1080, 1260, 1512, 1890, 2520, 3780.

**Input** − int num = 20000

**Output** − Count numbers which are divisible by all the numbers from 2 to 10 are − 3

**Explanation** − There are 7 numbers from 1 to 10000 that are divisible by all the numbers starting from 2 till 10 and those are − 2520, 5040, 7560, 10080, 12600, 15120 and 17640,

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the **naive approach**.

Input the number let’s say num

Take an array and store all the numbers from 2 to 10 inside an integer array of fixed length which is 9.

Take temporary variables first is count to store the total of numbers and another is flag to check whether the number is divisible or not.

Start loop For from i to 1 till the num

Inside the loop, set num to i and index to 0

Start while till index is less than 9 i.e. size of an array

Check IF num % arr[index++] == 0 then set flag as 1 Else set flag as 0

Check IF flag is 1 then increment the count by 1

Return count

Print the result.

As we can see there is a pattern in the numbers that is divisible by all the numbers from 2 to 10.

The smallest number which is divisible by all the numbers from 2 to 10 is 2520

5 * 7 * 8 * 9 = 2520(n = 1) 5 * 7 * 8 * 9 * 2 = 5040(n = 2) 5 * 7 * 8 * 9 * 3 = 7560(n = 3) . .

As we can see 2520 is the common factor of all the numbers divisible by 2, 3, 4, 5, 6, 7, 8, 9, 10. so , if we divide the given number by 2520 we will get our result.

#include <bits/stdc++.h> using namespace std; int count(int num){ int count = 0; int flag=0; int index=0; int arr[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i = 1; i <= num; i++){ int num = i; index=0; while(index<9){ if(num % arr[index++] == 0){ flag=1; } else{ flag=0; break; } } if (flag == 1){ count++; } } return count; } int main(){ int num = 10000; cout<<"Count numbers which are divisible by all the numbers from 2 to 10 are: "<<count(num); return 0; }

If we run the above code it will generate the following output −

Count numbers which are divisible by all the numbers from 2 to 10 are: 3

#include <bits/stdc++.h> using namespace std; int main(){ int num = 10000; int count = num / 2520; cout<<"Count numbers which are divisible by all the numbers from 2 to 10 are: "<<count; return 0; }

If we run the above code it will generate the following output −

Count numbers which are divisible by all the numbers from 2 to 10 are: 3

- Related Questions & Answers
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Count numbers in a range that are divisible by all array elements in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Count all prefixes of the given binary array which are divisible by x in C++
- Count n digit numbers divisible by given number in C++
- Sum of the numbers up to N that are divisible by 2 or 5 in c programming
- Program to print all the numbers divisible by 3 and 5 in C++
- Count of numbers which can be made power of 2 by given operation in C++
- Smallest possible number divisible by all numbers from 1 to n in JavaScript
- Sum of first N natural numbers which are divisible by X or Y
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Count numbers from range whose prime factors are only 2 and 3 in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- N digit numbers divisible by 5 formed from the M digits in C++

Advertisements