- Related Questions & Answers
- Find a number x such that sum of x and its digits is equal to given n using C++.
- Find minimum x such that (x % k) * (x / k) == n in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in C++
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B in Python
- Find the smallest number X such that X! contains at least Y trailing zeros in C++
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in Python
- Absolute difference between the first X and last X Digits of N?
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- Construct a frequency array of digits of the values obtained from x^1, x^2, ....., x^n in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Here we will see one problem, where we take a number n, we have to find another value say x, such that x + digit sum of x is same as the given number n. Suppose the value of n is 21. This program will return a number x = 15, as 15 + digit sum of 15, i.e. 15 + 1 + 5 = 21 = n.

To solve this problem, we have to follow simple approach. We will iterate through 1 to n, in each iteration, we will see if the sum of the number and its digit sum is same as the number, then stop, otherwise continue.

#include<iostream> using namespace std; int getDigitSum(int n) { int sum = 0; while (n) { sum += n % 10; n /= 10; } return sum; } int getNumber(int n) { for (int i = 0; i <= n; i++) if (i + getDigitSum(i) == n) return i; return -1; } int main() { int n = 21; cout << "The value of x is: " << getNumber(n); }

The value of x is: 15

Advertisements