- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Count total divisors of A or B in a given range in C++

We are given four integers L, R, A and B. The goal is to find the count of numbers in range [L,R] that fully divide either A or B or both.

We will do this by traversing from L to R and for each number if number%A==0 or number%B==0 then increment count of divisors.

Let’s understand with examples.

**Input** − L=10, R=15, A=4, B=3

**Output** − Count of divisors of A or B − 2

**Explanation** −

Number 12 is fully divisible by 3 and 4. Number 15 is fully divisible by 3 only. Total divisors=2

**Input** − L=20, R=30, A=17, B=19

**Output** − Count of divisors of A or B − 0

**Explanation** − No number between 20 and 30 fully divisible by A or B or both.

## Approach used in the below program is as follows

We have taken four variables A, B, L and R.

Function countDivisors(int l, int r, int a, int b) takes all as input and returns the divisors of A or B or both that lie in range [L, R].

Take the initial count as 0.

Starting from i=L to i=R, if i%a==0 or i%b==0 increment count.

At the end of loop count as divisors of A or B.

Return count as result.

## Example

#include <bits/stdc++.h> using namespace std; int countDivisors(int l, int r, int a,int b){ int count = 0; for (int i = l; i <= r; i++){ if(i%a==0 || i%b==0) { count++ ; } } return count; } int main(){ int L=5; int R=15; int A=2; int B=5; cout <<endl<< "Total divisors of A and B : "<<countDivisors(L,R,A,B); return 0; }

## Output

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

Total divisors of A and B : 7

- Related Articles
- Program to find count of numbers having odd number of divisors in given range in C++
- Find numbers with K odd divisors in a given range in C++
- Count factorial numbers in a given range in C++
- C++ program to find numbers with K odd divisors in a given range
- Count all perfect divisors of a number in C++
- Count the number of common divisors of the given strings in C++
- Count BST nodes that lie in a given range in C++
- Count divisors of array multiplication in C++
- Count number of smallest elements in given range in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Finding the count of total upside down numbers in a range using JavaScript
- Bitwise OR (or - ) of a range in C++
- Check if count of divisors is even or odd in Python
- C Program to Check if count of divisors is even or odd?
- Find A and B from list of divisors in C++