Write a program to find square and cube of a number in python

In this post, we will see how to calculate the square and cube of every number of the given list of numbers. We will use map[] along with lambda expression for calculation.

Algorithm
Step 1: Declare a list of numbers
Step 2: Find the square of number by multiplying the number itself two times.
Step 3: Print the numbers with square value
Step 4: Find the cube of number by multiplying the number itself three times.
Step 5: Print the numbers with cube value
Step 6: End
Example

Input
[1, 2, 3, 4, 5]
Output
Square of every number of the provided list:
[1, 4, 9, 16, 25]
Cube of every number of the provided list:
[1, 8, 27, 64, 125]

Program

numbers = [1, 2, 3, 4, 5]
print["List of numbers:"]
print[numbers]
print["\nSquare of every number of the provided list:"]
square_numbers = list[map[lambda x: x ** 2, numbers]]
print[square_numbers]
print["\nCube of every number of the provided list:"]
cube_numbers = list[map[lambda x: x ** 3, numbers]]
print[cube_numbers]

Output

List of numbers:
[1, 2, 3, 4, 5]

Square of every number of the provided list:
[1, 4, 9, 16, 25]

Cube of every number of the provided list:
[1, 8, 27, 64, 125]

HomePython ProgrammingPrint Square and Cube of Given number in Python

Write a program in python to enter a number and print its square and cube? 

num=int[input["Enter any integer number: "]]

sq=num**2

print["Square of Given number: ",sq]

cube=num**3

print["Cube of Given number: ",cube]

Output:-

Enter any integer number: 5

Square of Given number:  25

Cube of Given number:  125

Post a Comment

0 Comments

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a number, the task is to write a Python program to calculate square of the given number.

    Examples:

    Input: 4
    Output: 16
    
    Input: 3
    Output: 9
    
    Input: 10
    Output: 100

    We will provide the number, and we will get the square of that number as output. We have three ways to do so:

    • Multiplying the number to get the square [N * N]
    • Using Exponent Operator
    • Using math.pow[] Method

    Method 1: Multiplication

    In this approach, we will multiply the number with one another to get the square of the number.

    Example:

    Python3

    n = 4

    square = n * n

    print[square]

    Output:

    16

    Method 2: Using Exponent Operator

    In this approach, we use the exponent operator to find the square of the number.

    Exponent Operator: **

    Return: a ** b will return a raised to power b as output.

    Example:

    Python3

    n = 4

    square = n ** 2

    print[square]

    Output:

    16

    Method 3: Using pow[] Method

    In this approach, we will use the pow[] method to find the square of the number. This function computes x**y and returns a float value as output.

    Syntax: float pow[x,y]

    Parameters :

    x : Number whose power has to be calculated.

    y : Value raised to compute power.
     

    Return Value : 
    Returns the value x**y in float.

    Example:

    Python3

    n = 4

    square = pow[n, 2]

    print[square]

    Output:

    16.0

    How do you find the square and cube of a number in Python?

    Python Write functions to find the square and cube of a given....
    Example: Input: Enter an integer number: 6 Output: Square of 6 is 36 Cube of 6 is 216..
    Function to get square: def square [num]: return [num*num].
    Function to get cube: def cube [num]: return [num*num*num].

    How do you find the cube of a number in Python?

    Python Program to Find Cube of a Number.
    def cube[x]:.
    return x * x * x..
    n = int[input[" Enter the number : "]].
    cube1 = cube[n].
    print["The Cube of {0} = {1}". format[n, cube1]].

    How do you find a square number in Python?

    You can also find the square of a given number using the exponent operator in python. It is represented by "**". While applying this method, the exponent operator returns the exponential power resulting in the square of the number.

    How do you cube all numbers in a list in Python?

    Given a list, the task is to write a python program to cube all the list elements..
    Input: [1, 2, 3, 4].
    Output: [1, 8, 27, 64].
    Explanation: Cubing all the list elements..
    Input: [2, 4, 6].
    Output: [8, 64, 216].

    Bài mới nhất

    Chủ Đề