# Number with even sum of digits

A positive integer is considered a good number if sum of its digits is even. Find n-th smallest good number.

**Examples :**

Input : n = 1 Output : 2 First good number is smallest positive number with sum of digits even which is 2. Input : n = 10 Output : 20

A **simple solution** is to start from 1 and traverse through all natural numbers. For every number x, check if sum of digits is even. If even increment count of good numbers. Finally return the n-th Good number.

An **efficient solution** is based on a pattern in the answer. Let us list down first 20 good numbers. The first 20 good numbers are: 2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, 28, 31, 33, 35, 37, 39, 40. Observe that if last digit of n is from 0 to 4 the answer is 2*n and if last digit of n is from 5 to 9 the answer is 2*n + 1.

## C++

`// C++ program to find n-th ` `// Good number. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find kth good number. ` `long` `long` `int` `findKthGoodNo(` `long` `long` `int` `n) ` `{ ` ` ` `// Find the last digit of n. ` ` ` `int` `lastDig = n % 10; ` ` ` ` ` `// If last digit is between ` ` ` `// 0 to 4 then return 2 * n. ` ` ` `if` `(lastDig >= 0 && lastDig <= 4) ` ` ` `return` `n << 1; ` ` ` ` ` `// If last digit is between ` ` ` `// 5 to 9 then return 2*n + 1. ` ` ` `else` ` ` `return` `(n << 1) + 1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `int` `n = 10; ` ` ` `cout << findKthGoodNo(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find n-th ` `// Good number. ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find kth good number. ` ` ` `static` `int` `findKthGoodNo(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Find the last digit of n. ` ` ` `int` `lastDig = n % ` `10` `; ` ` ` ` ` `// If last digit is between ` ` ` `// 0 to 4 then return 2*n. ` ` ` `if` `(lastDig >= ` `0` `&& lastDig <= ` `4` `) ` ` ` `return` `n << ` `1` `; ` ` ` ` ` `// If last digit is between ` ` ` `// 5 to 9 then return 2*n + 1. ` ` ` `else` ` ` `return` `(n << ` `1` `) + ` `1` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` ` ` `System.out.println(findKthGoodNo(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find ` `# n-th Good number. ` ` ` `# Function to find kth ` `# good number. ` `def` `findKthGoodNo(n): ` ` ` ` ` `# Find the last digit of n. ` ` ` `lastDig ` `=` `n ` `%` `10` ` ` ` ` `# If last digit is between ` ` ` `# 0 to 4 then return 2 * n. ` ` ` `if` `(lastDig >` `=` `0` `and` `lastDig <` `=` `4` `) : ` ` ` `return` `n << ` `1` ` ` ` ` `# If last digit is between ` ` ` `# 5 to 9 then return 2 * n + 1. ` ` ` `else` `: ` ` ` `return` `(n << ` `1` `) ` `+` `1` ` ` ` ` `# Driver code ` `n ` `=` `10` `print` `(findKthGoodNo(n)) ` ` ` `# This code is contributed by ` `# Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find n-th ` `// Good number. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find kth ` ` ` `// good number ` ` ` `public` `static` `int` `findKthGoodNo(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Find the last digit of n. ` ` ` `int` `lastDig = n % 10; ` ` ` ` ` `// If last digit is between ` ` ` `// 0 to 4 then return 2*n. ` ` ` `if` `(lastDig >= 0 && lastDig <= 4) ` ` ` `return` `n << 1; ` ` ` ` ` `// If last digit is between ` ` ` `// 5 to 9 then return 2*n + 1. ` ` ` `else` ` ` `return` `(n << 1) + 1; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main (` `string` `[]args) ` ` ` `{ ` ` ` `int` `n = 10; ` ` ` `Console.WriteLine(findKthGoodNo(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ajit. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find n-th ` `// Good number. ` ` ` `// Function to find kth ` `// good number. ` `function` `findKthGoodNo(` `$n` `) ` `{ ` ` ` `// Find the last digit of n. ` ` ` `$lastDig` `= ` `$n` `% 10; ` ` ` ` ` `// If last digit is between ` ` ` `// 0 to 4 then return 2*n. ` ` ` `if` `(` `$lastDig` `>= 0 && ` `$lastDig` `<= 4) ` ` ` `return` `$n` `<< 1; ` ` ` ` ` `// If last digit is between ` ` ` `// 5 to 9 then return 2*n + 1. ` ` ` `else` ` ` `return` `(` `$n` `<< 1) + 1; ` `} ` ` ` `// Driver code ` `$n` `= 10; ` `echo` `(findKthGoodNo(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

20

**Time Complexity: ** O(1)

**Auxiliary Space: ** O(1)

## Recommended Posts:

- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find the Largest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits
- Minimum number of digits to be removed so that no two consecutive digits are same
- Smallest number with given sum of digits and sum of square of digits
- Number of digits in the nth number made of given four digits
- Build Lowest Number by Removing n digits from a given number
- Find maximum number that can be formed using digits of a given number
- Print a number strictly less than a given number such that all its digits are distinct.
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Count number of digits after decimal on dividing a number
- Number of digits to be removed to make a number divisible by 3
- Find the smallest number whose digits multiply to a given number n

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.