- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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

- Related Questions & Answers
- Find a number x such that sum of x and its digits is equal to given n in C++
- Find a Number X whose sum with its digits is equal to N in 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 the Number of Solutions of n = x + n x using 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++
- 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++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! 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++
- Count of all possible values of X such that A % X = B in C++
- Find maximum N such that the sum of square of first N natural numbers is not more than X in Python
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++

Advertisements