# Find the Nth digit from right in base B of the given number in Decimal base

Given a number **A** in decimal base, the task is to find the **N ^{th}** digit from last in base

**B**

**Examples:**

Input:A = 100, N = 3, B = 4

Output:2

Explanation:

(100)_{4}= 1210

3^{rd}digit from last is 2

Input:A = 50, N = 3, B = 5

Output:2

Explanation:

(50)_{5}= 200

3^{rd}digit from last is 2

**Approach:** The idea is to skip (N-1) digits of the given number in base B by dividing the number with B, (N – 1) times and then return the modulo of the current number by the B to get the N^{th} digit from the right.

Below is the implementation of the above approach:

## C++

`// C++ Implementation to find Nth digit ` `// from right in base B ` ` ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to compute Nth digit ` `// from right in base B ` `int` `nthDigit(` `int` `a, ` `int` `n, ` `int` `b) ` `{ ` ` ` ` ` `// Skip N-1 Digits in Base B ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `a = a / b; ` ` ` ` ` `// Nth Digit from right in Base B ` ` ` `return` `a % b; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a = 100; ` ` ` `int` `n = 3; ` ` ` `int` `b = 4; ` ` ` `cout << nthDigit(a, n, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Implementation to find Nth digit ` `// from right in base B ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to compute Nth digit ` `// from right in base B ` `static` `int` `nthDigit(` `int` `a, ` `int` `n, ` `int` `b) ` `{ ` ` ` ` ` `// Skip N-1 Digits in Base B ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i++) ` ` ` `a = a / b; ` ` ` ` ` `// Nth Digit from right in Base B ` ` ` `return` `a % b; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `100` `; ` ` ` `int` `n = ` `3` `; ` ` ` `int` `b = ` `4` `; ` ` ` `System.out.print(nthDigit(a, n, b)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Ptyhon3 Implementation to find Nth digit ` `# from right in base B ` ` ` `# Function to compute Nth digit ` `# from right in base B ` `def` `nthDigit(a, n, b): ` ` ` ` ` `# Skip N-1 Digits in Base B ` ` ` `for` `i ` `in` `range` `(` `1` `, n): ` ` ` `a ` `=` `a ` `/` `/` `b ` ` ` ` ` `# Nth Digit from right in Base B ` ` ` `return` `a ` `%` `b ` ` ` `# Driver Code ` `a ` `=` `100` `n ` `=` `3` `b ` `=` `4` `print` `(nthDigit(a, n, b)) ` ` ` `# This code is contributed by ApurvaRaj ` |

*chevron_right*

*filter_none*

## C#

`// C# Implementation to find Nth digit ` `// from right in base B ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to compute Nth digit ` ` ` `// from right in base B ` ` ` `static` `int` `nthDigit(` `int` `a, ` `int` `n, ` `int` `b) ` ` ` `{ ` ` ` ` ` `// Skip N-1 Digits in Base B ` ` ` `for` `(` `int` `i = 1; i < n; i++) ` ` ` `a = a / b; ` ` ` ` ` `// Nth Digit from right in Base B ` ` ` `return` `a % b; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `a = 100; ` ` ` `int` `n = 3; ` ` ` `int` `b = 4; ` ` ` `Console.Write(nthDigit(a, n, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Given a number N in decimal base, find number of its digits in any base (base b)
- Given a number N in decimal base, find the sum of digits in any base B
- Find the Largest N digit perfect square number in Base B
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Program to find the last digit of X in base Y
- Largest N digit number in Base B
- Largest value of x such that ax
^{x}is N-digit number of base b - Add two integers of different base and represent sum in smaller base of the two
- Convert from any base to decimal and vice versa
- Convert all substrings of length 'k' from base 'b' to decimal
- Find most significant bit of a number X in base Y
- Find the length of factorial of a number in any given base
- Largest Even and Odd N-digit numbers of base B
- Count of N-digit numbers in base K with no two consecutive zeroes
- Check if a number is in given base or not
- Check if a N base number is Even or Odd
- Complement of a number with any base b
- Pandigital number in a given base
- Check whether a number has consecutive 0's in the given base or not
- Check if a number N starts with 1 in b-base

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.