**Objective**: Given a number n, write a program to calculate Log_{2}n without using built-in function.

**Example**:

N = 32 Log_{2}32 = 5 N = 64 Log_{2}64 = 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:**

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

public class Logn { | |

static void computeLogN(int n){ | |

int result = 0; | |

int number = n; | |

while(n>0){ | |

n=n/2; | |

if(n>=1) | |

result++; | |

} | |

System.out.println("Log"+number + " value: " + result); | |

} | |

public static void main(String[] args) { | |

int n = 64; | |

computeLogN(n); | |

} | |

} |

**Output:**

Log64 value: 6