# Count all x divisible number in a range

To count all the numbers divisible by a given number 'x' within a range of numbers, you can follow the steps below:

Determine the starting and ending points of the range.

Determine the value of 'x'.

Divide the starting point by 'x' and round up to the nearest integer. This will give you the smallest integer value that is divisible by 'x' and falls within the range.

Divide the ending point by 'x' and round down to the nearest integer. This will give you the largest integer value that is divisible by 'x' and falls within the range.

Subtract the value obtained in step 3 from the value obtained in step 4.

Add 1 to the result obtained in step 5 to account for the first integer value divisible by 'x' that was rounded up in step 3.

The final result will be the count of all the numbers divisible by 'x' in the given range.

Here is an example:

Suppose you want to count all the numbers divisible by 5 in the range [10, 30].

The starting point is 10 and the ending point is 30.

The value of 'x' is 5.

10 divided by 5 is 2, which when rounded up to the nearest integer, is still 2.

30 divided by 5 is 6, which when rounded down to the nearest integer, is also 6.

6 - 2 = 4.

4 + 1 = 5.

Therefore, there are 5 numbers divisible by 5 in the range [10, 30].

```
public class CountDivisibleNumbersInRange
{
public static int countDivisible(int x, int start, int ends)
{
if(x == 0)
{
return (ends-start) + 1;
}
if (x == ends)
{
return 1;
}
int a = start;
int b = ends;
// Find first nearest divisible number to x
while (a <= ends)
{
if ((a % x) == 0)
{
break;
}
a++;
}
// Find last nearest divisible number to x
while (b > a)
{
if ((b % x) == 0)
{
break;
}
b--;
}
return ((b - a) / x) + 1;
}
public static void main(String[] args)
{
// Test case A
// When x = 4;
// start = 2
// ends = 38
System.out.println(countDivisible(4, 2, 38));
// Test case B
// When x = 3;
// start = 1
// ends = 10
System.out.println(countDivisible(3, 1, 10));
// Other test
System.out.println(countDivisible(13, 21, 33));
System.out.println(countDivisible(7, 7, 29));
System.out.println(countDivisible(2, 0, 0));
System.out.println(countDivisible(6, 50, 120));
}
}
```

Other simple approach.

```
// C program
// Count all x divisible number in a range
#include <stdio.h>
// Count the divisible number in the given range
void count_divisible(int start, int last, int x)
{
if (start > last)
{
//Change sequence
count_divisible(last, start, x);
return;
}
else
{
int num = start;
int counter = 0;
printf("\n Divisible by %d in range of [%d-%d] are : [", x, start, last);
while (num <= last)
{
if (num % x == 0)
{
// When x, is divisible by num
printf(" %d ", num);
//When get divisible number then increase counter value
counter++;
}
if (counter > 0)
{
//visit to next divisible number
num += x;
}
else
{
num++;
}
}
// Display calculated result
printf("]\n Result : %d\n", counter);
}
}
int main()
{
//Test case
int x = 4;
count_divisible(2, 38, x);
x = 3;
count_divisible(1, 10, x);
x = 13;
count_divisible(21, 33, x);
x = 7;
count_divisible(7, 29, x);
x = 2;
count_divisible(0, 0, x);
x = 6;
count_divisible(50, 120, x);
return 0;
}
```

#### Output

```
Divisible by 4 in range of [2-38] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [1-10] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [21-33] are : [ 26 ]
Result : 1
Divisible by 7 in range of [7-29] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [0-0] are : [ 0 ]
Result : 1
Divisible by 6 in range of [50-120] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
/*
Java program
Count all x divisible number in a range
*/
class DivisibleNumbers
{
// Count the divisible number in the given range
public void count_divisible(int start, int last, int x)
{
if (start > last)
{
//Change sequence
count_divisible(last, start, x);
return;
}
else
{
int num = start;
int counter = 0;
System.out.print("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
while (num <= last)
{
if (num % x == 0)
{
// When x, is divisible by num
System.out.print(" " + num + " ");
//When get divisible number then increase counter value
counter++;
}
if (counter > 0)
{
//visit to next divisible number
num += x;
}
else
{
num++;
}
}
// Display calculated result
System.out.print("]\n Result : " + counter + "\n");
}
}
public static void main(String[] args)
{
DivisibleNumbers obj = new DivisibleNumbers();
//Test case
int x = 4;
obj.count_divisible(2, 38, x);
x = 3;
obj.count_divisible(1, 10, x);
x = 13;
obj.count_divisible(21, 33, x);
x = 7;
obj.count_divisible(7, 29, x);
x = 2;
obj.count_divisible(0, 0, x);
x = 6;
obj.count_divisible(50, 120, x);
}
}
```

