C/CPP program to find the square root in different ways


C program that uses binary search to find the square root of a given integer number:


Algorithm:

  1. Initialize the low and high variables to 0 and the input number respectively.
  2. While the difference between high and low is greater than 1, do the following:
    a. Calculate the mid point as the average of low and high.
    b. If mid*mid is greater than the input number, set high to mid.
    c. Else, set low to mid.
  3. Return the value of low as the closest square root of the input number.
Coding in C:

#include <stdio.h>
 
int squareRoot(int n)
{
    int low = 0;
    int high = n;
    int mid;
 
    while (high - low > 1) {
        mid = (low + high) / 2;
 
        if (mid * mid > n) {
            high = mid;
        } else {
            low = mid;
        }
    }
    return low;
}
 
int main()
{
    int number, result;
    printf("Enter a number: ");
    scanf("%d", &number);
 
    result = squareRoot(number);
    printf("Square root of %d is %d", number, result);
 
    return 0;
}
 
Coding in CPP/C++ to find the square root using binary search algorithm:

#include <iostream>
using namespace std;
 
int squareRoot(int n)
{
    int low = 0;
    int high = n;
    int mid;
 
    while (high - low > 1) {
        mid = (low + high) / 2;
 
        if (mid * mid > n) {
            high = mid;
        } else {
            low = mid;
        }
    }
    return low;
}
 
int main()
{
    int number, result;
    cout << "Enter a number: ";
    cin >> number;
 
    result = squareRoot(number);
    cout << "Square root of " << number << " is " << result << endl;
 
    return 0;
}
 
C program to find the square root of double value using binary search algorithm:

#include <stdio.h>
#include <math.h>
 
double squareRoot(double n)
{
    double x = n;
    double y = 1;
    double e = 0.00001;
    while(x - y > e)
    {
        x = (x + y)/2;
        y = n/x;
    }
    return x;
}
 
int main()
{
    double number, result;
    printf("Enter a number: ");
    scanf("%lf", &number);
 
    result = squareRoot(number);
    printf("Square root of %lf is %lf", number, result);
 
    return 0;
}
 
C program to find the square root of a number using sqrt() function:

#include <stdio.h>
#include <math.h>
 
int main()
{
    double number, result;
    printf("Enter a number: ");
    scanf("%lf", &number);
 
    result = sqrt(number);
    printf("Square root of %lf is %lf", number, result);
 
    return 0;
}
 
C program to find the square root of a number using the Newton-Raphson method:

#include <stdio.h>
#include <math.h>
 
double squareRoot(double n) {
    double x = n;
    double y = 1;
    double e = 0.00001;
    while(fabs(x - y) > e) {
        x = (x + y)/2;
        y = n/x;
    }
    return x;
}
 
int main() {
    double number, result;
    printf("Enter a number: ");
    scanf("%lf", &number);
 
    result = squareRoot(number);
    printf("Square root of %lf is %lf", number, result);
 
    return 0;
}
 
c program to find the square root of a number using Babylonian method:

#include <stdio.h>
#include <math.h>
 
double squareRoot(double n) {
    double x = n;
    double y = 1;
    double e = 0.00001;
    while(fabs(x - y) > e) {
        x = (x + y)/2;
        y = n/x;
    }
    return x;
}
 
int main() {
    double number, result;
    printf("Enter a number: ");
    scanf("%lf", &number);
 
    result = squareRoot(number);
    printf("Square root of %lf is %lf", number, result);
 
    return 0;
}
 
Do programming and enjoy.
#HappyCoding

No comments:

Post a Comment