Cube of a number in python
Show
The cube root means a number that produces a given number when cubed. The cube root of a number is a value that, when multiplied thrice or three times, produces the original value. The cube root symbol is denoted by ‘3√’. In the case of square root, we have used just the root symbol such as ‘√’, also called a radical. For example, the cube root of 64, denoted as 3√64, is 4 because when we multiply 4 by itself three times, we get 4 x 4 x 4 = 64 = 43. To find the cube root in Python, use the simple math equation: x ** (1. / 3). It computes the (floating-point) cube root of x. It is a simple math equation takes the cube root of x, rounds it to the nearest integer, raises it to the third power, and checks whether the result equals x. x = 27 cr = x ** (1./3.) print(cr) Output3.0 To calculate the exponent number in Python, we use the **. The double star(**) is also called the power operator. To calculate the cube root, we can set the power equal to 1/3. Find a cube root of a negative number in Python.To find the cube root of a negative number in Python, first, use the abs() function, and then you can use the simple math equation to calculate the cube root. We can not find the cube root of the negative numbers the way we calculated for the above method. For example, the cube root of integer -27 should be -3, but Python returns 1.5000000000000004+2.598076211353316j. Let’s write a complete function that will check if the input number is negative, then it will calculate accordingly. def cuberoot(x): if x < 0: x = abs(x) cube_root = x**(1/3)*(-1) else: cube_root = x**(1/3) return cube_root print(cuberoot(27)) print(round(cuberoot(-27))) Output3.0 -3 As you can see, we need to round the result to get the cube root’s exact value. Using Numpy cbrt() functionTo find a cube root in numpy, use the numpy.cbrt() method. The np.cbrt() function returns the cube root of every element of the array. Numpy cbrt() is a mathematical method that is used to find the cube root of every element in a given array. import numpy as np arr1 = [1, 8, 27, 64] arr2 = np.cbrt(arr1) print(arr2) Output[1. 2. 3. 4.] The np.cbrt() function is the easiest method to calculate a number’s cube root. It does not get in trouble with negative inputs and returns the exact number like 4 for input 64, unlike the above approaches. That is it for this tutorial. Related postsPython sqrt np.cbrt() Python sum In this post, we will learn how to find the cube of a number using Python Programming language. The number that is obtained by multiplying an integer to itself three times is known as the cube of a number. For example: The cube of 2 is 2 x 2 x 2 = 8. We will be using the following ways to find the cube of a number.
So, without further ado, let’s begin this tutorial.
# Python Program to Find Cube of a Number num = int(input("Enter an integer: ")) # Calculating cube cube = num * num * num # Displaying output print("Cube of {0} is {1}" .format(num, cube)) Output
How Does This Program Work ?num = int(input("Enter an integer: ")) In this program, the user is asked to enter an integer. # Calculating cube cube = num * num * num Then, we calculate the cube of the entered number by multiplying the number itself by three times. # Displaying output print("Cube of {0} is {1}" .format(num, cube)) Finally, the cube of the number is displayed on the screen using print() function. Python Program to Find Cube of a Number Using Exponent# Python Program to Find the Cube of a Number Using Exponent num = int(input("Enter an integer: ")) # Calculating cube cube = num ** 3 # Displaying output print("Cube of {0} is {1}" .format(num, cube)) Output
Python Program to Find Cube of a Number Using Functions# Python Program to Find Cube of a Number Using Functions def cube(num): return num * num * num num = int(input("Enter an number: ")) # Calling out function cube_num = cube(num) # Displaying output print("Cube of {0} is {1}" .format(num, cube_num)) Output
ConclusionI hope after going through this post, you understand how to find the cube of a number using Python Programming language. If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query. Also Read:
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 square or cube 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 draw a cube in Python?It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle (pen).. First draw the front square.. Move to back square through one bottom left side.. Draw the back square.. Draw the remaining side as shown in code.. 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]. |