# Print all Good numbers in given range

Given a digit ‘d’ and a range [L, R] where L < R, print all good numbers in given range that don't contain digit 'd'. A number is good if its every digit is larger than the sum of digits which are on the right side of that digit. For example 9620 is good number because 2 > 0, 6 > 2+0 and 9 > 6+2+0.

Example:

```Input:  L = 410, R = 520, d = 3
Output: 410 420 421 510 520
All the numbers in output are good (every digit is more
than sum of digits on right of it) and don't have digit 3.

Input:  L = 410, R = 520, d = 1
Output: 420 430 520
All the numbers in output are good (every digit is more
than sum of digits on right of it) and don't have digit 1.
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

The idea is to traverse all numbers in given range. For every number, traverse all digits. While traversing keep track of digit sum so far. At any point, if previous sum becomes more than or equal to sum, return false. Also, if current digit becomes ‘d’, return false.

Below is implementation of the idea.

## C/C++

 `// C++ program to print good numbers in a given range [L, R] ` `#include ` `using` `namespace` `std; ` ` `  `// To check whether n is a good number and doesn't contain ` `// digit 'd'. ` `bool` `isValid(``int` `n, ``int` `d) ` `{ ` `    ``// Get last digit and initialize sum from right side ` `    ``int` `digit = n%10; ` `    ``int` `sum = digit; ` ` `  `    ``// If last digit is d, return ` `    ``if` `(digit == d) ` `      ``return` `false``; ` ` `  `    ``// Traverse remaining digits ` `    ``n /= 10; ` `    ``while` `(n) ` `    ``{ ` `        ``// Current digit ` `        ``digit = n%10; ` ` `  `        ``// If digit is d or digit is less than or ` `        ``// equal to sum of digits on right side ` `        ``if` `(digit  == d || digit <= sum) ` `            ``return` `false``; ` ` `  `        ``// Update sum and n ` `        ``else` `        ``{ ` `            ``sum += digit; ` `            ``n /= 10; ` `        ``} ` `    ``} ` `    ``return` `1; ` `} ` ` `  `// Print Good numbers in range [L, R] ` `void` `printGoodNumbers(``int` `L, ``int` `R, ``int` `d) ` `{ ` `   ``// Traverse all numbers in given range ` `   ``for` `(``int` `i=L; i<=R; i++) ` `   ``{ ` `       ``// If current numbers is good, print it. ` `       ``if` `(isValid(i, d)) ` `          ``cout << i << ``" "``; ` `   ``} ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `L = 410, R = 520, d = 3; ` ` `  `    ``// Print good numbers in [L, R] ` `    ``printGoodNumbers(L, R, d); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to print good numbers in a given range [L, R] ` `import` `java.io.*; ` ` `  `class` `Numbers  ` `{ ` `    ``// Function to check whether n is a good number and doesn't contain ` `    ``// digit 'd' ` `    ``static` `boolean` `isValid(``int` `n, ``int` `d) ` `    ``{ ` `        ``// Get last digit and initialize sum from right side ` `        ``int` `digit = n%``10``; ` `        ``int` `sum = digit; ` ` `  `        ``// If last digit is d, return ` `        ``if` `(digit == d) ` `        ``return` `false``; ` ` `  `        ``// Traverse remaining digits ` `        ``n /= ``10``; ` `        ``while` `(n>``0``) ` `        ``{ ` `            ``// Current digit ` `            ``digit = n%``10``; ` `     `  `            ``// If digit is d or digit is less than or ` `            ``// equal to sum of digits on right side ` `            ``if` `(digit == d || digit <= sum) ` `                ``return` `false``; ` ` `  `            ``// Update sum and n ` `                ``else` `                ``{ ` `                    ``sum += digit; ` `                    ``n /= ``10``; ` `                ``} ` `        ``} ` `    ``return` `true``; ` `    ``} ` `     `  `    ``// Print Good numbers in range [L, R] ` `    ``static` `void` `printGoodNumber(``int` `L, ``int` `R, ``int` `d) ` `    ``{ ` `        ``// Traverse all numbers in given range ` `        ``for``(``int` `i=L;i<=R;i++) ` `        ``{ ` `            ``// If current numbers is good, print it ` `            ``if``(isValid(i, d)) ` `                ``System.out.print(i+``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `L = ``410``, R = ``520``, d = ``3``; ` `         `  `        ``// Print good numbers in [L, R] ` `        ``printGoodNumber(L, R, d); ` `    ``} ` `} `

