How to Find the Square Root using Python Program
In this session, we are going to learn to find the square root of a number using exponent operator and cmath module and positive real numbers. Python is a simple programming language. We can learn easily. Ok let's start the article to find the square root of a number .
Program Highlights
- First program to find the square root of a number.
- Operator and Module-Exponent operator and cmath Module
- First program to find the square root of a number usin the ** exponent operator
Program-1 -Find the Square Root
From this program, we are going to learn to find the square root of a number using exponent operator and cmath module. Also Read Program to check whether the input character is an alphabetSource Code
# Python Program to calculate the square root
# Note: change this value for a different result
num = 9
# To get the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
Output
The square root of 8.000 is 3.000
Program-2
From this program, we are going to store the number in num and find the square root using the ** exponent operator. This program works for all positive real numbers. But for negative or complex numbers, it can be done as follows.Also Read Python Program to Check Vowel or Consonant
# Find square root of real or complex numbers# Importing the complex math moduleimport cmathnum = 2+6j# To get input from the user#num = eval(input('Enter a number: '))num_sqrt = cmath.sqrt(num)print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))
Output
The square root of (2+6j) is 2.040+1.470j