program to calculate square root of a number using sqrt()

Description

Square root of a number can be calculated using predefined sqrt() function.

sqrt() is a predefined function in math.h header file, it is used for 
calculate square root of any number.

C/C++

/* C Program to calculate square root of a number using - sqrt() */
//Save it as SquareRootOfNumberUsingSqrtFunction.c

#include<stdio.h>
#include<math.h>
int main(){

    double num, squareRoot;

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

    squareRoot = sqrt(num);

    printf("The square root of %lf is %lf", num, squareRoot);

    return 0;
}
Input:
Enter a number : 20

Output:
The square root of 20.0 is 4.47213595499958

Java

/* Java Program to calculate square root of a number using - sqrt() */
//Save it as SquareRootOfNumberUsingSqrtFunction.java

import java.io.*;
import java.util.Scanner;

public class SquareRootOfNumberUsingSqrtFunction {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter a number : ");
        double num = scanner.nextDouble();
        
        double squareRoot = Math.sqrt(num);
        
        System.out.println("The square root of "+num+" is "+squareRoot);
    }
}
Input:
Enter a number : 
20

Output:
The square root of 20.0 is 4.47213595499958

Related Programs

1) Program to calculate square root of a number without using standard library function sqrt()
2) Program to calculate power of a number using recursion
3) Program to Display Fibonacci Series using Recursion
4) Program to find HCF using Recursion
5) Program to find sum of digits of a number
6) Program to reverse a number
7) Program to convert Binary to Decimal
8) Program to convert Octal to Decimal
9) Program to calculate square root of a number without using standard library function sqrt()
10) Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor) and LCM(Least Common Multiple)
Share Me

Leave a Reply