#### Output

```
Divisible by 4 in range of [2-38] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [1-10] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [21-33] are : [ 26 ]
Result : 1
Divisible by 7 in range of [7-29] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [0-0] are : [ 0 ]
Result : 1
Divisible by 6 in range of [50-120] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
//Include header file
#include <iostream>
using namespace std;
/*
C++ program
Count all x divisible number in a range
*/
class DivisibleNumbers
{
public:
// Count the divisible number in the given range
void count_divisible(int start, int last, int x)
{
if (start > last)
{
//Change sequence
this->count_divisible(last, start, x);
return;
}
else
{
int num = start;
int counter = 0;
cout << "\n Divisible by " << x << " in range of [" << start << "-" << last << "] are : [";
while (num <= last)
{
if (num % x == 0)
{
// When x, is divisible by num
cout << " " << num << " ";
//When get divisible number then increase counter value
counter++;
}
if (counter > 0)
{
//visit to next divisible number
num += x;
}
else
{
num++;
}
}
// Display calculated result
cout << "]\n Result : " << counter << "\n";
}
}
};
int main()
{
DivisibleNumbers obj = DivisibleNumbers();
//Test case
int x = 4;
obj.count_divisible(2, 38, x);
x = 3;
obj.count_divisible(1, 10, x);
x = 13;
obj.count_divisible(21, 33, x);
x = 7;
obj.count_divisible(7, 29, x);
x = 2;
obj.count_divisible(0, 0, x);
x = 6;
obj.count_divisible(50, 120, x);
return 0;
}
```

#### Output

```
Divisible by 4 in range of [2-38] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [1-10] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [21-33] are : [ 26 ]
Result : 1
Divisible by 7 in range of [7-29] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [0-0] are : [ 0 ]
Result : 1
Divisible by 6 in range of [50-120] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
//Include namespace system
using System;
/*
C# program
Count all x divisible number in a range
*/
class DivisibleNumbers
{
// Count the divisible number in the given range
public void count_divisible(int start, int last, int x)
{
if (start > last)
{
//Change sequence
count_divisible(last, start, x);
return;
}
else
{
int num = start;
int counter = 0;
Console.Write("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
while (num <= last)
{
if (num % x == 0)
{
// When x, is divisible by num
Console.Write(" " + num + " ");
//When get divisible number then increase counter value
counter++;
}
if (counter > 0)
{
//visit to next divisible number
num += x;
}
else
{
num++;
}
}
// Display calculated result
Console.Write("]\n Result : " + counter + "\n");
}
}
public static void Main(String[] args)
{
DivisibleNumbers obj = new DivisibleNumbers();
//Test case
int x = 4;
obj.count_divisible(2, 38, x);
x = 3;
obj.count_divisible(1, 10, x);
x = 13;
obj.count_divisible(21, 33, x);
x = 7;
obj.count_divisible(7, 29, x);
x = 2;
obj.count_divisible(0, 0, x);
x = 6;
obj.count_divisible(50, 120, x);
}
}
```

#### Output

```
Divisible by 4 in range of [2-38] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [1-10] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [21-33] are : [ 26 ]
Result : 1
Divisible by 7 in range of [7-29] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [0-0] are : [ 0 ]
Result : 1
Divisible by 6 in range of [50-120] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
<?php
/*
Php program
Count all x divisible number in a range
*/
class DivisibleNumbers
{
// Count the divisible number in the given range
public function count_divisible($start, $last, $x)
{
if ($start > $last)
{
//Change sequence
$this->count_divisible($last, $start, $x);
return;
}
else
{
$num = $start;
$counter = 0;
echo "\n Divisible by ". $x ." in range of [". $start ."-". $last ."] are : [";
while ($num <= $last)
{
if ($num % $x == 0)
{
// When x, is divisible by num
echo " ". $num ." ";
//When get divisible number then increase counter value
$counter++;
}
if ($counter > 0)
{
//visit to next divisible number
$num += $x;
}
else
{
$num++;
}
}
// Display calculated result
echo "]\n Result : ". $counter ."\n";
}
}
}
function main()
{
$obj = new DivisibleNumbers();
//Test case
$x = 4;
$obj->count_divisible(2, 38, $x);
$x = 3;
$obj->count_divisible(1, 10, $x);
$x = 13;
$obj->count_divisible(21, 33, $x);
$x = 7;
$obj->count_divisible(7, 29, $x);
$x = 2;
$obj->count_divisible(0, 0, $x);
$x = 6;
$obj->count_divisible(50, 120, $x);
}
main();
```

