- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 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.

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.

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

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

Total divisors of A and B : 7

- Related Questions & Answers
- Larger of a^b or b^a in C++
- 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++
- Find A and B from list of divisors 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++
- Bitwise OR (or - ) of a range in C++
- Count number of triplets in an array having sum in the range [a,b] in C++
- Finding the count of total upside down numbers in a range using JavaScript
- Count the number of common divisors of the given strings in C++
- Count BST nodes that lie in a given range in C++
- Count of a, b & c after n seconds for given reproduction rate in C++
- Count number of smallest elements in given range in C++
- Count total bits in a number in C++

Advertisements