In this python if else program, we will print a square or cube if the given number is divided by 2 or 3 respectively.
To find the square of a number we need to multiply that number by itself.
To find the cube of a number we need to multiply that number by itself 3 times.
Steps to solve the program
- Take a number as input through the user.
- If the number is divided by 2 then print its square.
- If the number is divided by 3 then print its square.
- Use if-else statement for this purpose.
- Print square or cube.
# Take input through the user
num = int(input("Enter a number: "))
# Check for division
# If divisible by 2 then print square
# If divisible by 3 then print cube
if num%2 == 0:
# Print output
print("Sqaure: ",num**2)
elif num%3 == 0:
# Print output
print("Cube: ",num**3)
Output :
Enter a number: 9
Cube: 729
Related Articles
Python program to describe the interview process.
Python program to determine whether a given number is available in the list of numbers or not.
Python program to find the largest number among three numbers.
Python program to check any person eligible to vote or not.
Python program to check whether any given number is a palindrome.
Python program to check if any given string is palindrome or not.