Print a square or cube of the given number

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

  1. Take a number as input through the user.
  2. If the number is divided by 2 then print its square.
  3. If the number is divided by 3 then print its square.
  4. Use if-else statement for this purpose.
  5. 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

validate user_id in the list of user_ids.

describe the interview process.

Leave a Comment