疯知识

C语言阶乘While和FOR

什么是阶乘?

n! = 1*2*3*4*...*n;

While循环

#include <stdio.h>

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


}

For循环

#include <stdio.h>
int main(){

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

}
Copyright © 2020 风也