Saturday, December 28, 2024
Google search engine
HomeData Modelling & AIC program to find square root of a given number

C program to find square root of a given number

Given a number N, the task is to write a C program to find the square root of the given number N.

Examples: 

Input: N = 12 
Output: 3.464102

Input: N = 16 
Output:

Method 1: Using inbuilt sqrt() function: The sqrt() function returns the sqrt of any number N.

Below is the implementation of the above approach:

C




// C program for the above approach
#include <math.h>
#include <stdio.h>
 
// Function to find the square-root of N
double findSQRT(double N) { return sqrt(N); }
 
// Driver Code
int main()
{
 
    // Given number
    int N = 12;
 
    // Function call
    printf("%f ", findSQRT(N));
    return 0;
}


Output: 

3.464102

 

Time complexity: O(logN), as the inbuilt sqrt() function take log(n)
Auxiliary space: O(1) 

Method 2: Using Binary Search: This approach is used to find the square root of the given number N with precision 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




// C program for the above approach
#include <stdio.h>
#include <stdlib.h>
 
// Function to find the square-root of N
float findSQRT(int number)
{
    int start = 0, end = number;
    int mid;
 
    // To store the answer
    float ans;
 
    // To find integral part of square
    // root of number
    while (start <= end) {
 
        // Find mid
        mid = (start + end) / 2;
 
        // If number is perfect square
        // then break
        if (mid * mid == number) {
            ans = mid;
            break;
        }
 
        // Increment start if integral
        // part lies on right side
        // of the mid
        if (mid * mid < number) {
          //first start value should be added to answer
            ans=start;
          //then start should be changed
            start = mid + 1;
        }
 
        // Decrement end if integral part
        // lies on the left side of the mid
        else {
            end = mid - 1;
        }
    }
 
    // To find the fractional part
    // of square root upto 5 decimal
    float increment = 0.1;
    for (int i = 0; i < 5; i++) {
        while (ans * ans <= number) {
            ans += increment;
        }
 
        // Loop terminates,
        // when ans * ans > number
        ans = ans - increment;
        increment = increment / 10;
    }
    return ans;
}
 
// Driver Code
int main()
{
 
    // Given number
    int N = 12;
 
    // Function call
    printf("%f ", findSQRT(N));
    return 0;
}


Output: 

3.464099

 

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

Let d be our answer for input 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, 

d = pow(2, 0.5*log2(n)) 

Below is the implementation of the above approach:

C




// C program for the above approach
#include <math.h>
#include <stdio.h>
 
// Function to find the square-root of N
double findSQRT(double N) { return pow(2, 0.5 * log2(N)); }
 
// Driver Code
int main()
{
 
    // Given number
    int N = 12;
 
    // Function call
    printf("%f ", findSQRT(N));
    return 0;
}


Output: 

3.464102

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments