
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find a Number X whose sum with its digits is equal to N in C++
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.
Example
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; }
Output
If you execute the above program, then you will get the following result.
76
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- 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++.
- 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++
- Count numbers whose sum with x is equal to XOR with x in C++
- C++ program to find largest or equal number of A whose sum of digits is divisible by 4
- A number has two digit whose sum is 9.If 27 is added to the number its digits are reversed. Find the number.
- Minimum number of single digit primes required whose sum is equal to N 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++
- A two digit number is 4 times the sum of its digits and twice the product of its digits. Find the number.
- How do you find continuous sub array whose sum is equal to a given number in Java?
- How to find all pairs of elements in Java array whose sum is equal to a given number?
- 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++

Advertisements