2011年1月19日 星期三

[C]遞迴求n!

#include <iostream.h>
long factorial(int n){
    if(n==1)
        return 1;  //遞迴結束條件
    else
        return n*factorial(n-1);
    }
int main(){
    int no;
    cin >> no;
    if(no>0){
        cout << factorial(no);
        }
//-----------------------
    printf("\n");
    system("PAUSE");
    return EXIT_SUCCESS;
}

 

沒有留言:

張貼留言