×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

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,

NumberSquareCube
22*2 = 42*2*2 = 8
33*3 = 93*3*3 = 27
44*4 = 164*4*4 = 64
55*5 = 255*5*5 = 125
66*6 = 366*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 »

Advertisement
Advertisement

Related Programs

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.