📜  C程序查找给定数字的平方根

📅  最后修改于: 2021-05-26 01:18:17             🧑  作者: Mango

给定数字N ,任务是编写一个C程序以找到给定数字N的平方根。

例子:

方法1:使用内置的sqrt()函数: sqrt()函数返回任意数字N的sqrt。

下面是上述方法的实现:

C
// C program for the above approach
#include 
#include 
 
// 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;
}


C
// C program for the above approach
#include 
#include 
 
// 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;
}


C
// C program for the above approach
#include 
#include 
 
// 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;
}


输出:
3.464102

方法2:使用Binary Search 此方法用于查找给定数字N的平方根,精度最高为5个小数位。

  1. N的平方根的范围是0≤squareRoot≤N。初始化start = 0和end = number。
  2. 将中间整数的平方与给定数字进行比较。如果等于数字,那么我们找到了整数部分,否则根据条件在中间的左侧或右侧寻找相同的部分。
  3. 找到不可分割的部分后,我们将找到小数部分。
  4. 用0.1初始化增量变量并迭代计算小数位数直到5位小数
  5. 对于每次迭代,将增量更改为其先前值的1/10
  6. 最后,返回计算出的答案。

下面是上述方法的实现:

C

// C program for the above approach
#include 
#include 
 
// 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;
}
输出:
3.464099

方法3:使用log2() 可以使用log2()计算数字N的平方根:

下面是上述方法的实现:

C

// C program for the above approach
#include 
#include 
 
// 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;
}
输出:
3.464102

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。