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

31 Jan, 2022
Given a number A in decimal base, the task is to find the Nth digit from last in base BExamples:  
Input: A = 100, N = 3, B = 4 Output: 2 Explanation: (100)4 = 1210 3rd digit from last is 2 Input: A = 50, N = 3, B = 5 Output: 2 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++
Java
Python3
C#
Javascript
// 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 Bint 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 Codeint main(){    int a = 100;    int n = 3;    int b = 4;    cout << nthDigit(a, n, b);    return 0;}
// Java Implementation to find Nth digit// from right in base Bimport java.util.*; class GFG{ // Function to compute Nth digit// from right in base Bstatic 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 Codepublic 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 Implementation to find Nth digit# from right in base B # Function to compute Nth digit# from right in base Bdef 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 Codea = 100n = 3b = 4print(nthDigit(a, n, b)) # This code is contributed by ApurvaRaj
// C# Implementation to find Nth digit// from right in base Busing 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
<script> // Javascript Implementation to find Nth digit// from right in base B // Function to compute Nth digit// from right in base Bfunction nthDigit(a, n, b){     // Skip N-1 Digits in Base B    for (var i = 1; i < n; i++)        a = parseInt(a / b);     // Nth Digit from right in Base B    return a % b;} // Driver Codevar a = 100;var n = 3;var b = 4;document.write(nthDigit(a, n, b)); // This code is contributed by rutvik_56.</script>
2
 
Time Complexity: O(N)
Auxiliary Space: O(1)
ApurvaRaj
princiraj1992
ankthon
rutvik_56
rohitsingh07052
simmytarika5
base-conversion
number-digits
Technical Scripter 2019
Mathematical
Technical Scripter
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Program to find GCD or HCF of two numbers
Prime Numbers
Program to find sum of elements in a given array
Program for Decimal to Binary Conversion
Sieve of Eratosthenes
Program for factorial of a number
Operators in C / C++
Euclidean algorithms (Basic and Extended)