Calculate Log2n without using built-in function

Objective: Given a number n, write a program to calculate Log2n without using built-in function.

Example:

N = 32
Log232 = 5

N = 64
Log264 = 6

Approach:

  • Initialize result = 0.
  • Keep dividing the given number by 2 till number is greater than 0 and add one to the result if n is greater than equal to 1.

Java Code:

Output:

Log64 value: 6

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: