天天看點

計算一個數的階乘

while語句

# include<stdio.h>

int main()
{
    int n = 1;
    
    scanf("%d", &n);
    
    int fact = 1;
    
    int i = 1;
    while(i<=n){
        fact*=i;
        i++;
    }
    
    printf("%d!=%d\n", n, fact);
    
    return 0;
}
           

for語句

# include<stdio.h>

int main()
{
    int n = 1;
    
    scanf("%d", &n);
    
    int fact = 1;
    
    int i = 1;
    for(i=1; i<=n; i++){
        fact*=i;
    }
    
    printf("%d!=%d\n", n, fact);
    
    return 0;
}
           

繼續閱讀