- Related Questions & Answers
- Count common prime factors of two numbers in C++
- Sum of the multiples of two numbers below N in C++
- N-th multiple in sorted list of multiples of two numbers in C++
- C++ Program for Common Divisors of Two Numbers?
- Finding the least common multiple of a range of numbers in JavaScript?
- Position of rightmost common bit in two numbers in C++
- C++ Program for the Common Divisors of Two Numbers?
- Count Unary Numbers in a Range in C++
- Count factorial numbers in a given range in C++
- Find count of common nodes in two Doubly Linked Lists in C++
- Count of Range Sum in C++
- Print the kth common factor of two numbers
- Python Program for Common Divisors of Two Numbers
- Java Program for Common Divisors of Two Numbers
- Count common subsequence in two strings 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 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.

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.

#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; }

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

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

Advertisements