Place Value of a given digit in a number

03 May, 2022
Place value can be defined as the value represented by a digit in a number on the basis of its position in the number. Here is the illustration of place value with an example: 
Let’s take another example to illustrate the place value for N = 45876  
Problem: Given a positive integer N and a digit D. The task is to find out the place value of a digit D in the given number N. If multiple occurrences of digit occur then find the minimum place value.Example: 
Input: N = 3928, D = 3 Output: 3000 Explanation: Place value of 3 in this number is 3*1000 = 3000Input: N = 67849, D = 6 Output: 60000  
 
Solution Approach:  
Find the position of the digit from the right side of the input number. In order to find the position take the mod of that number by 10 and check for the number and divide the number by 10.When the position of the digit is found then just multiply the digit with the 10 position.
Find the position of the digit from the right side of the input number. In order to find the position take the mod of that number by 10 and check for the number and divide the number by 10.
When the position of the digit is found then just multiply the digit with the 10 position.
Here is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation to find// place value of a number#include<bits/stdc++.h>using namespace std; // Function to find place valueint placeValue(int N, int num){    int total = 1, value = 0, rem = 0;    while (true)    {        rem = N % 10;        N = N / 10;         if (rem == num)        {            value = total * rem;            break;        }        total = total * 10;    }    return value;} // Driver Codeint main(){     // Digit, which we want    // to find place value.    int D = 5;     // Number from where we    // want to find place value.    int N = 85932;     cout << (placeValue(N, D));} // This code is contributed by Ritik Bansal
// Java implementation to find// place value of a number import java.util.*;import java.io.*;import java.lang.*; class GFG {     // function to find place value    static int placeValue(int N, int num)    {        int total = 1, value = 0, rem = 0;        while (true) {            rem = N % 10;            N = N / 10;             if (rem == num) {                value = total * rem;                break;            }             total = total * 10;        }        return value;    }     // Driver Code    public static void main(String[] args)    {         // Digit, which we want        // to find place value.        int D = 5;         // Number from where we        // want to find place value.        int N = 85932;         System.out.println(placeValue(N, D));    }}
# Python3 implementation to find# place value of a number # Function to find place valuedef placeValue(N, num):         total = 1    value = 0    rem = 0         while (True):        rem = N % 10        N = N // 10         if (rem == num):            value = total * rem            break        total = total * 10             return value # Driver Code # Digit, which we want# to find place value.D = 5 # Number from where we# want to find place value.N = 85932 print(placeValue(N, D)) # This code is contributed by divyamohan123
// C# implementation to find// place value of a numberusing System;class GFG{ // function to find place valuestatic int placeValue(int N, int num){    int total = 1, value = 0, rem = 0;    while (true)    {        rem = N % 10;        N = N / 10;         if (rem == num)        {            value = total * rem;            break;        }         total = total * 10;    }    return value;} // Driver Codepublic static void Main(){     // Digit, which we want    // to find place value.    int D = 5;     // Number from where we    // want to find place value.    int N = 85932;     Console.Write(placeValue(N, D));}} // This code is contributed by Code_Mech
<script>       // JavaScript implementation to find      // place value of a number       // Function to find place value      function placeValue(N, num) {        var total = 1,          value = 0,          rem = 0;        while (true) {          rem = N % 10;          N = parseInt(N / 10);           if (rem == num) {            value = total * rem;            break;          }          total = total * 10;        }        return value;      }       // Driver Code       // Digit, which we want      // to find place value.      var D = 5;       // Number from where we      // want to find place value.      var N = 85932;      document.write(placeValue(N, D));       </script>
5000
 
Time Complexity: O(log N) Auxiliary space: O(1) 
divyamohan123
Code_Mech
bansal_rtk_
rdtank
avtarkumar719
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.