- Related Questions & Answers
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Count elements that are divisible by at-least one element in another array in C++
- Find an array element such that all elements are divisible by it using c++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- Print array elements that are divisible by at-least one other in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Elements to be added so that all elements of a range are present in array in C++
- Elements of an array that are not divisible by any element of another array in C++
- Count the number of elements in an array which are divisible by k in C++
- Python Program to Print all Numbers in a Range Divisible by a Given Number
- Count all prefixes of the given binary array which are divisible by x in C++
- Finding the count of numbers divisible by a number within a range using JavaScript
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++

- 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 provided two numbers START and END to define a range of numbers. And also an array of positive numbers Arr[]. The goal is to find all the numbers that are divisible by all elements of Arr[] and are in the range [START,END] .

We will do this by traversing numbers from START to END and for each number we will check if the number is divisible by all elements of the array. If yes increment count.

We will find the LCM of all array elements and then check and count all numbers in range [START,END] that are fully divisible by that LCM.

Let’s understand with examples.

**Input**

START=1 END=20 Arr[]= { 2, 4, 8 }

**Output**

Numbers that are divisible by all array elements: 2

**Explanation**

Numbers 8 and 16 are in the range that are divisible by all array elements.

**Input**

START=100 END=200 Arr[]= { 230, 321, 490, 521 }

**Output**

Numbers that are divisible by all array elements: 0

**Explanation**

No number between 100 to 200 divisible by any array element.

We take an integers START and END as range variables.

Function divisiblebyArr(int start, int end, int arr[], int len) takes range variables and arrays and returns the count of numbers divisible by all array elements.

Take the initial variable count as 0 for such numbers.

Take variable flag as 0

Traverse range of numbers using for loop. i=start to i=end

Now for each number num=i, using while loop check if number is divisible by all array elements.

If all the elements fully divide num, set flag=1.

Outside while if flag=1 increment count

At the end of all loops count will have a total number which is divisible by all elements of the array.

Return the count as result.

#include <bits/stdc++.h> using namespace std; int divisiblebyArr(int start, int end, int arr[], int len){ int count = 0; int flag=0; int index=0; for (int i = start; i <= end; i++){ int num = i; index=0; while(index<len){ if(num % arr[index++] == 0) { flag=1; } else{ flag=0; break; } } if (flag == 1) { count++; } } return count; } int main(){ int START = 5, END = 20; int Arr[] = {2,4,8 }; int len=sizeof(Arr)/sizeof(Arr[0]); cout <<"Numbers that are divisible by all array elements: "<< divisiblebyArr(START,END,Arr,len); return 0; }

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

Numbers that are divisible by all array elements: 2

We take an integers START and END as range variables.

Function getLCM(int a, int b) takes two numbers and returns the LCM of them by finding the first number which is divisible by both using while loop.

Function getLCMArray(int arr[], int n) takes an array and its length as input and returns the LCM of all the elements of the array.

Calculate first LCM as getLCM(arr[0], arr[1]). After that consecutively find lcm of previous lcm and arr[i] by calling getLCM(lcm, arr[i]) where i=2 to i<n.

Function divisiblebyArr(int start, int end, int arr[], int len) takes range variables and arrays and returns the count of numbers divisible by all array elements.

Take the initial variable count as 0 for such numbers.

Take variable lcm as getLCMArray(int arr[], int len).

Traverse range of numbers using for loop. i=start to i=end

Now for each number i, check if it is divisible lcm. If true, increment count.

At the end of all loops count will have a total number which is divisible by all elements of the array.

Return the count as result.

#include <bits/stdc++.h> using namespace std; int getLCM(int a, int b){ int m; m = (a > b) ? a : b; while(true){ if(m % a == 0 && m % b == 0) return m; m++; } } int getLCMArray(int arr[], int n){ int lcm = getLCM(arr[0], arr[1]); for(int i = 2; i < n; i++){ lcm = getLCM(lcm, arr[i]); } return lcm; } int divisiblebyArr(int start, int end, int arr[], int len){ int count = 0; int flag=0; int lcm=getLCMArray(arr,len); for (int i = start; i <= end; i++){ if(i%lcm==0) { count++; } } return count; } int main(){ int START = 5, END = 20; int Arr[] = {2,4,8 }; int len=sizeof(Arr)/sizeof(Arr[0]); cout <<"Numbers that are divisible by all array elements: "<< divisiblebyArr(START,END,Arr,len); return 0; }

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

Numbers that are divisible by all array elements: 2

Advertisements