{"id":24161,"date":"2021-10-14T08:33:42","date_gmt":"2021-10-14T03:03:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=24161"},"modified":"2021-11-05T20:54:23","modified_gmt":"2021-11-05T15:24:23","slug":"how-to-find-cube-root-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-find-cube-root-in-python\/","title":{"rendered":"How to Find Cube Root in Python"},"content":{"rendered":"
In the previous article, we have discussed Python Program for isfinite() Function<\/a> Cube root:<\/strong><\/p>\n A cube root is a number that, when cubed, yields a given number. A number’s cube root is a value that, when multiplied by itself three times, yields the original value.<\/p>\n \u20183\u221a\u2019 represents the cube root symbol. In the case of square root, we simply used the root symbol, such as \u2018\u221a\u2019, which is also known as a radical.<\/p>\n For example :<\/p>\n The cube root of 27 is 3. (3*3*3=27)<\/p>\n Cube root of a Negative Number:<\/strong><\/p>\n We cannot find the cube root of negative numbers using the method described above. The cube root of integer -27, for example, should be -3, but Python returns 1.5000000000000004+2.598076211353316j.<\/p>\n To calculate the cube root of a negative number in Python, first, use the abs() function, and then use the simple math equation.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to calculate the cube root of a given number in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples<\/a>.<\/p>\n In the previous article, we have discussed Python Program for isfinite() Function Given a number and the task is to calculate the cube root of a given number in Python. Cube root: A cube root is a number that, when cubed, yields a given number. A number’s cube root is a value that, when multiplied …<\/p>\n
\nGiven a number and the task is to calculate the cube root of a given number in Python.<\/p>\nGiven Number = 125<\/pre>\n
The cube root of the given number { 125 } = 4.999999999999999<\/pre>\n
Given Number = -27<\/pre>\n
The cube root of the given number { -27 } = -3.0<\/pre>\n
Program for How To Find Cube Root in Python<\/h2>\n
\n
Method #1: Using Power Operator (Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_numb = 125\r\n# Check if the given is less than 0 or not using the if conditional statement.\r\nif gvn_numb < 0:\r\n # If it is true, then get the absolute value of the given number using the\r\n # abs() function and store it in the another variable.\r\n abs_numbr = abs(gvn_numb)\r\n # Calculate the value of the given number raised to the power (1\/3) and\r\n # multiplied with -1 to get the cube root of the given number.\r\n # Store it in another variable say rslt_cuberoott.\r\n rslt_cuberoott = abs_numbr**(1\/3)*(-1)\r\nelse:\r\n # Else, calculate the value of a given number raised to the power (1\/3) to\r\n # get the cube root of the given number.\r\n # Store it in the same variable say rslt_cuberoott.\r\n rslt_cuberoott = gvn_numb**(1\/3)\r\n# Print the cube root of the given number\r\nprint(\"The cube root of the given number {\", gvn_numb, \"} = \", rslt_cuberoott)\r\n<\/pre>\n
The cube root of the given number { 125 } = 4.999999999999999<\/pre>\n
Method #2: Using Power Operator (User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and \r\n# store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Check if the given is less than 0 or not using the if conditional statement.\r\nif gvn_numb < 0:\r\n # If it is true, then get the absolute value of the given number using the\r\n # abs() function and store it in the another variable.\r\n abs_numbr = abs(gvn_numb)\r\n # Calculate the value of the given number raised to the power (1\/3) and\r\n # multiplied with -1 to get the cube root of the given number.\r\n # Store it in another variable say rslt_cuberoott.\r\n rslt_cuberoott = abs_numbr**(1\/3)*(-1)\r\nelse:\r\n # Else, calculate the value of a given number raised to the power (1\/3) to\r\n # get the cube root of the given number.\r\n # Store it in the same variable say rslt_cuberoott.\r\n rslt_cuberoott = gvn_numb**(1\/3)\r\n# Print the cube root of the given number\r\nprint(\"The cube root of the given number {\", gvn_numb, \"} = \", rslt_cuberoott)\r\n<\/pre>\n
Enter some random number = -27\r\nThe cube root of the given number { -27 } = -3.0<\/pre>\n
\n