Find N numbers such that a number and its reverse are divisible by sum of its digits
Last Updated :
15 Jul, 2025
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);
}
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
Python3
# Python3 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
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
JavaScript
<script>
// Javascript 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
function digit_sum(n)
{
let sum = 0, m;
// Loop to iterate through every
// digit of the number
while (n > 0) {
m = n % 10;
sum = sum + m;
n = parseInt(n / 10, 10);
}
// Returning the sum of digits
return (sum);
}
// Function to calculate the reverse
// of a number
function reverse(n)
{
let r = 0;
// Loop to calculate the reverse
// of the number
while (n != 0) {
r = r * 10;
r = r + n % 10;
n = parseInt(n / 10, 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
function operation(n)
{
let 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) {
document.write(i + " ");
count++;
i++;
}
else
i++;
}
}
let n = 10;
operation(n);
</script>
Output: 1 2 3 4 5 6 7 8 9 10
Time Complexity: O(n * log10n)
Auxiliary Space: O(1)
Similar Reads
Find a N-digit number such that it is not divisible by any of its digits Given an integer N, the task is to find an N-digit number such that it is not divisible by any of its digits.Note: There can be multiple answers for each value of N. Examples: Input: N = 4 Output: 6789 Explanation: As the number 6789 is not divisible by any of its digits, it is 6, 7, 8 and 9 and it
5 min read
Find a N-digit number such that it is not divisible by any of its digits Given an integer N, the task is to find any N-digit positive number (except for zeros) such that it is not divisible by any of its digits. If it is not possible to find any such number then print -1.Note: There can be more than one such number for the same N-digit. Examples: Input: N = 2 Output: 232
7 min read
Smallest number with sum of digits as N and divisible by 10^N Find the smallest number such that the sum of its digits is N and it is divisible by 10^N . Examples : Input : N = 5 Output : 500000 500000 is the smallest number divisible by 10^5 and sum of digits as 5. Input : N = 20 Output : 29900000000000000000000Recommended PracticeSmallest number with sum of
6 min read
Sum of n digit numbers divisible by a given number Given n and a number, the task is to find the sum of n digit numbers that are divisible by given number.Examples: Input : n = 2, number = 7Output : 728Explanation: There are thirteen n digit numbers that are divisible by 7. Numbers are : 14+ 21 + 28 + 35 + 42 + 49 + 56 + 63 +70 + 77 + 84 + 91 + 98.
9 min read
Minimum and maximum number of digits required to be removed to make a given number divisible by 3 Given a numeric string S, the task is to find the minimum and the maximum number of digits that must be removed from S such that it is divisible by 3. If it is impossible to do so, then print "-1". Examples: Input: S = "12345" Output: Minimum: 0 Maximum: 4 Explanation: The given number 12345 is divi
11 min read
Number of digits to be removed to make a number divisible by 3 Given a very large number num (1 <= num <= 10^1000), print the number of digits that needs to be removed to make the number exactly divisible by 3. If it is not possible then print -1.Examples : Input: num = "1234"Output: 1Explanation: we need to remove one digit that is 1 or 4, to make thenum
13 min read