
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count numbers which are divisible by all the numbers from 2 to 10 in C++
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,
Approach used in the below program is as follows
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.
Efficient approach
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.
Code-1(naive approach)
Example
#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; }
Output
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
Code-2(Efficient Approach)
Example
#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; }
Output
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 Articles
- Count numbers in a range that are divisible by all array elements in C++
- JavaScript Program to Count rotations which are divisible by 10
- Find the number of all three digit natural numbers which are divisible by 9.
- Find the sum of all 3-digit natural numbers which are divisible by 13.
- 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++
- Find the Numbers that are not divisible by any number in the range [2, 10] using C++
- Find the sum of all 2-digit natural numbers divisible by 4.
- Smallest possible number divisible by all numbers from 1 to n in JavaScript
- Find the total two-digit numbers which are divisible by $5$.
- 1870 is divisible by 22. Which two numbers nearest to 1870 are each divisible by 22?
- Find the sum of all natural numbers between 1 and 100, which are divisible by 3.
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Count n digit numbers divisible by given number in C++
- Count the numbers divisible by ‘M’ in a given range in C++
