Program in C to Find Square Root of a Number

Program in C to Find Square Root of a Number

Finding the square root of a number is a common mathematical operation. The square root of a number is a value that, when multiplied by itself, gives the original number. This program demonstrates how to calculate the square root using different methods in C, including the sqrt() function from the math library and an approximation using simple arithmetic.

Understanding The Problem

The program should take a number as input from the user and return its square root. It should handle positive numbers correctly, as negative numbers do not have real square roots in standard arithmetic. The program can use built-in functions or implement an algorithm for learning purposes.

Steps to Solve the Problem:

  • Take a number as input from the user.
  • Use a method to calculate the square root.
  • Display the result.

Solution 1: Using the sqrt() Function

The easiest and most accurate way to calculate a square root in C is using the sqrt() function from the <math.h> library.

#include <stdio.h>
#include <math.h>

int main() {

    double number, root;

    printf("Enter a number: ");
    scanf("%lf", &number);

    if(number < 0) {
        printf("Square root of negative numbers is not real.\n");

    } else {

        root = sqrt(number);
        printf("Square root of %.2lf is %.2lf\n", number, root);

    }

    return 0;

}

The program first checks if the number is negative. If not, it calculates the square root using sqrt() and prints the result. This method is precise and simple.

Solution 2: Using the Exponent Operator (pow())

Another approach is to use the pow() function, which can calculate any power, including fractional powers.

#include <stdio.h>
#include <math.h>

int main() {

    double number, root;

    printf("Enter a number: ");
    scanf("%lf", &number);

    if(number < 0) {
        printf("Square root of negative numbers is not real.\n");

    } else {

        root = pow(number, 0.5);
        printf("Square root of %.2lf is %.2lf\n", number, root);

    }

    return 0;

}

Raising a number to the power of 0.5 is equivalent to finding its square root. This method is flexible for other fractional powers.

Solution 3: Using the Babylonian (Newton-Raphson) Method

This method is an approximation technique useful for learning algorithms. It repeatedly improves a guess until it converges to the square root.

#include <stdio.h>

int main() {

    double number, guess, epsilon = 0.00001;

    printf("Enter a number: ");
    scanf("%lf", &number);

    if(number < 0) {
        printf("Square root of negative numbers is not real.\n");

    } else {

        guess = number / 2; // Initial guess

        while((guess * guess - number) > epsilon || (number - guess * guess) > epsilon) {
            guess = (guess + number / guess) / 2;
        }

        printf("Approximate square root of %.2lf is %.5lf\n", number, guess);

    }

    return 0;

}

The Babylonian method iteratively improves the guess using the formula (guess + number/guess)/2 until the difference between guess*guess and the number is very small.

FAQs

Q1: Can we find the square root of negative numbers?
No, in standard arithmetic, negative numbers do not have real square roots. Complex numbers are needed to represent them.

Q2: Which method is best?
The sqrt() function is the simplest and most precise. The pow() function is flexible for fractional powers. The Babylonian method is useful for learning iterative approximation techniques.

Q3: Can this program handle decimals?
Yes, using double or float allows the program to handle decimal numbers accurately.

Conclusion

This program demonstrates multiple ways to calculate the square root of a number in C. Using sqrt() is straightforward and reliable, pow() provides flexibility, and iterative methods like Babylonian are useful for learning algorithmic approaches. The logic can be applied to solve related problems involving roots and powers.

References & Additional Resources

Scroll to Top