Skip to content

Find Cube Root Of A Number

Updated: at 09:17 PM

Table of contents

Open Table of contents

Write a program to find cube root of a number

Input  : 27
Output : 3

C

// Write a C program to find cube root of a number

#include <stdio.h>
#include <math.h>

int main()
{
    double num;

    printf("Enter a number: ");
    scanf("%lf", &num);

    double result = cbrt(num);

    printf("Cube root of %.2f is    :    %.2f\n", num, result);
}

C#

// Write a C# program to find cube root of a number

using System;
class MainClass {
    public static void Main (string[] args) {
    Console.WriteLine ("Please enter a number : ");
    double num = Convert.ToDouble(Console.ReadLine());
    double cubeRoot = Math.Cbrt(num);
    Console.WriteLine("Cube root of given number : "+cubeRoot);
  }
}

C++

// Write a C++ program to find cube root of a number

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{

    int input;

    cout << "enter a number:" << endl;
    cin >> input;

    cout << "cube root of " << input << " is " << cbrt(input) << endl;

    return 0;
}

Dart

// Write a Dart program to find cube root of a number

import 'dart:io';
import 'dart:math';
void main(){
  print("Enter a number :");
  var number =int.parse(stdin.readLineSync());
  var cube_root = pow(number,1/3).round();
  print('Cube root of the given number : $cube_root');
}

Go

// Write a Go program to find cube root of a number

package main

import (
	"fmt"
	"math"
)

func main() {
	var num float64
	fmt.Scanf("%f", &num)
	ans := math.Cbrt(num)
	fmt.Println(ans)
}

Haskell

-- Write a Haskell program to find cube root of a number

import Data.Fixed

cubeRoot :: Double -> Double
cubeRoot x = (cuberoot x) / 1
   where cuberoot = (** (1/3))

main :: IO ()
main = do
   let num = 27
   print (cubeRoot num)

Java

// Write a Java program to find cube root of a number

import java.util.Scanner;

public class FindCubeRootOfANumber {
    public static void main(String args[]){
        double i, precision = 0.000001;
        System.out.println("Enter a number:");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        for(i = 1; (i*i*i) <= num; ++i);
        for(--i; (i*i*i) < num; i += precision);
        System.out.println("Cube root of " + num + " is "+i);
    }
}

JavaScript

// Write a JavaScript program to find cube root of a number

function cubeRoot(num) {
  return Math.cbrt(num);
}

let number = 27;
console.log(`The cube root of ${number} is ${cubeRoot(number)}.`);

Julia

# Write a Julia program to find cube root of a number

# Read input from the user
print("Enter a number: ")
num = parse(Float64, readline())

# Calculate the cube root
cubeRoot = cbrt(num)

# Print the result
println("Cube root of $num is: $cubeRoot")

Perl

# Write a Perl program to find cube root of a number

print "Enter a number : \n";
$input = <STDIN>;
if($input<0){
$input*=-1;
$out=-1*$input**(1/3);
$input*=-1;
}
else{
$out=$input**(1/3);
}
&printout;

sub printout{
print "The cube root of $input is $out\n\n";
}

PHP

// Write a PHP program to find cube root of a number

<?php
$x = readline('Enter number to calculate cube root: ');

$x1 = $x**(1/3);
echo "Cube root of $x is $x1.\n";

?>

Python

# Write a Python program to find cube root of a number

print(round(int(input()) ** (1 / 3), 3))

R

# Write a R program to find cube root of a number

cuberoot = function(x){
    if(x < 0)
    { - (-x)^(1/3)}
    else
    {x^(1/3)}
    }

cuberoot(64)

Ruby

# Write a Ruby program to find cube root of a number

<?php

function cubeRoot($n) {
 return pow($n, 1/3) ;
}

echo cubeRoot(50);

?>

Rust

// Write a Rust program to find cube root of a number

fn main()
{
    let num: f32 = 27.0;
    let result = num.powf(1.0/3.0);

    println!("Cube root : {}", result);
}

Scala

// Write a Scala program to find cube root of a number

import scala.io.StdIn._;

object cubeRoot{
    def main(args:Array[String]):Unit={
        var num:Double    = 0;
        var res:Double = 0;

        print("Enter a number: ")
        num= readDouble()

        res= scala.math.cbrt(num)
        println("\nCube root of the given number : "+res);
    }
}

Swift

// Write a Swift program to find cube root of a number

import Swift
import Foundation

print("Enter number: ")
var number = Double(readLine()!)!

print ("Cube root : ", pow(number,1/3))

TypeScript

// Write a TypeScript program to find cube root of a number

function findCubeRootOfANumber(cube: number) {
  const cubeRot = Math.cbrt(cube);
  console.log(cubeRot);
}

findCubeRootOfANumber(1331);

Similar programs