Find the middle digit of a given Number

Given a number N, the task is to find the digit of the given number N. If the number has two middle digits then print the first middle digit.

Examples:

Input: N = 12345
Output: 3

Input: N = 98562
Output: 5

Approach: The middle digit of any number N can be given by \left ( \frac{N}{10^{\frac{len}{2}}} \right )\ mod 10.
The length(len) of the given Number can be calculated as  \left ( \log_{10} N \right ) + 1.
For example:



If N = 12345
len = (int)log10(12345) + 1 = 5
Therefore,
First half of N = N/105/2 = N/102 = 123
Therefor middle digit of N = last digit of First half of N = (First half of N) % 10 = 123 % 10 = 3

Below code is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to find middle digit of number
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the middle digit
int middleDigit(int n)
{
    // Find total number of digits
    int digits = (int)log10(n) + 1;
  
    // Find middle digit
    n = (int)(n / pow(10, digits / 2))
        % 10;
  
    // Return middle digit
    return n;
}
  
// Driver program
int main()
{
    // Given Number N
    int N = 98562;
  
    // Function call
    cout << middleDigit(N)
         << "\n";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find middle digit of number 
class GFG{
  
// Function to find the middle digit 
static int middleDigit(int n) 
      
    // Find total number of digits 
    int digits = (int)Math.log10(n) + 1
  
    // Find middle digit 
    n = (int)(n / Math.pow(10, digits / 2)) % 10
  
    // Return middle digit 
    return n; 
  
// Driver Code
public static void main(String[] args)
{
      
    // Given number N
    int N = 98562;
  
    // Function call
    System.out.println(middleDigit(N));
}
}
  
// This code is contributed by rutvik_56    

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find middle digit of number
import math
  
# Function to find the middle digit
def middleDigit(n):
  
    # Find total number of digits
    digits = math.log10(n) + 1;
  
    # Find middle digit
    n = int((n // math.pow(10, digits // 2))) % 10;
  
    # Return middle digit
    return n;
  
# Driver program
  
# Given Number N
N = 98562;
  
# Function call
print(middleDigit(N))
  
# This code is contributed by Code_Mech

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find middle digit of number
using System;
  
class GFG{
      
// Function to find the middle digit 
static int middleDigit(int n) 
          
    // Find total number of digits 
    int digits = (int)Math.Log10(n) + 1; 
      
    // Find middle digit 
    n = (int)(n / Math.Pow(10, digits / 2)) % 10; 
      
    // Return middle digit 
    return n; 
  
// Driver code
static void Main() 
{
          
    // Given number N 
    int N = 98562; 
      
    // Function call 
    Console.WriteLine(middleDigit(N)); 
}
}
  
// This code is contributed by divyeshrabadiya07    

chevron_right


Output:

5

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.