## Python3

 `# Python3 program to print good  ` `# numbers in a given range [L, R] ` ` `  `# Function to check whether n is  ` `# a good number and doesn't contain ` `# digit 'd' ` `def` `isValid(n, d): ` `     `  `    ``# Get last digit and initialize ` `    ``# sum from right side ` `    ``digit ``=` `n ``%` `10``; ` `    ``sum` `=` `digit; ` ` `  `    ``# If last digit is d, return ` `    ``if` `(digit ``=``=` `d): ` `        ``return` `False``; ` ` `  `    ``# Traverse remaining digits ` `    ``n ``=` `int``(n ``/` `10``); ` `    ``while` `(n > ``0``): ` `         `  `        ``# Current digit ` `        ``digit ``=` `n ``%` `10``; ` `     `  `        ``# If digit is d or digit is  ` `        ``# less than or equal to sum  ` `        ``# of digits on right side ` `        ``if``(digit ``=``=` `d ``or` `digit <``=` `sum``): ` `            ``return` `False``; ` `             `  `        ``# Update sum and n ` `        ``else``: ` `            ``sum` `+``=` `digit; ` `            ``n ``=` `int``(n ``/` `10``); ` `    ``return` `True``; ` `     `  `# Print Good numbers in range [L, R] ` `def` `printGoodNumber(L, R, d): ` `     `  `    ``# Traverse all numbers  ` `    ``# in given range ` `    ``for` `i ``in` `range``(L, R ``+` `1``): ` `         `  `        ``# If current numbers is good, ` `        ``# print it ` `        ``if``(isValid(i, d)): ` `            ``print``(i, end ``=` `" "``); ` `     `  `# Driver Code ` `L ``=` `410``; ` `R ``=` `520``; ` `d ``=` `3``; ` `         `  `# Print good numbers in [L, R] ` `printGoodNumber(L, R, d); ` ` `  `# This code is contributed by mits `

## C#

 `// C# program to print good numbers in a  ` `// given range [L, R] ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to check whether n is a good  ` `    ``// number and doesn't contain digit 'd' ` `    ``static` `bool` `isValid(``int` `n, ``int` `d) ` `    ``{ ` `         `  `        ``// Get last digit and initialize  ` `        ``// sum from right side ` `        ``int` `digit = n % 10; ` `        ``int` `sum = digit; ` ` `  `        ``// If last digit is d, return ` `        ``if` `(digit == d) ` `            ``return` `false``; ` ` `  `        ``// Traverse remaining digits ` `        ``n /= 10; ` `        ``while` `(n > 0) ` `        ``{ ` `             `  `            ``// Current digit ` `            ``digit = n % 10; ` `     `  `            ``// If digit is d or digit is  ` `            ``// less than or equal to sum of  ` `            ``// digits on right side ` `            ``if` `(digit == d || digit <= sum) ` `                ``return` `false``; ` ` `  `            ``// Update sum and n ` `            ``else` `            ``{ ` `                ``sum += digit; ` `                ``n /= 10; ` `            ``} ` `        ``} ` `         `  `    ``return` `true``; ` `    ``} ` `     `  `    ``// Print Good numbers in range [L, R] ` `    ``static` `void` `printGoodNumber(``int` `L,  ` `                               ``int` `R, ``int` `d) ` `    ``{ ` `         `  `        ``// Traverse all numbers in given range ` `        ``for``(``int` `i = L; i <= R; i++) ` `        ``{ ` `             `  `            ``// If current numbers is good, ` `            ``// print it ` `            ``if``(isValid(i, d)) ` `                ``Console.Write(i+``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `L = 410, R = 520, d = 3; ` `         `  `        ``// Print good numbers in [L, R] ` `        ``printGoodNumber(L, R, d); ` `    ``} ` `} ` ` `  `//This code is contributed by vt_m. `

## PHP

 ` `

Output:

` 410 420 421 510 520 `

My Personal Notes arrow_drop_up

Improved By : Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.