C program to find square root of a given number – GeeksforGeeks

Given a number N, the undertaking is to write a C program to find the square solution of the given number N. Examples:

Input: N = 12
Output: 3.464102 Input: N = 16
Output: 4

Method 1: Using inbuilt sqrt() function: The sqrt ( ) routine returns the sqrt of any count N. Below is the implementation of the above approach :

Contents
  1. C
  2. C
  3. C

C




#include #include double findSQRT( double N) { return sqrt (N); } int main() {           int N = 12;           printf ( "%f " , findSQRT(N));      return 0; }



Output: 

3.464102

Method 2: Using Binary Search : This approach is used to find the squarely root of the given count N with preciseness upto 5 decimal places .

  1. The square root of number N lies in range 0 ≤ squareRoot ≤ N. Initialize start = 0 and end = number.
  2. Compare the square of the mid integer with the given number. If it is equal to the number, then we found our integral part, else look for the same in the left or right side of mid depending upon the condition.
  3. After finding an integral part, we will find the fractional part.
  4. Initialize the increment variable by 0.1 and iteratively calculate the fractional part upto 5 decimal places.
  5. For each iteration, change increment to 1/10th of its previous value.
  6. Finally, return the answer computed.

Below is the implementation of the above approach :

C




#include #include float findSQRT( int number) {      int start = 0, end = number;      int mid;           float ans;                while (start <= end) {                   mid = (start + end) / 2;                            if (mid * mid == number) {              ans = mid;              break ;          }                                     if (mid * mid < number) {                         ans=start;                         start = mid + 1;          }         

                  else {              end = mid - 1;          }      }                float increment = 0.1;      for ( int i = 0; i < 5; i++) {          while (ans * ans <= number) {              ans += increment;          }                            ans = ans - increment;          increment = increment / 10;      }      return ans; } int main() {           int N = 12;           printf ( "%f " , findSQRT(N));      return 0; }



Output: 

3.464099

Method 3: Using log2() : The square-root of a numeral N can be calculated using log2 ( ) as :

Let five hundred be our answer for input signal number N, then d = N^{\frac{1}{2}}    Apply log2 ( ) both sides log2(d) = log2(N^{\frac{1}{2}})     log2(d) = {\frac{1}{2}}*log2(N)     d = 2^{{\frac{1}{2}}*log2(N)}    therefore, five hundred = prisoner of war ( 2, 0.5*log2 ( newton ) )

Below is the execution of the above approach :

C




#include #include double findSQRT( double N) { return pow (2, 0.5 * log2(N)); } int main() {           int N = 12;           printf ( "%f " , findSQRT(N));      return 0; }



Output: 

3.464102

My Personal Notes

arrow_drop_up

source : https://thefartiste.com
Category : Tech

About admin

I am the owner of the website thefartiste.com, my purpose is to bring all the most useful information to users.

Check Also

articlewriting1

How to Show Profile Picture Instead of Video in Zoom Meeting

Zoom is a democratic and utilitarian outside meet and television conferencing tool. however, not everyone …

Leave a Reply

Your email address will not be published. Required fields are marked *