Найдите среднюю цифру данного числа
Учитывая число N, задача состоит в том, чтобы найти среднюю цифру данного числа N. Если в номере две средние цифры, выведите первую среднюю цифру.
Примеры:
Input: N = 12345
Output: 3Input: N = 98562
Output: 5
Подход: средняя цифра любого числа N может быть задана как
Длина ( len ) данного числа может быть рассчитана как
Например:
If N = 12345
len = (int)log10(12345) + 1 = 5
Therefore,
First half of N = N/105/2 = N/102 = 123
Therefore 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++
// 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) << "
" ; return 0; } |
Java
// 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 |
Python3
# 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 |
C#
// 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 |
Javascript
<script> // Program to find middle digit of number // Function to find the middle digit function middleDigit(n) { // Find total number of digits let digits = parseInt(Math.log10(n) + 1); // Find middle digit n = parseInt(parseInt(n / Math.pow(10, parseInt(digits / 2))) % 10); // Return middle digit return n; } // Driver program // Given Number N let N = 98562; // Function call document.write(middleDigit(N)); // This code is contributed by subham348. </script> |
5
Сложность времени: O (1)
Вспомогательное пространство: O (1)
Вниманию читателя! Не прекращайте учиться сейчас. Получите все важные математические концепции для соревновательного программирования с курсом Essential Maths for CP по доступной для студентов цене. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .