- 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

In this tutorial, we are going to find a number whose some including with its digits is equal to the given number N.

The idea is simple, we are going to check the left and right 100 numbers of the given number. It won't lie out that bound as N ≤ 1000000000 and the sum won't exceed 100.

Let's see the steps to solve the problem.

Initialize the number.

Write a loop that iterates 100 times.

Get the n - i and n + i values.

Find the sum of the digits and add them.

If anyone of them is equal to the N, print them.

Let's see the code.

#include <bits/stdc++.h> using namespace std; int digitsSum(int n) { int sum = 0; while (n) { sum += n % 10; n /= 10; } return sum; } void findX(long int n) { bool is_found = false; for (int i = 0; i <= 100; i++) { long int valueOnLeft = abs(n - i) + digitsSum(abs(n - i)); long int valueOnRight = n + i + digitsSum(n + i); if (valueOnLeft == n) { is_found = true; cout << abs(n - i) << " "; } if (valueOnRight == n) { is_found = true; cout << (n + i) << " "; } } if (!is_found) { cout << "No numbers found"; } cout << endl; } int main() { int n = 89; findX(n); return 0; }

If you execute the above program, then you will get the following result.

76

If you have any queries in the tutorial, mention them in the comment section.

- 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 such that sum of x and its digits is equal to given n using C++.
- Count numbers whose sum with x is equal to XOR with x in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- n-th number whose sum of digits is ten in C++
- Maximum Primes whose sum is equal to given N in C++
- Count the nodes whose sum with X is a Fibonacci number in C++
- Minimum number of single digit primes required whose sum is equal to N in C++
- Count numbers whose difference with N is equal to XOR with N in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Find N distinct numbers whose bitwise Or is equal to K in C++
- Count quadruples from four sorted arrays whose sum is equal to a given value x in C++
- Count pairs from two sorted arrays whose sum is equal to a given value x in C++

Advertisements