Skip to content
Related Articles
Find the Nth digit from right in base B of the given number in Decimal base
• Difficulty Level : Hard
• Last Updated : 25 Mar, 2021

Given a number A in decimal base, the task is to find the Nth digit from last in base B
Examples:

Input: A = 100, N = 3, B = 4
Output:
Explanation:
(100)4 = 1210
3rd digit from last is 2
Input: A = 50, N = 3, B = 5
Output:
Explanation:
(50)5 = 200
3rd 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 Nth 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 ``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;``}`

## 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`

## 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`

## 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`

## Javascript

 ``
Output:
`2`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up