# Find N numbers such that a number and its reverse are divisible by sum of its digits

Given a number **N**, the task is to print the first **N** numbers such that every number and the reverse of the number is divisible by its sum of digits.

**Example:**

Input:N = 4

Output:1 2 3 4

Explanation:

The reverse of every single digit number is the same number. And, every number is divisible by itself.

Input:N = 12

Output:1 2 3 4 5 6 7 8 9 10 12 18

**Approach:** The idea is to iterate through every number from 1 and compute the sum of the digits. For every such number, check if the number and the reverse of the number are divisible by the sum or not. Therefore, the following steps are followed to compute the answer:

- Initialize the counter to one and iterate through all the numbers one by one.
- For every number, find the reverse of the number.
- While finding the reverse of the number, compute the sum of the digits of the number.
- Now, check if the number and the reverse of the number are divisible by the sum of its digits.
- If it is, then increment the counter. Repeat the above steps until this counter is equal to
**N**.

Below is the implementation of the above approach:

## C++

`// C++ program to print the first N numbers ` `// such that every number and the reverse ` `// of the number is divisible by its ` `// sum of digits ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the sum of digits ` `int` `digit_sum(` `int` `n) ` `{ ` ` ` `int` `sum = 0, m; ` ` ` ` ` `// Loop to iterate through every ` ` ` `// digit of the number ` ` ` `while` `(n > 0) { ` ` ` `m = n % 10; ` ` ` `sum = sum + m; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `// Returning the sum of digits ` ` ` `return` `(sum); ` `} ` ` ` `// Function to calculate the reverse ` `// of a number ` `int` `reverse(` `int` `n) ` `{ ` ` ` `int` `r = 0; ` ` ` ` ` `// Loop to calculate the reverse ` ` ` `// of the number ` ` ` `while` `(n != 0) { ` ` ` `r = r * 10; ` ` ` `r = r + n % 10; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `// Return the reverse of the ` ` ` `// number ` ` ` `return` `(r); ` `} ` ` ` `// Function to print the first N numbers ` `// such that every number and the reverse ` `// of the number is divisible by its ` `// sum of digits ` `void` `operation(` `int` `n) ` `{ ` ` ` `int` `i = 1, a, count = 0, r; ` ` ` ` ` `// Loop to continuously check and ` ` ` `// generate number until there ` ` ` `// are n outputs ` ` ` `while` `(count < n) { ` ` ` ` ` `// Variable to hold the sum of ` ` ` `// the digit of the number ` ` ` `a = digit_sum(i); ` ` ` ` ` `// Computing the reverse of the ` ` ` `// number ` ` ` `r = reverse(i); ` ` ` ` ` `// Checking if the condition satisfies. ` ` ` `// Increment the count and print the ` ` ` `// number if it satisfies. ` ` ` `if` `(i % a == 0 && r % a == 0) { ` ` ` `cout << i << ` `" "` `; ` ` ` `count++; ` ` ` `i++; ` ` ` `} ` ` ` `else` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `operation(n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print the first N numbers ` `// such that every number and the reverse ` `// of the number is divisible by its ` `// sum of digits ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to calculate the sum of digits ` `static` `int` `digit_sum(` `int` `n) ` `{ ` ` ` `int` `sum = ` `0` `, m; ` ` ` ` ` `// Loop to iterate through ` ` ` `// every digit of the number ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `m = n % ` `10` `; ` ` ` `sum = sum + m; ` ` ` `n = n / ` `10` `; ` ` ` `} ` ` ` ` ` `// Returning the sum of digits ` ` ` `return` `(sum); ` `} ` ` ` `// Function to calculate the ` `// reverse of a number ` `static` `int` `reverse(` `int` `n) ` `{ ` ` ` `int` `r = ` `0` `; ` ` ` ` ` `// Loop to calculate the ` ` ` `// reverse of the number ` ` ` `while` `(n != ` `0` `) ` ` ` `{ ` ` ` `r = r * ` `10` `; ` ` ` `r = r + n % ` `10` `; ` ` ` `n = n / ` `10` `; ` ` ` `} ` ` ` ` ` `// Return the reverse ` ` ` `// of the number ` ` ` `return` `(r); ` `} ` ` ` `// Function to print the first N numbers ` `// such that every number and the reverse ` `// of the number is divisible by its ` `// sum of digits ` `static` `void` `operation(` `int` `n) ` `{ ` ` ` `int` `i = ` `1` `, a, count = ` `0` `, r; ` ` ` ` ` `// Loop to continuously check and ` ` ` `// generate number until there ` ` ` `// are n outputs ` ` ` `while` `(count < n) ` ` ` `{ ` ` ` ` ` `// Variable to hold the sum ` ` ` `// of the digit of the number ` ` ` `a = digit_sum(i); ` ` ` ` ` `// Computing the reverse of the ` ` ` `// number ` ` ` `r = reverse(i); ` ` ` ` ` `// Checking if the condition satisfies. ` ` ` `// Increment the count and print the ` ` ` `// number if it satisfies. ` ` ` `if` `(i % a == ` `0` `&& r % a == ` `0` `) ` ` ` `{ ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `count++; ` ` ` `i++; ` ` ` `} ` ` ` `else` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` ` ` `operation(n); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Pyhton3 program to print the first N numbers ` `# such that every number and the reverse ` `# of the number is divisible by its ` `# sum of digits ` ` ` `# Function to calculate the sum of digits ` `def` `digit_sum(n): ` ` ` `sum` `=` `0` ` ` ` ` `# Loop to iterate through every ` ` ` `# digit of the number ` ` ` `while` `(n > ` `0` `): ` ` ` `m ` `=` `n ` `%` `10` `; ` ` ` `sum` `=` `sum` `+` `m; ` ` ` `n ` `=` `n ` `/` `/` `10` ` ` ` ` `# Returning the sum of digits ` ` ` `return` `(` `sum` `) ` ` ` `# Function to calculate the reverse ` `# of a number ` `def` `reverse(n): ` ` ` `r ` `=` `0` ` ` ` ` `# Loop to calculate the reverse ` ` ` `# of the number ` ` ` `while` `(n !` `=` `0` `): ` ` ` `r ` `=` `r ` `*` `10` ` ` `r ` `=` `r ` `+` `n ` `%` `10` ` ` `n ` `=` `n ` `/` `/` `10` ` ` ` ` `# Return the reverse of the ` ` ` `# number ` ` ` `return` `(r) ` ` ` `# Function to print the first N numbers ` `# such that every number and the reverse ` `# of the number is divisible by its ` `# sum of digits ` `def` `operation(n): ` ` ` `i ` `=` `1` ` ` `count ` `=` `0` ` ` ` ` `# Loop to continuously check and ` ` ` `# generate number until there ` ` ` `# are n outputs ` ` ` `while` `(count < n): ` ` ` ` ` `# Variable to hold the sum of ` ` ` `# the digit of the number ` ` ` `a ` `=` `digit_sum(i) ` ` ` ` ` `# Computing the reverse of the ` ` ` `# number ` ` ` `r ` `=` `reverse(i) ` ` ` ` ` `# Checking if the condition satisfies. ` ` ` `# Increment the count and print the ` ` ` `# number if it satisfies. ` ` ` `if` `(i ` `%` `a ` `=` `=` `0` `and` `r ` `%` `a ` `=` `=` `0` `): ` ` ` ` ` `print` `(i, end ` `=` `" "` `) ` ` ` `count ` `+` `=` `1` ` ` `i ` `+` `=` `1` ` ` `else` `: ` ` ` `i ` `+` `=` `1` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `10` ` ` `operation(n) ` ` ` `# This code is contributed by Samarth ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print the first N numbers ` `// such that every number and the reverse ` `// of the number is divisible by its ` `// sum of digits ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to calculate the sum of digits ` `static` `int` `digit_sum(` `int` `n) ` `{ ` ` ` `int` `sum = 0, m; ` ` ` ` ` `// Loop to iterate through ` ` ` `// every digit of the number ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `m = n % 10; ` ` ` `sum = sum + m; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `// Returning the sum of digits ` ` ` `return` `(sum); ` `} ` ` ` `// Function to calculate the ` `// reverse of a number ` `static` `int` `reverse(` `int` `n) ` `{ ` ` ` `int` `r = 0; ` ` ` ` ` `// Loop to calculate the ` ` ` `// reverse of the number ` ` ` `while` `(n != 0) ` ` ` `{ ` ` ` `r = r * 10; ` ` ` `r = r + n % 10; ` ` ` `n = n / 10; ` ` ` `} ` ` ` ` ` `// Return the reverse ` ` ` `// of the number ` ` ` `return` `(r); ` `} ` ` ` `// Function to print the first N numbers ` `// such that every number and the reverse ` `// of the number is divisible by its ` `// sum of digits ` `static` `void` `operation(` `int` `n) ` `{ ` ` ` `int` `i = 1, a, count = 0, r; ` ` ` ` ` `// Loop to continuously check and ` ` ` `// generate number until there ` ` ` `// are n outputs ` ` ` `while` `(count < n) ` ` ` `{ ` ` ` ` ` `// Variable to hold the sum ` ` ` `// of the digit of the number ` ` ` `a = digit_sum(i); ` ` ` ` ` `// Computing the reverse of the ` ` ` `// number ` ` ` `r = reverse(i); ` ` ` ` ` `// Checking if the condition satisfies. ` ` ` `// Increment the count and print the ` ` ` `// number if it satisfies. ` ` ` `if` `(i % a == 0 && r % a == 0) ` ` ` `{ ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `count++; ` ` ` `i++; ` ` ` `} ` ` ` `else` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 10; ` ` ` ` ` `operation(n); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 5 6 7 8 9 10

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Find N digits number which is divisible by D
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Find all the prime numbers of given number of digits
- Find all Autobiographical Numbers with given number of digits
- Check if the sum of digits of number is divisible by all of its digits
- Count the numbers with N digits and whose suffix is divisible by K
- Count numbers in range L-R that are divisible by all of its non-zero digits
- N digit numbers divisible by 5 formed from the M digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Write a program to reverse digits of a number
- Smallest number with sum of digits as N and divisible by 10^N
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Number of digits to be removed to make a number divisible by 3
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Program to check if a number is divisible by sum of its digits

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.