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

更新時間: 19-06-2020

1 千次以上瀏覽

開啟你的 職業生涯

完成課程後獲得認證

開始學習
廣告
© . All rights reserved.