集册 Java实例教程 用于for语句以迭代方法计算阶乘。

用于for语句以迭代方法计算阶乘。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

500
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
用于for语句以迭代方法计算阶乘。

public class Main// 来 自 nowjava - 时  代  Java

{

   // recursive declaration of method factorial   

   public static long factorial(long number)

   {

      long result = 1;


      // iterative declaration of method factorial

      for (long i = number; i >= 1; i--)

         result *= i;


      return result;

   } 


   // output factorials for values 0-10

   public static void main(String[] args)

   {

      // calculate the factorials of 0 through 10

      for (int counter = 0; count
展开阅读全文