Home »
Python »
Python Programs
Find Square and Cube of a Number in Python
In this tutorial, we will learn how to find the factorial of a given number using Python program?
By IncludeHelp Last updated : January 05, 2024
Problem Statement
Given a number, and we have to write user defined function to find the square and cube of the number is Python.
How to Find Square and Cube of a Number?
To find the square and cube of a number, we can use simple mathematical calculations. To find the square, multiply the number by itself, and to find the cube, multiply the number by itself three times.
Consider the below example,
Number | Square | Cube |
2 | 2*2 = 4 | 2*2*2 = 8 |
3 | 3*3 = 9 | 3*3*3 = 27 |
4 | 4*4 = 16 | 4*4*4 = 64 |
5 | 5*5 = 25 | 5*5*5 = 125 |
6 | 6*6 = 36 | 6*6*6 = 216 |
Sample Input/Output
Input:
Enter an integer number: 6
Mathematical calculations:
Square = 6*6 = 36
Cube = 6*6*6 = 216
Output:
Square of 6 is 36
Cube of 6 is 216
User-defined functions to find factorial of a number
The following code snippets / user-defined functions can be used to calculate the square and cube of a number.
User-defined function to find square
def square (num):
return (num*num)
User-defined function to find cube
def cube (num):
return (num*num*num)
Python program to find square and cube of a number
# python program to find square and cube
# of a given number
# User defind method to find square
def square(num):
return num * num
# User defind method to find cube
def cube(num):
return num * num * num
# Main code
# input a number
number = int(input("Enter an integer number: "))
# square and cube
print("square of {0} is {1}".format(number, square(number)))
print("Cube of {0} is {1}".format(number, cube(number)))
Output
Enter an integer number: 6
square of 6 is 36
Cube of 6 is 216
To understand the above program, you should have the basic knowledge of the following Python topics:
Python Basic Programs »