Given two number l and r. Count the total numbers between l and r which when subtracted from their respective reverse, the difference is a product of k.

**Examples:**

Input : 20 23 6 Output : 2 20 and 22 are the two numbers. |20-2| = 18 which is a product of 6 |22-22| = 0 which is a product of 6 Input : 35 45 5 Output : 2 38 and 44 are the two numbers. |38-83| = 45 which is a product of 5 |44-44| = 0 which is a product of 5

**Approach:** For each number between the given range check if the difference between the number and its reverse number is divisible by k, increment count if yes.

## C++

`// C++ program to Count the numbers ` `// within a given range in which when ` `// you subtract a number from its ` `// reverse, the difference is a product ` `// of k ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `bool` `isRevDiffDivisible(` `int` `x, ` `int` `k) ` `{ ` ` ` `// function to check if the number ` ` ` `// and its reverse have their ` ` ` `// absolute difference divisible by k ` ` ` `int` `n = x; ` ` ` `int` `m = 0; ` ` ` `int` `flag; ` ` ` `while` `(x > 0) ` ` ` `{ ` ` ` `// reverse the number ` ` ` `m = m * 10 + x % 10; ` ` ` `x /= 10; ` ` ` `} ` ` ` ` ` `return` `(` `abs` `(n - m) % k == 0); ` `} ` ` ` `int` `countNumbers(` `int` `l, ` `int` `r, ` `int` `k) ` `{ ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = l; i <= r; i++) ` ` ` `if` `(isRevDiffDivisible(i, k)) ` ` ` `++count; ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 20, r = 23, k = 6; ` ` ` `cout << countNumbers(l, r, k) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to Count the ` `// numbers within a given range ` `// in which when you subtract ` `// a number from its reverse, ` `// the difference is a product of k ` `import` `java.io.*; ` `import` `java.math.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `boolean` `isRevDiffDivisible(` `int` `x, ` `int` `k) ` ` ` `{ ` ` ` `// function to check if the number ` ` ` `// and its reverse have their ` ` ` `// absolute difference divisible by k ` ` ` `int` `n = x; ` ` ` `int` `m = ` `0` `; ` ` ` `int` `flag; ` ` ` `while` `(x > ` `0` `) ` ` ` `{ ` ` ` `// reverse the number ` ` ` `m = m * ` `10` `+ x % ` `10` `; ` ` ` `x /= ` `10` `; ` ` ` `} ` ` ` ` ` `return` `(Math.abs(n - m) % k == ` `0` `); ` ` ` `} ` ` ` ` ` `static` `int` `countNumbers(` `int` `l, ` `int` `r, ` `int` `k) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = l; i <= r; i++) ` ` ` `if` `(isRevDiffDivisible(i, k)) ` ` ` `++count; ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `l = ` `35` `, r = ` `45` `, k = ` `5` `; ` ` ` `System.out.println(countNumbers(l, r, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to Count the numbers ` `# within a given range in which when you ` `# subtract a number from its reverse, ` `# the difference is a product of k ` ` ` `def` `isRevDiffDivisible(x, k) : ` ` ` `# function to check if the number ` ` ` `# and its reverse have their ` ` ` `# absolute difference divisible by k ` ` ` `n ` `=` `x; m ` `=` `0` ` ` `while` `(x > ` `0` `) : ` ` ` ` ` `# Reverse the number ` ` ` `m ` `=` `m ` `*` `10` `+` `x ` `%` `10` ` ` `x ` `=` `x ` `/` `/` `10` ` ` ` ` `return` `(` `abs` `(n ` `-` `m) ` `%` `k ` `=` `=` `0` `) ` ` ` `def` `countNumbers(l, r, k) : ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(l, r ` `+` `1` `) : ` ` ` ` ` `if` `(isRevDiffDivisible(i, k)) : ` ` ` `count ` `=` `count ` `+` `1` ` ` ` ` `return` `count ` ` ` ` ` `# Driver code ` `l ` `=` `20` `; r ` `=` `23` `; k ` `=` `6` `print` `(countNumbers(l, r, k)) ` ` ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to Count the ` `// numbers within a given range ` `// in which when you subtract ` `// a number from its reverse, ` `// the difference is a product of k ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `bool` `isRevDiffDivisible(` `int` `x, ` `int` `k) ` ` ` `{ ` ` ` `// function to check if the number ` ` ` `// and its reverse have their ` ` ` `// absolute difference divisible by k ` ` ` `int` `n = x; ` ` ` `int` `m = 0; ` ` ` ` ` `// int flag; ` ` ` `while` `(x > 0) ` ` ` `{ ` ` ` `// reverse the number ` ` ` `m = m * 10 + x % 10; ` ` ` `x /= 10; ` ` ` `} ` ` ` ` ` `return` `(Math.Abs(n - m) % k == 0); ` ` ` `} ` ` ` ` ` `static` `int` `countNumbers(` `int` `l, ` `int` `r, ` `int` `k) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `i = l; i <= r; i++) ` ` ` `if` `(isRevDiffDivisible(i, k)) ` ` ` `++count; ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `l = 35, r = 45, k = 5; ` ` ` `Console.WriteLine(countNumbers(l, r, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed ` `// by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to Count the ` `// numbers within a given ` `// range in which when you ` `// subtract a number from ` `// its reverse, the difference ` `// is a product of k ` `function` `isRevDiffDivisible(` `$x` `, ` `$k` `) ` `{ ` ` ` `// function to check if ` ` ` `// the number and its ` ` ` `// reverse have their ` ` ` `// absolute difference ` ` ` `// divisible by k ` ` ` `$n` `= ` `$x` `; ` ` ` `$m` `= 0; ` ` ` `$flag` `; ` ` ` `while` `(` `$x` `> 0) ` ` ` `{ ` ` ` `// reverse the number ` ` ` `$m` `= ` `$m` `* 10 + ` `$x` `% 10; ` ` ` `$x` `= (int)` `$x` `/ 10; ` ` ` `} ` ` ` ` ` `return` `(` `abs` `(` `$n` `- ` `$m` `) % ` ` ` `$k` `== 0); ` `} ` ` ` `function` `countNumbers(` `$l` `, ` `$r` `, ` `$k` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `for` `(` `$i` `= ` `$l` `; ` `$i` `<= ` `$r` `; ` `$i` `++) ` ` ` `if` `(isRevDiffDivisible(` `$i` `, ` `$k` `)) ` ` ` `++` `$count` `; ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$l` `= 20; ` `$r` `= 23; ` `$k` `= 6; ` `echo` `countNumbers(` `$l` `, ` `$r` `, ` `$k` `); ` ` ` `// This code is contributed by mits. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Counting numbers of n digits that are monotone
- First string from the given array whose reverse is also present in the same array
- Check if the product of digit sum and its reverse equals the number or not
- Program to print numbers from N to 1 in reverse order
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Count natural numbers whose all permutation are greater than that number
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count all prime numbers in a given range whose sum of digits is also prime
- Find two numbers whose sum is N and does not contain any digit as K
- Count of N digit Numbers whose sum of every K consecutive digits is equal
- Count of N digit Numbers whose sum of every K consecutive digits is equal | Set 2
- Count all N digit numbers whose digits are multiple of X
- First digit in product of an array of numbers
- Number of digits in the product of two numbers
- Find last k digits in product of an array numbers
- Find the maximum sum of digits of the product of two numbers
- Largest palindrome which is product of two N-digit numbers : Set 2
- Cumulative product of digits of all numbers in the given range

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.