- 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 three numbers A,B and M. A and B define the range [A,B] of numbers.The goal is to count numbers between A and B that are divisible by M.

We will start from i=A till first multiple of M. Increment count if i%M=0. Now increment i till i<=B and increase count.

Let’s understand with examples.

A=11,B=20, M=5

Count of numbers divisible by M in given range: 2

15 and 20 are only numbers that are divisible by 5 and lie in range [11,20].

A=20, B=50, M=11

Count of numbers divisible by M in given range: 3

22,33,44 are only numbers that are divisible by 11 and lie in range [20,50].

- We take A,B and M as integers.
- Function divisiblebyM (int a, int b, int m) take A,B and M as parameters and return the count of numbers between A and B that are divisible by M.
- Take the initial count as 0.
- Using for loop, start from i=A to i=B. Increment i by 1.
- If i%m=0, increment count.
- At the end, count as numbers between A and B that are divisible by m.
- Return count as result.

// Program to count the numbers divisible by // M in a given range #include <bits/stdc++.h> using namespace std; int divisiblebyM(int a, int b, int m){ int count = 0; // Running a loop from A to B and check // if a number is divisible by M. for (int i = a; i <= b;i++ ){ if (i % m == 0){ count++; } } return count; } int main(){ // A and B define the range, M is the dividend int A = 3, B = 15, M = 4; cout<<"Numbers divisible by M in given range:"<<divisiblebyM(A, B, M) << endl; return 0; }

Numbers divisible by M in given range:3

- Related Questions & Answers
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Count n digit numbers divisible by given number in C++
- Count numbers in a range that are divisible by all array elements in C++
- Count factorial numbers in a given range in C++
- Finding the count of numbers divisible by a number within a range using JavaScript
- Python Program to Print all Numbers in a Range Divisible by a Given Number
- Golang Program to Print all Numbers in a Range Divisible by a Given Number
- N digit numbers divisible by 5 formed from the M digits in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Find (a^b)%m where ‘a’ is very large in C++
- Count of m digit integers that are divisible by an integer n in C++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++

Advertisements