- 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

# Count the numbers divisible by ‘M’ in a given range in C++

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.

## Input

A=11,B=20, M=5

## Output

Count of numbers divisible by M in given range: 2

## Explanation

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

## Input

A=20, B=50, M=11

## Output

Count of numbers divisible by M in given range: 3

## Explanation

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

## Approach used in the below program is as follows

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

## Example

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

## Output

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++
- Finding the count of numbers divisible by a number within a range using JavaScript
- Count numbers in a range that are divisible by all array elements in C++
- 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
- Count factorial numbers in a given range in C++
- 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++
- Golang Program to Find the Numbers which are Divisible by 7 and Multiple of 5 in a Given Range
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M 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++
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++

Advertisements