Java程式求給定數字的立方根


以下是一個例程,求給定數字的立方根。

程式

import java.util.Scanner;
public class FindingCubeRoot {
   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 the given number is "+i);
   }
}

輸出

Enter a number ::
125
Cube root of the given number is 5.0

更新於: 2020 年 6 月 19 日

1 千+瀏覽

開啟你的 職業生涯

透過完成此課程獲得認證

開始
廣告
© . All rights reserved.