Home » Python programs

Python | Calculate square of a given number (3 different ways)

Here, we are going to implement python programs to calculate square of a given number using 3 different ways.
Submitted by IncludeHelp, on August 12, 2018

Given a number, and we have to calculate its square in Python.

Example:

    Input:
    Enter an integer numbers: 8

    Output:
    Square of 8 is 64

Calculating square is a basic operation in mathematics; here we are calculating the square of a given number by using 3 methods.

  1. By multiplying numbers two times: (number*number)
  2. By using Exponent Operator (**): (number**2)
  3. By using math.pow() method: (math.pow(number,2)

1) By multiplying numbers two times: (number*number)

To find the square of a number - simple multiple the number two times.

Program:

# Python program to calculate square of a number
# Method 1 (using  number*number)

# input a number 
number = int (raw_input ("Enter an integer number: "))

# calculate square
square = number*number

# print
print "Square of {0} is {1} ".format (number, square)

Output

    Enter an integer number: 8
    Square of 8 is 64 

2) By using Exponent Operator (**): (number**2)

The another way is to find the square of a given number is to use Exponent Operator (**), it returns the exponential power. This operator is represented by **

Example: Statement m**n will be calculated as "m to the power of n".

Program:

# Python program to calculate square of a number
# Method 2 (using  number**2)

# input a number 
number = int (raw_input ("Enter an integer number: "))

# calculate square
square = number**2

# print
print "Square of {0} is {1} ".format (number, square)

Output

    Enter an integer number: 8
    Square of 8 is 64 

3) By using math.pow() method: (math.pow(number,2)

pow(m,n) is an inbuilt method of math library, it returns the value of "m to the power n". To use this method, we need to import math library in the program.

The statement to import math library is import math.

Program:

# Python program to calculate square of a number
# Method 3 (using math.pow () method)

# importing math library 
import math 

# input a number 
number = int (raw_input ("Enter an integer number: "))

# calculate square
square = int(math.pow (number, 2))

# print
print "Square of {0} is {1} ".format (number, square)

Output

    Enter an integer number: 8
    Square of 8 is 64 





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL








Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.