- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find M-th number whose repeated sum of digits of a number is N in C++
In this problem, we are given two positive numbers N and M. Our task is to find the M-th number whose repeated sum of digits of a number is N.
Problem description: Here, we need to find the Mth number whose sum of digits till the sum becomes single digit is equal to N.
Let’s take an example to understand the problem,
Input: N = 4 M = 6
Output: 49
Solution Approach
A simple solution of the problem, is by finding all numbers and count the number whose sum of digits is N, and return m-th number.
Another solution to the problem is using formula to find M-th number whose sum of digits is equal to N,
M-th number = (m-1)*9 + N
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; int main() { int n = 4, m = 6; int mNumber = (m - 1) * 9 + n; cout<<m<<"-th number whose repeated sum of digits of a number is "<<n<<" is "<<mNumber; return 0; }
Output
6-th number whose repeated sum of digits of a number is 4 is 49
Advertisements