- 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

# Find Nth positive number whose digital root is X in C++

In this problem, we are given two integer values N and X. Our task is to create a program to Find Nth positive number whose digital root is X.

**Digital Root (X)** is a single digit positive number which is found by adding digits of N recursively adding digits, till the sum becomes single digit.

**Let’s take an example to understand the problem,**

## Input

N = 5, X = 4

## Output

40

## Solution Approach

A simple way to solve the problem is by counting the numbers with a digital root is X. For this, we will start from 1 and then check if the current number’s digital root is equal to X and count the numbers and return Nth such number.

**Program to illustrate the working of our solution,**

## Example

#include <iostream> using namespace std; int calcDigitalRoot(int num) { int digitSum = 1000, number = num; while (digitSum >= 10) { digitSum = 0; while (number > 0) { digitSum += number % 10; number /= 10; } number = digitSum; } return digitSum; } int calcNthDigitalRoot(int X, int N) { int countDigitalRootVals = 0; for (int i = 1; countDigitalRootVals < N; ++i) { int digitalRoot = calcDigitalRoot(i); if (digitalRoot == X) { ++countDigitalRootVals; } if (countDigitalRootVals == N) return i; } return -1; } int main(){ int X = 4, N = 5; cout<<N<<"th positive number whose digital root is "<<X<<" is "<<calcNthDigitalRoot(X, N); return 0; }

## Output

5th positive number whose digital root is 4 is 40

## Efficient approach

An efficient approach to solve the problem is by finding the N^{th} number with digital root equal to X using the direct formula which is,

N^{th}number = (N + 1)*9 + X

**Program to illustrate the working of our solution,**

## Example

#include <iostream> using namespace std; int calcNthDigitalRoot(int X, int N) { int nthNumber = (((N - 1) * 9) + X); return nthNumber; } int main() { int X = 4, N = 12; cout<<N<<"th positive number whose digital root is "<<X<<" is "<<calcNthDigitalRoot(X, N); return 0; }

## Output

12th positive number whose digital root is 4 is 103

- Related Questions & Answers
- Find the first natural number whose factorial is divisible by x in C++
- Digital root sort algorithm JavaScript
- Program to find number of pairs between x, whose multiplication is x and they are coprime in Python
- JavaScript to Calculate the nth root of a number
- Find a Number X whose sum with its digits is equal to N in C++
- Print a number containing K digits with digital root D in C++
- How to calculate the nth root of a number in JavaScript?
- C++ Program to find Numbers in a Range with Given Digital Root
- Find nth Hermite number in C++
- Digital Root (repeated digital sum) of the given large integer in C++ Program
- Count the nodes whose sum with X is a Fibonacci number in C++
- Find the Nth Ugly Number in Java
- Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++
- C++ code to find greater number whose factor is k
- Find cubic root of a number in C++