Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第三次作業 -- 遞迴 請寫出 n! 的遞迴程式 #3

Open
ccckmit opened this issue Apr 24, 2019 · 3 comments
Open

第三次作業 -- 遞迴 請寫出 n! 的遞迴程式 #3

ccckmit opened this issue Apr 24, 2019 · 3 comments

Comments

@ccckmit
Copy link
Contributor

ccckmit commented Apr 24, 2019

提示 : f(n) = ? f(n-1) ....

@istar0me
Copy link

istar0me commented Apr 24, 2019

iterate

function factorial(n) {
    let sum = 1;
    for (i=1; i<=n; i++) {
       sum *= i 
    }
    return sum
}
console.log(factorial(3)) // 6

recursion

function factorialR(n) {
    if (n <= 1) {
        return 1;
    } else { 
        return n * factorialR(n-1);
    }
}
console.log(factorialR(5)) // 120

@ChiaYuSu
Copy link

#include <stdio.h>
 
int factorial(int n);

int main()
{
  int number;
  printf("Please enter an integer: ");
  scanf("%d", &number);
  printf("%d! = %d\n", number, factorial(number));
  return 0;
}
 
int factorial(int n)
{
  int i;
  int total = 1;
  for (i = 1 ; i <= n ; i++) {
  	total = total * i;
  }
  return total;
}

@tian0108
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants