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++
#include <bits/stdc++.h>
using namespace std;
int digit_sum( int n)
{
int sum = 0, m;
while (n > 0) {
m = n % 10;
sum = sum + m;
n = n / 10;
}
return (sum);
}
int reverse( int n)
{
int r = 0;
while (n != 0) {
r = r * 10;
r = r + n % 10;
n = n / 10;
}
return (r);
}
void operation( int n)
{
int i = 1, a, count = 0, r;
while (count < n) {
a = digit_sum(i);
r = reverse(i);
if (i % a == 0 && r % a == 0) {
cout << i << " " ;
count++;
i++;
}
else
i++;
}
}
int main()
{
int n = 10;
operation(n);
}
|
Java
import java.util.*;
class GFG{
static int digit_sum( int n)
{
int sum = 0 , m;
while (n > 0 )
{
m = n % 10 ;
sum = sum + m;
n = n / 10 ;
}
return (sum);
}
static int reverse( int n)
{
int r = 0 ;
while (n != 0 )
{
r = r * 10 ;
r = r + n % 10 ;
n = n / 10 ;
}
return (r);
}
static void operation( int n)
{
int i = 1 , a, count = 0 , r;
while (count < n)
{
a = digit_sum(i);
r = reverse(i);
if (i % a == 0 && r % a == 0 )
{
System.out.print(i + " " );
count++;
i++;
}
else
i++;
}
}
public static void main(String args[])
{
int n = 10 ;
operation(n);
}
}
|
Python3
def digit_sum(n):
sum = 0
while (n > 0 ):
m = n % 10 ;
sum = sum + m;
n = n / / 10
return ( sum )
def reverse(n):
r = 0
while (n ! = 0 ):
r = r * 10
r = r + n % 10
n = n / / 10
return (r)
def operation(n):
i = 1
count = 0
while (count < n):
a = digit_sum(i)
r = reverse(i)
if (i % a = = 0 and r % a = = 0 ):
print (i, end = " " )
count + = 1
i + = 1
else :
i + = 1
if __name__ = = '__main__' :
n = 10
operation(n)
|
C#
using System;
class GFG{
static int digit_sum( int n)
{
int sum = 0, m;
while (n > 0)
{
m = n % 10;
sum = sum + m;
n = n / 10;
}
return (sum);
}
static int reverse( int n)
{
int r = 0;
while (n != 0)
{
r = r * 10;
r = r + n % 10;
n = n / 10;
}
return (r);
}
static void operation( int n)
{
int i = 1, a, count = 0, r;
while (count < n)
{
a = digit_sum(i);
r = reverse(i);
if (i % a == 0 && r % a == 0)
{
Console.Write(i + " " );
count++;
i++;
}
else
i++;
}
}
public static void Main()
{
int n = 10;
operation(n);
}
}
|
Javascript
<script>
function digit_sum(n)
{
let sum = 0, m;
while (n > 0) {
m = n % 10;
sum = sum + m;
n = parseInt(n / 10, 10);
}
return (sum);
}
function reverse(n)
{
let r = 0;
while (n != 0) {
r = r * 10;
r = r + n % 10;
n = parseInt(n / 10, 10);
}
return (r);
}
function operation(n)
{
let i = 1, a, count = 0, r;
while (count < n) {
a = digit_sum(i);
r = reverse(i);
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)
Last Updated :
12 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...