# 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.

- By multiplying numbers two times: (number*number)
- By using Exponent Operator (**): (number**2)
- 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

Recommended posts

- Python | Write functions to find square and cube of a given number.
- Python program to find the power of a number using loop
- Python program to find the power of a number using recursion
- Python program to find addition of two numbers (4 different ways)
- Python | Find factorial of a given number (2 different ways).
- Python program for simple interest
- Python program for compound interest
- Python program to check the given year is a leap year or not
- Simple pattern printing programs in Python
- Create a function to check EVEN or ODD in Python
- Create a function to return the absolute the given value in Python
- Python program to find power of a number using exponential operator
- Python program to reverse a given number (2 different ways)
- Python program to find floor division
- Python | Calculate discount based on the sale amount.
- Python | Calculate discount based on the sale amount using Nested if else.
- Python | Design a simple calculator using if elif (just like switch case)
- Python | Find the factorial of a number using recursion
- Python | Compute the net amount of a bank account based on the transactions.
- Python program to check prime number

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions