计算阶乘

时间:2016-07-06 06:35:05

标签: c function long-integer factorial

我编写了一个代码,在创建myFactorial函数后计算阶乘,但是当我试图处理更多数字时,我把它搞砸了。

我正在尝试使用长格式处理16以上的数字,但结果是无关紧要的,并且在代码的第二部分中事情变得更加怪异。 虽然结果不应随输入而改变,但它们确实会发生变化!

我通过以下评论分享我的代码:

的main.c

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int yourNumber;
    int i;

    //Take the input
    printf("I highly recommend you to make the command window fullscreen. Otherwise, the complete output will not be seen.\n");
    printf("Enter a positive integer and I will tell you its factorial.\n");
    scanf("%d", &yourNumber);

    //Calculate factorial and print it in three ways
    printf("Factorial of %d is %d!\n",yourNumber,myFactorial(yourNumber));
    printf("Using long format, factorial of %d is %lld!\n",yourNumber,myFactorial(yourNumber));
    printf("In hex: %#08X!",myFactorial(yourNumber));




    // Here on, second part of my code begins


    // Calculate and print factorials of numbers from 1 to 20 in %d format
    printf("\n\n\nLet's see more d's!\n\n");
    for (i = 1; i<21; i++)   printf("%d\n", myFactorial(i));

    // Calculate and print factorials of numbers from 1 to 20 in %lld format
    printf("\n\n\nNow let's see more lld's!\n\n");
    for (i = 1; i<21; i++)   printf("%lld\n", myFactorial(i));


    return 0;
}

myfactorial.c

#include <stdio.h>

long long int myFactorial(int bar) {
    long long out = 1;
    int i;
    for (i=1; i<=bar; i++)
    {
        out *= i;
    }
    return out;
}

1 个答案:

答案 0 :(得分:2)

您的printf格式存在问题:

打印myFactorial返回值的所有printf必须使用%lld格式,long long int

打印HEX值的

printf必须使用%llX来打印正确的值

//Calculate factorial and print it in three ways
printf("Factorial of %d is %lld!\n",yourNumber,myFactorial(yourNumber));
printf("Using long format, factorial of %d is %lld!\n",yourNumber,myFactorial(yourNumber));
printf("In hex: %#016llX!",myFactorial(yourNumber));

// Here on, second part of my code begins

// Calculate and print factorials of numbers from 1 to 20 in %d format
printf("\n\n\nLet's see more d's!\n\n");
for (i = 1; i<21; i++)   printf("%lld\n", myFactorial(i));

// Calculate and print factorials of numbers from 1 to 20 in %lld format
printf("\n\n\nNow let's see more lld's!\n\n");
for (i = 1; i<21; i++)   printf("%lld\n", myFactorial(i));

使用gcc编译时,您可以简单地找到添加-Wall选项的那种错误。它会告诉你

test.c: In function ‘main’:
test.c:84:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long long int’ [-Wformat=]
     printf("Factorial of %d is %d!\n",yourNumber,myFactorial(yourNumber));
     ^
test.c:86:5: warning: format ‘%X’ expects argument of type ‘unsigned int’, but argument 2 has type ‘long long int’ [-Wformat=]
     printf("In hex: %#08X!",myFactorial(yourNumber));
     ^
test.c:94:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long long int’ [-Wformat=]
     for (i = 1; i<21; i++)   printf("%d\n", myFactorial(i));

最好添加以下所有选项:-Wall -Wextra -pedantic

  

请注意,只要因子9223372036854775807小于long long int所允许的最大值,您的代码才能正常工作

这意味着您可以计算x <= 20

所在的阶乘