The Wayback Machine - https://web.archive.org/web/20241126093849/https://www.geeksforgeeks.org/place-value-of-a-given-digit-in-a-number/
Open In App

Place Value of a given digit in a number

Last Updated : 03 May, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 = 3000
Input: N = 67849, D = 6 
Output: 60000 
 

 

Solution Approach: 
 

  1. 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.
  2. 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++




// C++ implementation to find
// place value of a number
#include<bits/stdc++.h>
using namespace std;
 
// Function to find place value
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
int 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




// 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




# Python3 implementation to find
# place value of a number
 
# Function to find place value
def 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#




// C# implementation to find
// place value of a number
using System;
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()
{
 
    // 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


Javascript




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


Output: 

5000

 

Time Complexity: O(log N) 
Auxiliary space: O(1)
 



Similar Reads

Count ways to generate N digit number such that its every digit divisible by previous digit
Given a number N, the task is to count the number of ways to create an N digit number from digits 1 to 9 such that every digit is divisible by its previous digit that is if the number is represented by an array of digits A then A[i + 1] % A[i] == 0. print the answer modulo 109 + 7. Examples: Input: N = 2Output: 23Explanation: For N = 2 possible ans
15+ min read
Multiply a single digit number in place to a number represented as a Linked List
Given a linked list of N nodes where each node represents digits of a number and a single-digit number M, the task is to multiply the list by M in-place and print the resulting linked list. Examples: Input: Linked list: 1 ? 2 ? 7 ? 3 ? NULL, M = 3Output: 3 ? 8 ? 1 ? 9 ? NULLExplanation: The given linked list represents the number 1273. Multiplying
12 min read
Count of N-digit numbers having digit XOR as single digit
Given an integer N, the task is to find the total count of N-Digit numbers such that the Bitwise XOR of the digits of the numbers is a single digit. Examples: Input: N = 1Output: 9Explanation: 1, 2, 3, 4, 5, 6, 7, 8, 9 are the numbers. Input: N = 2Output: 66Explanation: There are 66 such 2-digit numbers whose Xor of digits is a single digit number.
14 min read
Find the unit place digit of sum of N factorials
Given a number N, the task is to find units place digit of the first N natural numbers factorials, i.e. 1!+2!+3!+....N! where N<=10e18.Examples: Input: n = 2 Output: 31! + 2! = 3Last digit is 3Input: n = 3Output: 91! + 2! + 3! = 9Last digit is 9 Brute Force Approach: In this approach, we are calculating the factorial of each number and then addi
10 min read
Count possible N-digit numbers such that each digit does not appear more than given number of times consecutively
Given an integer N and an array maxDigit[], the task is to count all the distinct N-digit numbers such that digit i does not appear more than maxDigit[i] times. Since the count can be very large, print it modulo 109 + 7. Examples: Input: N = 2, maxDigit[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}Output: 90Explanation:Any digit can't appear more than once co
15+ min read
Largest number less than N with digit sum greater than the digit sum of N
Given an integer N, the task is to find the greatest number less than N such that the sum of its digits is greater than the sum of the digits of N. If the condition isn't satisfied for any number then print -1.Examples: Input: N = 100 Output: 99 99 is the largest number less than 100 sum of whose digits is greater than the sum of the digits of 100I
9 min read
Count of Numbers in Range where first digit is equal to last digit of the number
Given a range represented by two positive integers L and R. Find the count of numbers in the range where the first digit is equal to the last digit of the number. Examples: Input : L = 2, R = 60 Output : 13 Explanation : Required numbers are 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44 and 55 Input : L = 1, R = 1000 Output : 108 Prerequisites: Digit DPTh
15+ min read
Find the remainder when First digit of a number is divided by its Last digit
Given a number N, find the remainder when the first digit of N is divided by its last digit.Examples: Input: N = 1234 Output: 1 First digit = 1 Last digit = 4 Remainder = 1 % 4 = 1 Input: N = 5223 Output: 2 First digit = 5 Last digit = 3 Remainder = 5 % 3 = 2 Approach: Find the first digit and the last digit of the number. Find then the remainder w
5 min read
Last digit of a number raised to last digit of N factorial
Given two number X and N, the task is to find the last digit of X raised to last digit of N factorial, i.e. [Tex]X^{\left ( N! \right )mod 10} [/Tex].Examples: Input: X = 5, N = 2 Output: 5 Explanation: Since, 2! mod 10 = 2 therefore 52 = 25 and the last digit of 25 is 5.Input: X = 10, N = 4 Output: 0 Explanation: Since, 4! mod 10 = 24 mod 10 = 4 t
15 min read
Min steps to convert N-digit prime number into another by replacing a digit in each step
Given two N-digit prime numbers A and B, the task is to find the minimum number of steps taken to convert A to B. Condition for the conversion is that only 1 digit of the current prime number can be modified such that the new number formed is also a prime number. If no such conversion is possible, print -1. Note: The range of N is [1, 5]. Examples:
12 min read
Count of integer whose factorial of digit sum contains every digit of original number
Given an array arr[] of length n which contains positive integers (0 ≤ arr[i] ≤ 109), the task is to count the number of elements present in the given array such that the factorial of their digit sum (we will do digit sum while its value is greater than 10) contains every digit present in the original integer. Examples: Input: arr[] = [653, 663, 24
14 min read
Count n digit numbers not having a particular digit
We are given two integers n and d, we need to count all n digit numbers that do not have digit d. Example : Input : n = 2, d = 7 Output : 72 All two digit numbers that don't have 7 as digit are 10, 11, 12, 13, 14, 15, 16, 18, ..... Input : n = 3, d = 9 Output : 648 A simple solution is to traverse through all d digit numbers. For every number, chec
8 min read
Count numbers in a range with digit sum divisible by K having first and last digit different
Given a range in the form of L and R, and a value K, the task is to count the numbers between the range L to R such that the sum of the digit is divisible by K, and the first digit is not equal to the last digit of the number. Examples: Input: L = 1, R = 10, K = 2 Output: 0 Explanation: The input numbers whose sum of the digits are divisible by 2 (
15 min read
Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
Given the range L and R, count all numbers between L to R such that sum of digits of each number and sum of square of digits of each number is Prime.Note: 10 <= [L, R] <= 108 Examples: Input: L = 10, R = 20 Output: 4 Such types of numbers are: 11 12 14 16 Input: L = 100, R = 130 Output: 9Such types of numbers are : 101 102 104 106 110 111 113
9 min read
Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
Given a number N, the task is to find the number of pairs (A, B) in the range [1, N] such that the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.Examples: Input: N = 25 Output: 17 Explanation: The pairs are: (1, 1), (1, 11), (2, 2), (2, 22), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8),
5 min read
Count of N-digit numbers having each digit as the mean of its adjacent digits
Given a positive integer N, the task is to count the number of N-digit numbers where each digit in the number is the mean of its two adjacent digits. Examples : Input: N = 1Output: 10Explanation: All numbers from 0 to 9 satisfy the given condition as there is only one digit. Input: N = 2Output: 90 Naive Approach: The simplest approach to solve the
11 min read
Count N-digit numbers such that every position is divisible by the digit at that position
Given a positive integer N, the task is to count the number of N-digit numbers such that every index (1-based indexing) in the number is divisible by the digit occurring at that index. Since the court can be very large, print it modulo 109 + 7. Examples: Input: N = 2Output: 2Explanation: The numbers 11 and 12 are the only 2-digit numbers satisfying
6 min read
Count of N digit numbers with at least one digit as K
Given a number N and a digit K, The task is to count N digit numbers with at least one digit as K. Examples: Input: N = 3, K = 2Output: 252Explanation: For one occurrence of 2 - In a number of length 3, the following cases are possible:=>When first digit is 2 and other two digits can have 9 values except '2'. Thus 9*9 = 81 combination are possib
6 min read
Count N-digit numbers that contains every possible digit atleast once
Given a positive integer N, the task is to count the number of N-digit numbers such that every digit from [0-9] occurs at least once. Examples : Input: N = 10Output : 3265920 Input: N = 5Output: 0Explanation: Since the number of digits is less than the minimum number of digits required [0-9], the answer is 0. Naive Approach: The simplest approach t
15 min read
Count of N-digit numbers with at least one digit repeating
Given a positive integer N, the task is to find the number of N-digit numbers such that at least one digit in the number has occurred more than once. Examples: Input: N = 2Output: 9Explanation:All the 2-digit number such that at least 1 digits occurs more than once are {11, 22, 33, 44, 55, 66, 77, 88, 99}. Therefore, the total count is 9. Input: N
15+ min read
Count 'd' digit positive integers with 0 as a digit
Given a number d, representing the number of digits of a positive integer. Find the total count of positive integer (consisting of d digits exactly) which have at-least one zero in them.Examples: Input : d = 1 Output : 0 There's no natural number of 1 digit that contains a zero. Input : d = 2 Output : 9 The numbers are, 10, 20, 30, 40, 50, 60, 70,
5 min read
Check if frequency of each digit is less than the digit
Given an integer n, the task is to check if frequency of each digit of the number is less than or equal to digit itself. Examples: Input : 51241 Output : False Input : 1425243 Output : True Naive Approach: Start from 0 and count the frequency for every digit upto 9, if at any place frequency is more than the digit value then return false, else retu
8 min read
Count all Pairs in an Array with Balanced Digit Sums and Even-Digit Concatenation
Given an array of integers arr[], A pairs (i, j) is valid, which follows the below two conditions: Concatenation of integer at i'th and j'th index have even number of digits (Even digit Concatenation)Sum of digits in the first half = sum of digits in the second half (Balanced Digit Sum)The task is to count all such valid pairs in the given array wi
9 min read
Arrange array elements such that last digit of an element is equal to first digit of the next element
Given an array arr[] of integers, the task is to arrange the array elements such that the last digit of an element is equal to first digit of the next element. Examples: Input: arr[] = {123, 321} Output: 123 321 Input: arr[] = {451, 378, 123, 1254} Output: 1254 451 123 378 Naive approach: Find all the permutations of the array elements and then pri
9 min read
Count of N digit numbers which contains all single digit primes
Given a positive integer n, the task is to count the number of n digits numbers which contain all single digit primes. Examples: Input: n = 4Output: 24Explanation: The number of single digit primes is 4 i.e.[2, 3, 5, 7]. Hence number of ways to arrange 4 numbers in 4 places is 4! = 24. Input: n = 5Output: 936 The simplest approach to solve the give
15+ min read
Count ways to place all the characters of two given strings alternately
Given two strings, str1 of length N and str2 of length M of distinct characters, the task is to count the number of ways to place all the characters of str1 and str2 alternatively. Note: |N - M| ? 1 Examples: Input: str1 =“ae”, str2 = “bd”Output: 8Explanations:Possible strings after rearrangements are : {“abed”, “ebad”, “adeb”, “edab”, “bade”, “bed
5 min read
Rearrange a given linked list in-place
Given a singly linked list L0 -> L1 -> … -> Ln-1 -> Ln. Rearrange the nodes in the list so that the newly formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ... You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 -> 4Output: 1 -> 4 -> 2 -> 3 Explanatio
10 min read
Javascript Program For Rearranging A Given Linked List In-Place
Given a singly linked list L0 -> L1 -> … -> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 -> 4Output: 1 -> 4 -> 2 -> 3Input: 1 ->
7 min read
Sort the given Matrix In-Place
Given a matrix of m rows and n columns, the task is to sort the matrix in the strict order that is every row is sorted in increasing order and the first element of every row is greater than the first element of the previous row. Input : mat[][] = { {5, 4, 7}, {1, 3, 8}, {2, 9, 6} }Output : 1 2 3 4 5 6 7 8 9 Input: mat[][] = { {5, 4, 7}, {1, 3, 8} }
9 min read
Total number of ways to place X and Y at n places such that no two X are together
Given N positions, the task is to count the total number of ways to place X and Y such that no two X are together. Examples: Input: 3Output: 5XYX, YYX, YXY, XYY and YYY Input: 4Output: 8XYXY, XYYX, YXYX, YYYX, YYXY, YXYY, XYYY and YYYY Approach: For N = 1, X and Y are 2 possible ways. For N = 2, XY, YX and YY are the 3 possible ways. For N = 3, XYX
5 min read
Practice Tags :
three90RightbarBannerImg