n-th number whose sum of digits is ten
Last Updated :
17 Aug, 2023
Given an integer value n, find out the n-th positive integer whose sum is 10.
Examples:
Input: n = 2
Output: 28
The first number with sum of digits as
10 is 19. Second number is 28.
Input: 15
Output: 154
Method 1 (Simple):
We traverse through all numbers. For every number, we find the sum of digits. We stop when we find the n-th number with the sum of digits as 10.
C++
// Simple CPP program to find n-th number
// with sum of digits as 10.
#include <bits/stdc++.h>
using namespace std;
int findNth(int n)
{
int count = 0;
for (int curr = 1;; curr++) {
// Find sum of digits in current no.
int sum = 0;
for (int x = curr; x > 0; x = x / 10)
sum = sum + x % 10;
// If sum is 10, we increment count
if (sum == 10)
count++;
// If count becomes n, we return current
// number.
if (count == n)
return curr;
}
return -1;
}
int main()
{
printf("%d\n", findNth(5));
return 0;
}
Java
// Java program to find n-th number
// with sum of digits as 10.
import java.util.*;
import java.lang.*;
public class GFG {
public static int findNth(int n)
{
int count = 0;
for (int curr = 1;; curr++) {
// Find sum of digits in current no.
int sum = 0;
for (int x = curr; x > 0; x = x / 10)
sum = sum + x % 10;
// If sum is 10, we increment count
if (sum == 10)
count++;
// If count becomes n, we return current
// number.
if (count == n)
return curr;
}
}
public static void main(String[] args)
{
System.out.print(findNth(5));
}
}
// Contributed by _omg
Python3
# Python3 program to find n-th number
# with sum of digits as 10.
import itertools
# function to find required number
def findNth(n):
count = 0
for curr in itertools.count():
# Find sum of digits in current no.
sum = 0
x = curr
while(x):
sum = sum + x % 10
x = x // 10
# If sum is 10, we increment count
if (sum == 10):
count = count + 1
# If count becomes n, we return current
# number.
if (count == n):
return curr
return -1
# Driver program
if __name__=='__main__':
print(findNth(5))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to find n-th number
// with sum of digits as 10.
using System;
class GFG {
public static int findNth(int n)
{
int count = 0;
for (int curr = 1;; curr++) {
// Find sum of digits in current no.
int sum = 0;
for (int x = curr; x > 0; x = x / 10)
sum = sum + x % 10;
// If sum is 10, we increment count
if (sum == 10)
count++;
// If count becomes n, we
// return current number.
if (count == n)
return curr;
}
}
// Driver Code
static public void Main()
{
Console.WriteLine(findNth(5));
}
}
// This code is contributed
// by Sach_Code
JavaScript
<script>
// Simple JavaScript program to find n-th number
// with sum of digits as 10.
function findNth(n)
{
let count = 0;
for (let curr = 1;; curr++) {
// Find sum of digits in current no.
let sum = 0;
for (let x = curr; x > 0; x = Math.floor(x / 10))
sum = sum + x % 10;
// If sum is 10, we increment count
if (sum == 10)
count++;
// If count becomes n, we return current
// number.
if (count == n)
return curr;
}
return -1;
}
document.write(findNth(5));
// This code is contributed by Surbhi Tyagi.
</script>
PHP
<?php
// Simple PHP program to find n-th
// number with sum of digits as 10.
function findNth($n)
{
$count = 0;
for ($curr = 1; ; $curr++)
{
// Find sum of digits in
// current no.
$sum = 0;
for ($x = $curr;
$x > 0; $x = $x / 10)
$sum = $sum + $x % 10;
// If sum is 10, we increment
// count
if ($sum == 10)
$count++;
// If count becomes n, we return
// current number.
if ($count == $n)
return $curr;
}
return -1;
}
// Driver Code
echo findNth(5);
// This code is contributed by Sach .
?>
Time Complexity: O(N*log10(N))
Auxiliary Space: O(1)
Method 2 (Efficient):
If we take a closer look, we can notice that all multiples of 9 are present in arithmetic progression 19, 28, 37, 46, 55, 64, 73, 82, 91, 100, 109,... However, there are numbers in the above series whose sum of digits is not 10, for example, 100. So instead of checking one by one, we start with 19 and increment by 9.
C++
// Simple CPP program to find n-th number
// with sum of digits as 10.
#include <bits/stdc++.h>
using namespace std;
int findNth(int n)
{
int count = 0;
for (int curr = 19;; curr += 9) {
// Find sum of digits in current no.
int sum = 0;
for (int x = curr; x > 0; x = x / 10)
sum = sum + x % 10;
// If sum is 10, we increment count
if (sum == 10)
count++;
// If count becomes n, we return current
// number.
if (count == n)
return curr;
}
return -1;
}
int main()
{
printf("%d\n", findNth(5));
return 0;
}
Java
// Java program to find n-th number
// with sum of digits as 10.
import java.util.*;
import java.lang.*;
public class GFG {
public static int findNth(int n)
{
int count = 0;
for (int curr = 19;; curr += 9) {
// Find sum of digits in current no.
int sum = 0;
for (int x = curr; x > 0; x = x / 10)
sum = sum + x % 10;
// If sum is 10, we increment count
if (sum == 10)
count++;
// If count becomes n, we return current
// number.
if (count == n)
return curr;
}
}
public static void main(String[] args)
{
System.out.print(findNth(5));
}
}
// Contributed by _omg
Python3
# Python3 program to find n-th
# number with sum of digits as 10.
def findNth(n):
count = 0;
curr = 19;
while (True):
# Find sum of digits in
# current no.
sum = 0;
x = curr;
while (x > 0):
sum = sum + x % 10;
x = int(x / 10);
# If sum is 10, we increment
# count
if (sum == 10):
count+= 1;
# If count becomes n, we return
# current number.
if (count == n):
return curr;
curr += 9;
return -1;
# Driver Code
print(findNth(5));
# This code is contributed
# by mits
C#
// C# program to find n-th number
// with sum of digits as 10.
using System;
class GFG {
public static int findNth(int n)
{
int count = 0;
for (int curr = 19;; curr += 9) {
// Find sum of digits in
// current no.
int sum = 0;
for (int x = curr;
x > 0; x = x / 10)
sum = sum + x % 10;
// If sum is 10, we increment
// count
if (sum == 10)
count++;
// If count becomes n, we return
// current number.
if (count == n)
return curr;
}
}
// Driver Code
static public void Main()
{
Console.WriteLine(findNth(5));
}
}
// This code is contributed
// by Sach_Code
JavaScript
<script>
// Simple Javascript program to find n-th
// number with sum of digits as 10.
function findNth(n)
{
let count = 0;
for (let curr = 19; ;curr += 9)
{
// Find sum of digits in
// current no.
let sum = 0;
for (let x = curr; x > 0;
x = parseInt(x / 10))
sum = sum + x % 10;
// If sum is 10, we increment
// count
if (sum == 10)
count++;
// If count becomes n, we return
// current number.
if (count == n)
return curr;
}
return -1;
}
// Driver Code
document.write(findNth(5));
// This code is contributed
// by gfgking
</script>
PHP
<?php
// Simple PHP program to find n-th
// number with sum of digits as 10.
function findNth($n)
{
$count = 0;
for ($curr = 19; ;$curr += 9)
{
// Find sum of digits in
// current no.
$sum = 0;
for ($x = $curr; $x > 0;
$x = (int)$x / 10)
$sum = $sum + $x % 10;
// If sum is 10, we increment
// count
if ($sum == 10)
$count++;
// If count becomes n, we return
// current number.
if ($count == $n)
return $curr;
}
return -1;
}
// Driver Code
echo findNth(5);
// This code is contributed
// by Sach_Code
?>
Time Complexity: O(N*log10(N))
Auxiliary Space: O(1)
Similar Reads
Find the smallest number whose sum of digits is N Given a positive integers N, the task is to find the smallest number whose sum of digits is N.Example: Input: N = 10Output: 19Explanation: 1 + 9 = 10 = N Input: N = 18Output: 99Explanation: 9 + 9 = 18 = N Naive Approach: A Naive approach is to run a loop of i starting from 0 and find Sum of digits o
6 min read
Smallest number whose sum of digits is square of N Given an integer N, the task is to find the smallest number whose sum of digits is N2. Examples: Input: N = 4 Output: 79 24 = 16 sum of digits of 79 = 76 Input: N = 6 Output: 9999 210 = 1024 which has 4 digits Approach: The idea is to find the general term for the smallest number whose sum of digits
3 min read
What is the smallest 4 digit number? The method to represent and work with numbers is known as the number system. A number system is a system of writing to represent numbers. It is the mathematical notation used to represent numbers of a given set by using digits or other symbols. It has arithmetic operations to perform division, multi
5 min read
Find the n-th number made of even digits only Given a number n, find out the n-th positive number made up of even digits (0, 2, 4, 6, 8) only. First few numbers made of even digits are 0, 2, 4, 6, 8, 20, 22, 24....... Examples : Input : 2 Output : 2 Second number made of 0, 2, 4, 6, 8 is 2 Input : 10 Output : 28 Naive Approach A naive approach
14 min read
Maximum number made up of distinct digits whose sum is equal to N Given a positive integer N, the task is to find the largest positive number made up of distinct digits having the sum of its digits equal to N. If no such number exists, print â-1â. Examples: Input: N = 25Output: 98710Explanation:The number 98710 is the largest number that contains only unique digit
7 min read