# Find most significant bit of a number X in base Y

Given two positive integers X and Y, the task is to find the MSB of X, in the given base Y.

Examples:

Input: X = 55, Y = 3
Output: 2
Explanation:
55 is 2001 in base 3 with first digit as 2.

Input: X = 123, Y = 10
Output: 1
Explanation:
123 is 123 in base 10 with first digit 1.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Let the task to find 1st digit of X = 1234 in base Y = 10, So to get First digit = 1:

Divide 1234 by 1000
= X / 103
= X / 10Number of Digits in X – 1
= X / 10log(X) / log(10) (which is for base 10)

For any other base, we can replace 10 with Y. Therefore, we can calculate the first digit of a number X in base Y by using the formula:

X / Y(log(X)/log(Y))

Below is the implementation of the above approach:

## C++

 `// C++ Program to find the ` `// first digit of X in base Y ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the first ` `// digit of X in base Y ` `void` `first_digit(``int` `x, ``int` `y) ` `{ ` `    ``// calculating number of digits of ` `    ``// x in base y ` `    ``int` `length = ``log``(x) / ``log``(y) + 1; ` ` `  `    ``// finding first digit of x in base y ` `    ``int` `first_digit = x / ``pow``(y, length - 1); ` ` `  `    ``cout << first_digit; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `X = 55, Y = 3; ` ` `  `    ``first_digit(X, Y); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java Program to find the ` `// first digit of X in base Y ` `import` `java.util.*; ` `class` `GFG{ ` ` `  `// Function to find the first ` `// digit of X in base Y ` `static` `void` `first_digit(``int` `x, ``int` `y) ` `{ ` `    ``// calculating number of digits of ` `    ``// x in base y ` `    ``int` `length = (``int``)(Math.log(x) /  ` `                       ``Math.log(y) + ``1``); ` ` `  `    ``// finding first digit of x in base y ` `    ``int` `first_digit = (``int``)(x / Math.pow(y,  ` `                                ``length - ``1``)); ` ` `  `    ``System.out.println(first_digit); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `X = ``55``, Y = ``3``; ` ` `  `    ``first_digit(X, Y); ` `} ` `} ` ` `  `// This code is contributed by AbhiThakur `

## Python3

 `# Python3 program to find the  ` `# first digit of X in base Y  ` `import` `math ` ` `  `# Function to find the first  ` `# digit of X in base Y  ` `def` `first_digit(x, y): ` `     `  `    ``# Calculating number of digits of  ` `    ``# x in base y  ` `    ``length ``=` `int` `(math.log(x) ``/`  `                  ``math.log(y) ``+` `1``)  ` `     `  `    ``# Finding first digit of x in base y ` `    ``first_digit ``=` `x ``/` `math.``pow``(y, length ``-` `1``) ` ` `  `    ``print``(``int``(first_digit)) ` `     `  `# Driver code  ` `X ``=` `55` `Y ``=` `3` ` `  `first_digit(X, Y) ` ` `  `# This code is contributed by ishayadav181 `

## C#

 `// C# Program to find the ` `// first digit of X in base Y ` `using` `System; ` `class` `GFG{ ` ` `  `// Function to find the first ` `// digit of X in base Y ` `static` `void` `first_digit(``int` `x, ``int` `y) ` `{ ` `    ``// calculating number of digits of ` `    ``// x in base y ` `    ``int` `length = (``int``)(Math.Log(x) /  ` `                       ``Math.Log(y) + 1); ` ` `  `    ``// finding first digit of x in base y ` `    ``int` `first_digit = (``int``)(x / Math.Pow(y,  ` `                                ``length - 1)); ` ` `  `    ``Console.Write(first_digit); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `X = 55, Y = 3; ` ` `  `    ``first_digit(X, Y); ` `} ` `} ` ` `  `// This code is contributed by Akanksha_Rai `

Output:

```2
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.