#### Output

```
Divisible by 4 in range of [2-38] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [1-10] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [21-33] are : [ 26 ]
Result : 1
Divisible by 7 in range of [7-29] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [0-0] are : [ 0 ]
Result : 1
Divisible by 6 in range of [50-120] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
/*
Node Js program
Count all x divisible number in a range
*/
class DivisibleNumbers
{
// Count the divisible number in the given range
count_divisible(start, last, x)
{
if (start > last)
{
//Change sequence
this.count_divisible(last, start, x);
return;
}
else
{
var num = start;
var counter = 0;
process.stdout.write("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
while (num <= last)
{
if (num % x == 0)
{
// When x, is divisible by num
process.stdout.write(" " + num + " ");
//When get divisible number then increase counter value
counter++;
}
if (counter > 0)
{
//visit to next divisible number
num += x;
}
else
{
num++;
}
}
// Display calculated result
process.stdout.write("]\n Result : " + counter + "\n");
}
}
}
function main()
{
var obj = new DivisibleNumbers();
//Test case
var x = 4;
obj.count_divisible(2, 38, x);
x = 3;
obj.count_divisible(1, 10, x);
x = 13;
obj.count_divisible(21, 33, x);
x = 7;
obj.count_divisible(7, 29, x);
x = 2;
obj.count_divisible(0, 0, x);
x = 6;
obj.count_divisible(50, 120, x);
}
main();
```

#### Output

```
Divisible by 4 in range of [2-38] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [1-10] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [21-33] are : [ 26 ]
Result : 1
Divisible by 7 in range of [7-29] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [0-0] are : [ 0 ]
Result : 1
Divisible by 6 in range of [50-120] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
# Python 3 program
# Count all x divisible number in a range
class DivisibleNumbers :
# Count the divisible number in the given range
def count_divisible(self, start, last, x) :
if (start > last) :
# Change sequence
self.count_divisible(last, start, x)
return
else :
num = start
counter = 0
print("\n Divisible by ", x ," in range of [", start ,"-", last ,"] are : [", end = "")
while (num <= last) :
if (num % x == 0) :
# When x, is divisible by num
print(" ", num ," ", end = "")
# When get divisible number then increase counter value
counter += 1
if (counter > 0) :
# visit to next divisible number
num += x
else :
num += 1
# Display calculated result
print("]\n Result : ", counter ,"\n", end = "")
def main() :
obj = DivisibleNumbers()
# Test case
x = 4
obj.count_divisible(2, 38, x)
x = 3
obj.count_divisible(1, 10, x)
x = 13
obj.count_divisible(21, 33, x)
x = 7
obj.count_divisible(7, 29, x)
x = 2
obj.count_divisible(0, 0, x)
x = 6
obj.count_divisible(50, 120, x)
if __name__ == "__main__": main()
```

#### Output

```
Divisible by 4 in range of [ 2 - 38 ] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [ 1 - 10 ] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [ 21 - 33 ] are : [ 26 ]
Result : 1
Divisible by 7 in range of [ 7 - 29 ] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [ 0 - 0 ] are : [ 0 ]
Result : 1
Divisible by 6 in range of [ 50 - 120 ] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
# Ruby program
# Count all x divisible number in a range
class DivisibleNumbers
# Count the divisible number in the given range
def count_divisible(start, last, x)
if (start > last)
# Change sequence
self.count_divisible(last, start, x)
return
else
num = start
counter = 0
print("\n Divisible by ", x ," in range of [", start ,"-", last ,"] are : [")
while (num <= last)
if (num % x == 0)
# When x, is divisible by num
print(" ", num ," ")
# When get divisible number then increase counter value
counter += 1
end
if (counter > 0)
# visit to next divisible number
num += x
else
num += 1
end
end
# Display calculated result
print("]\n Result : ", counter ,"\n")
end
end
end
def main()
obj = DivisibleNumbers.new()
# Test case
x = 4
obj.count_divisible(2, 38, x)
x = 3
obj.count_divisible(1, 10, x)
x = 13
obj.count_divisible(21, 33, x)
x = 7
obj.count_divisible(7, 29, x)
x = 2
obj.count_divisible(0, 0, x)
x = 6
obj.count_divisible(50, 120, x)
end
main()
```

#### Output

```
Divisible by 4 in range of [2-38] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [1-10] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [21-33] are : [ 26 ]
Result : 1
Divisible by 7 in range of [7-29] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [0-0] are : [ 0 ]
Result : 1
Divisible by 6 in range of [50-120] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
/*
Scala program
Count all x divisible number in a range
*/
class DivisibleNumbers
{
// Count the divisible number in the given range
def count_divisible(start: Int, last: Int, x: Int): Unit = {
if (start > last)
{
//Change sequence
count_divisible(last, start, x);
return;
}
else
{
var num: Int = start;
var counter: Int = 0;
print("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
while (num <= last)
{
if (num % x == 0)
{
// When x, is divisible by num
print(" " + num + " ");
//When get divisible number then increase counter value
counter += 1;
}
if (counter > 0)
{
//visit to next divisible number
num += x;
}
else
{
num += 1;
}
}
// Display calculated result
print("]\n Result : " + counter + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: DivisibleNumbers = new DivisibleNumbers();
//Test case
var x: Int = 4;
obj.count_divisible(2, 38, x);
x = 3;
obj.count_divisible(1, 10, x);
x = 13;
obj.count_divisible(21, 33, x);
x = 7;
obj.count_divisible(7, 29, x);
x = 2;
obj.count_divisible(0, 0, x);
x = 6;
obj.count_divisible(50, 120, x);
}
}
```

#### Output

```
Divisible by 4 in range of [2-38] are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [1-10] are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [21-33] are : [ 26 ]
Result : 1
Divisible by 7 in range of [7-29] are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [0-0] are : [ 0 ]
Result : 1
Divisible by 6 in range of [50-120] are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

```
/*
Swift 4 program
Count all x divisible number in a range
*/
class DivisibleNumbers
{
// Count the divisible number in the given range
func count_divisible(_ start: Int, _ last: Int, _ x: Int)
{
if (start > last)
{
//Change sequence
self.count_divisible(last, start, x);
return;
}
else
{
var num: Int = start;
var counter: Int = 0;
print("\n Divisible by ", x ," in range of [", start ,"-", last ,"]are : [", terminator: "");
while (num <= last)
{
if (num % x == 0)
{
// When x, is divisible by num
print(" ", num ," ", terminator: "");
//When get divisible number then increase counter value
counter += 1;
}
if (counter > 0)
{
//visit to next divisible number
num += x;
}
else
{
num += 1;
}
}
// Display calculated result
print("]\n Result : ", counter ,"\n", terminator: "");
}
}
}
func main()
{
let obj: DivisibleNumbers = DivisibleNumbers();
//Test case
var x: Int = 4;
obj.count_divisible(2, 38, x);
x = 3;
obj.count_divisible(1, 10, x);
x = 13;
obj.count_divisible(21, 33, x);
x = 7;
obj.count_divisible(7, 29, x);
x = 2;
obj.count_divisible(0, 0, x);
x = 6;
obj.count_divisible(50, 120, x);
}
main();
```

#### Output

```
Divisible by 4 in range of [ 2 - 38 ]are : [ 4 8 12 16 20 24 28 32 36 ]
Result : 9
Divisible by 3 in range of [ 1 - 10 ]are : [ 3 6 9 ]
Result : 3
Divisible by 13 in range of [ 21 - 33 ]are : [ 26 ]
Result : 1
Divisible by 7 in range of [ 7 - 29 ]are : [ 7 14 21 28 ]
Result : 4
Divisible by 2 in range of [ 0 - 0 ]are : [ 0 ]
Result : 1
Divisible by 6 in range of [ 50 - 120 ]are : [ 54 60 66 72 78 84 90 96 102 108 114 120 ]
Result : 12
```

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

## New Comment