For a given number find the square root using log function. Number may be int, float or double.

Examples:

Input : n = 9 Output : 3 Input : n = 2.93 Output : 1.711724

We can find square root of a number using sqrt() method.

`// C++ program to demonstrate finding ` `// square root of a number using sqrt() ` `#include<bits/stdc++.h> ` ` ` `int` `main(` `void` `) ` `{ ` ` ` `double` `n = 12; ` ` ` `printf` `(` `"%lf "` `, ` `sqrt` `(n)); ` ` ` `return` `0; ` `} ` |

Output :

3.464102

**We can also find square root using log2() library function:**

`// C++ program to demonstrate finding ` `// square root of a number using log2() ` `#include<bits/stdc++.h> ` ` ` `double` `squareRoot(` `double` `n) ` `{ ` ` ` `return` `pow` `(2, 0.5*log2(n)); ` `} ` ` ` `int` `main(` `void` `) ` `{ ` ` ` `double` `n = 12; ` ` ` `printf` `(` `"%lf "` `, squareRoot(n)); ` ` ` `return` `0; ` `} ` |

Input :7 Output:2.645751

**How does above program work?**

let d be our answer for input number n then n^{(1/2)}= d apply log2 on both sides log2(n^{(1/2)}) = log2(d) log2(d) = 1/2 * log2(n) d = 2^{(1/2 * log2(n)) }d = pow(2, 0.5*log2(n))

This article is contributed by ** Tumma Umamaheswararao** from Jntuh College of Engineering . 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

**Practice Tags :**