Cara menggunakan python cube root integer

Last update on August 19 2022 21:50:48 [UTC/GMT +8 hours]

Python Basic - 1: Exercise-146 with Solution

A Python list contains two positive integers. Write a Python program to check whether the cube root of the first number is equal to the square root of the second number.

Sample Data:

[[8, 4]] -> True
[[64, 16]] -> True
[[64, 36]] -> False

Sample Solution-1:

Python Code:

def test[nums]:
    x = nums[0]
    y = nums[1]
    t = y**0.5
    if[x == t*t*t]:
        return True
    else:
        return False         
nums = [8, 4]
print["Original list of positive numbers:"]
print[nums]
print[test[nums]]
print["Check square root and cube root of the said numbers:"]
nums = [64, 16]
print["Original list of positive numbers:"]
print[nums]
print["Check square root and cube root of the said numbers:"]
print[test[nums]]
nums = [64, 36]
print["Original list of positive numbers:"]
print[nums]
print["Check square root and cube root of the said numbers:"]
print[test[nums]] 

Sample Output:

Original list of positive numbers:
[8, 4]
True
Check square root and cube root of the said numbers:
Original list of positive numbers:
[64, 16]
Check square root and cube root of the said numbers:
True
Original list of positive numbers:
[64, 36]
Check square root and cube root of the said numbers:
False

Flowchart:


Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

def test[nums]:
	return [[[nums[1] ** 0.5] ** 3] == nums[0]]

nums = [8, 4]
print["Original list of positive numbers:"]
print[nums]
print[test[nums]]
print["Check square root and cube root of the said numbers:"]
nums = [64, 16]
print["Original list of positive numbers:"]
print[nums]
print["Check square root and cube root of the said numbers:"]
print[test[nums]]
nums = [64, 36]
print["Original list of positive numbers:"]
print[nums]
print["Check square root and cube root of the said numbers:"]
print[test[nums]] 

Sample Output:

Original list of positive numbers:
[8, 4]
True
Check square root and cube root of the said numbers:
Original list of positive numbers:
[64, 16]
Check square root and cube root of the said numbers:
True
Original list of positive numbers:
[64, 36]
Check square root and cube root of the said numbers:
False

Flowchart:


Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code [and comments] through Disqus.

Previous: Largest and smallest digit of a given number.
Next: Sum of the digits in each number in a list is equal.

Python: Tips of the Day

Concatenating iterable to a single string:

>>> x = ["python","really", "rocks"]
>>> " ".join[x]
'python really rocks'

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.

Table of Contents

  • Write a program in python to enter a number and print its square and cube? 
  • Post a Comment
  • How do you calculate squares in Python?
  • How do you find the square root and square of a number in Python?
  • How do you find the cube of a number in Python?
  • How do you cube all numbers in a list in Python?

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 calculate squares in Python?

    To calculate the square of a number in Python, we have three different ways..

    By multiplying numbers two times: [number*number].

    By using Exponent Operator [**]: [number**2].

    Using math.pow[] method: [math.pow[number, 2]].

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

    sqrt[] function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.sqrt[x] Parameter: x is any number such that x>=0 Returns: It returns the square root of the number passed in the parameter.

    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 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ủ Đề