Program to find square root of a number
In this post, you will learn how to write program to find the square root of a number in different programming languages. Such a type of question is generally asked in programming interviews.
In mathematics, the square root of a number is the number that we need to multiply by itself to get the original number. If a is the square root of b, it means that a×a=b.
Python program to find square root using sqrt() method
The sqrt() method returns the square root of a number. To use this method, first we need to import the math module, and then we need to call this function using the math static object. It accepts a number as an argument. The number must be greater than or equal to 0. If the number is less than 0, it returns a ValueError. If the value is not a number, it returns a TypeError.
import math
# Return the square root of different numbers
print(math.sqrt(9))
print(math.sqrt(25))
print(math.sqrt(16))
print(math.sqrt(30))
print(math.sqrt(5.9))
print(math.sqrt(3.7))
print(math.sqrt(13.65))
Output of the above code:
3.0
5.0
4.0
5.477225575051661
2.4289915602982237
1.9235384061671346
3.6945906403822333
Python program to find square root using pow() method
The predefined method pow() is also used to find out the power of a number. Before using this method, we need to import the math module. This function takes two arguments as input. The first is the number itself, and the second one is the power of that number.
import math
num = int(input("Enter a number:"))
sqrt = math.pow(num, 0.5)
print("square root: ", sqrt)
Output of the above code:
Enter a number:7
square root: 2.6457513110645907
Enter a number:8
square root: 2.8284271247461903
Java program to find square root using sqrt() method
In Java, we can use the standard library function sqrt() to find the square root of a number. We need to include the "java.lang.Math" library in the program to use this method.
import java.lang.Math;
public class SqRootProgram {
// driver code
public static void main(String args[])
{
double x = 20;
System.out.println(Math.sqrt(x));
x = 52;
System.out.println(Math.sqrt(x));
x = 32;
System.out.println(Math.sqrt(x));
x = 82;
System.out.println(Math.sqrt(x));
}
}
Output of the above code:
4.47213595499958
7.211102550927978
5.656854249492381
9.055385138137417
C program to find square root using sqrt() method
We can use the standard library function sqrt() to find the square root of a number. It accepts a single argument of type double and returns the square root of that number. As, the sqrt() is defined in the math.h library. We need to include the "math.h" header file in the program to use this method.
/**
* C program to get square root of a number
*/
#include <stdio.h>
#include <math.h>
int main()
{
double num, root;
/* Get number input from user */
printf("Enter any number: ");
scanf("%lf", &num);
/* Calculate square root of number */
root = sqrt(num);
/* Print output */
printf("Square root of %.2lf : %.2lf", num, root);
return 0;
}
Related Articles
Java program to find area of triangleArea of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Prime factors of a number in c
Armstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to convert Decimal to Octal
C program to convert decimal to binary
C program to check whether a number is even or odd