Menu Close

C语言练习,利用条件语句计算一个整数的阶乘

阶乘:factorial of n (n!) = 1 * 2 * 3 * 4....n
#include <stdio.h>
int main() {
    int n, i;
    unsigned long long fact = 1;
    printf("Enter an integer: ");
    scanf("%d", &n);

    // shows error if the user enters a negative integer
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else {
        for (i = 1; i <= n; ++i) {
            fact *= i;
        }
        printf("Factorial of %d = %llu", n, fact);
    }

    return 0;
}

结果:

Enter an integer: 25
Factorial of 25 = 7034535277573963776
Process returned 0 (0x0) execution time : 4.852 s
Press any key to continue.
READ  C语言练习,检查一个字符是否在字母表内
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in C 决策和循环语句, C语言习题集

Related Posts