# Count of common multiples of two numbers in a range in C++

We are given two numbers A and B. Also provided two numbers START and END to define a range of numbers. The Ath tile has paint white and Bth tile has paint black. If the tile is painted both black and white then it turns grey. The goal is to find the total number of grey tiles .

We will do this by traversing numbers from START to END and for each number we will check if the number is multiple of both A and B. If yes increment count.

Let’s understand with examples.

Input

START=10 END=20 A=3 B=6

Output

Common multiples of A and B ( grey tiles ): 2

Explanation

Numbers 12, 18 are multiples of 3 and 6.

Input

START=1 END=100 A=10 B=11

Output

Common multiples of A and B ( grey tiles ): 0

Explanation

No common multiple of 10 and 11 in range.

## Approach used in the below program is as follows

• We take an integers START and END as range variables.

• We take A and B as two variables.

• Function countGrey(int start, int end, int a, int b) takes range variables, a, b and returns the count of multiples of a and b.

• Take the initial variable count as 0 for such numbers.

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

• If i%a==0 && i%b==0. Then ‘i’ is multiple of both a and b.

• At the end of all loops count will have a total numbers which are multiples of ‘a’ and ‘b’

• Return the count as result.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int countGrey(int start, int end, int a, int b){
int count = 0;
for (int i = start; i <= end; i++){
if(i%a==0 && i%b==0) //tile is grey
{ count++; }
}
return count;
}
int main(){
int START =10, END = 30;
int A=4, B=3;
cout <<"Common multiples of A and B ( grey tiles ): "<<
countGrey(START,END, A, B);
return 0;
}

## Output

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

Common multiples of A and B ( grey tiles ): 2