
- 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
n-th number whose sum of digits is ten in C++
The numbers whose digits sum is equal to 10 are
19, 28, 37, 46, 55, 64, 73, 82, 91, etc..,
If you observe the series, each number is incremented by 9. There are numbers in the above sequence whose digits sum does not equal 10 while incrementing by 9. But, you will get all the numbers whose digits sum is equal to 10.
So, we can have a loop that increments by 9 and checks for digits sum and finds the n-th number. Let's see some examples
Inputs
3 7
Outputs
37 73
Algorithm
- Initialise the number n
- Initialise a counter to 0.
- Write a loop that iterates from 19
- If the current number digits sum is 10, increment the counter by 1.
- If the counter is equal to n, then return the current number.
- Increment the iterative variable by 9.
Implementation
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h> using namespace std; int findNthNumber(int n) { int count = 0, i = 19; while (true) { int sum = 0; for (int number = i; number > 0; number = number / 10) { sum = sum + number % 10; } if (sum == 10) { count++; } if (count == n) { return i; } i += 9; } return -1; } int main() { int n = 7; cout << findNthNumber(7) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
73
- Related Articles
- Find M-th number whose repeated sum of digits of a number is N in C++
- Find a Number X whose sum with its digits is equal to N in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Program to count number of consecutive lists whose sum is n in C++
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- Find the $5^{th}$ term of an A.P. of $n$ terms whose sum is $n^2−2n$.
- Finding n-th number made of prime digits (2, 3, 5 and 7) only in C++
- N-th Tribonacci Number in C++
- N-th polite number in C++
- N-th root of a number in C++
- Minimum number of single digit primes required whose sum is equal to N in C++
- Minimum number of squares whose sum equals to given number n\n
- Find smallest number with given number of digits and sum of digits in C++

Advertisements