
- 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 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 Nth number with digital root equal to X using the direct formula which is,
Nth 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 Articles
- The sum of a number and its positive square root is $\frac{6}{25}$. Find the number.
- JavaScript to Calculate the nth root of a number
- How to calculate the nth root of a number in JavaScript?
- Find the first natural number whose factorial is divisible by x in C++
- Find nth Hermite number in C++
- Find two consecutive positive integers, sum of whose squares is 365.
- Print a number containing K digits with digital root D in C++
- Program to find number of pairs between x, whose multiplication is x and they are coprime in Python
- Find a Number X whose sum with its digits is equal to N in C++
- Find the Nth Ugly Number in Java
- Find two consecutive odd positive integers, sum of whose squares is 970.
- Digital root sort algorithm JavaScript
- Positive and Negative Logic in Digital Electronics
- Find the number whose 16% is 36?
- C++ Program to find Numbers in a Range with Given Digital Root
