How would you write a non-recursive algorithm to calculate factorials?

后端 未结 22 959
不思量自难忘°
不思量自难忘° 2020-12-10 11:39

How would you write a non-recursive algorithm to compute n!?

22条回答
  •  感情败类
    2020-12-10 12:26

    For a non-recursive approach, it can't get simpler than this

    int fac(int num) {
        int f = 1;
        for (int i = num; i > 0; i--)
            f *= i;
        return f;
    }
    

提交回复
热议问题