Find a number x such that sum of x and its digits is equal to given n.

Given a positive number n. We need to find a number x such that sum of digits of x to itself is equal to n.
If no such x is possible print -1.

Examples:

Input : n = 21
Output : x = 15
Explanation : x + its digit sum = 15 + 1 + 5 = 21 

Input : n = 5
Output : -1

We iterate from 1 to n and for each intermediate number x find its digit sum and then add that to x, if that is equal to n then x will be our required answer.

    // iterate from 1 to n. For every no.
    // check if its digit sum with it is
    // equal to n.
    for (int i = 0; i <= n; i++)
        if (i + digSum(i)  == n)
            return i;

    return -1;

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find x such that x +
// digSum(x) is equal to n.
#include <bits/stdc++.h>
using namespace std;
  
// utility function for digit sum
int digSum(int n)
{
    int sum = 0, rem = 0;
    while (n) {
        rem = n % 10;
        sum += rem;
        n /= 10;
    }
    return sum;
}
  
// function for finding x
int findX(int n)
{
    // iterate from 1 to n. For every no.
    // check if its digit sum with it is
    // equal to n.
    for (int i = 0; i <= n; i++) 
        if (i + digSum(i) == n)
            return i;
      
    // if no such i found return -1
    return -1;
}
  
// driver function
int main()
{
    int n = 43;
    cout << "x = " << findX(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find x such that x +
// digSum(x) is equal to n.
class GFG
{
      
    // utility function for digit sum
    static int digSum(int n)
    {
        int sum = 0, rem = 0;
          
        while (n>0) {
            rem = n % 10;
            sum += rem;
            n /= 10;
        }
          
        return sum;
    }
      
    // function for finding x
    static int findX(int n)
    {
          
        // iterate from 1 to n. For every no.
        // check if its digit sum with it is
        // equal to n.
        for (int i = 0; i <= n; i++) 
            if (i + digSum(i) == n)
                return i;
          
        // if no such i found return -1
        return -1;
    }
      
    // Driver code
    public static void main (String[] args)
    {
        int n = 43;
          
        System.out.println("x = "+findX(n));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# x such that dx + igSum(x) 
# is equal to n.
  
# utility function 
# for digit sum
def digSum(n):
    sum = 0;
    rem = 0;
    while(n):
        rem = n % 10;
        sum = sum + rem;
        n = int(n / 10);
    return sum;
  
# function for finding x
def findX(n):
      
    # iterate from 1 to n.
    # For every no.
    # check if its digit
    # sum with it is# equal to n.
    for i in range(n + 1):
        if (i + digSum(i) == n):
            return i;
      
    # if no such i
    # found return -1
    return -1;
  
# Driver Code
n = 43;
print("x = ", findX(n));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find x such that 
// x + digSum(x) is equal to n.
using System;
  
class GFG {
      
    // utility function for digit sum
    static int digSum(int n)
    {
        int sum = 0, rem = 0;
          
        while (n > 0) 
        {
            rem = n % 10;
            sum += rem;
            n /= 10;
        }
          
        return sum;
    }
      
    // function for finding x
    static int findX(int n)
    {
          
        // iterate from 1 to n. For every no.
        // check if its digit sum with it is
        // equal to n.
        for (int i = 0; i <= n; i++) 
            if (i + digSum(i) == n)
                return i;
          
        // if no such i found return -1
        return -1;
    }
      
    // Driver code
    public static void Main()
    {
        int n = 43;
          
        Console.Write("x = " + findX(n));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find x such that 
// dx + igSum(x) is equal to n.
  
// utility function 
// for digit sum
function digSum($n)
{
    $sum = 0; $rem = 0;
    while ($n
    {
        $rem = $n % 10;
        $sum += $rem;
        $n /= 10;
    }
    return $sum;
}
  
// function for finding x
function findX($n)
{
      
    // iterate from 1 to n.
    // For every no.
    // check if its digit
    // sum with it is
    // equal to n.
    for ($i = 0; $i <= $n; $i++) 
        if ($i + digSum($i) == $n)
            return $i;
      
    // if no such i 
    // found return -1
    return -1;
}
  
    // Driver Code
    $n = 43;
    echo "x = " , findX($n);
  
// This code is contributed by vt_m.
?>

chevron_right



Output:

x = 35

This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.