w3resource

Python Exercises: Check square root and cube root of a number

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:

Flowchart: Python - Check square root and cube root of a number.

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:

Flowchart: Python - Check square root and cube root of a number.

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.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.