2016-07-06 6 views
2

myFactorial関数を作成した後に階乗を計算するコードを書きましたが、大きな数値を処理しようとしたときにそれを乱しました。ファクタシアルを計算する際の長形式の処理

私は長い形式を使用して16を超える数値を扱おうとしていますが、結果は無関係で、コードの第2部分では奇妙なものになります。 結果は入力で変更する必要はありませんが、変更は必要です。

私は以下のコメントで自分のコードを共有:

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

あなたは直面している問題を詳しく教えていただけますか?階乗出力は正しい:http://cpp.sh/8twwh –

+0

こんにちは、長いlongを使用すると、long rangeのオーバーフローも発生します。あなたが本当に学ぶことを望むなら、これを通過してください。 http://www.geeksforgeeks.org/factorial-large-number/ または https://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-チュートリアル –

答えて

2

あなたのprintf形式に問題があります。

HEX値が正しい値に印刷する%llXを使用する必要があります印刷しlong long int

printfためmyFactorialの戻り値は%lld形式を使用する必要があります印刷しているすべてのprintf

//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オプションを追加する。あなたのコードは限り階乗が最大long long int

のために許可されていること 9223372036854775807その後、小さいとして正しく動作することを -Wall -Wextra -pedantic

をメモ:それはあなたの

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)); 

ベストは、以下のすべてのオプションを追加することが表示されます

これは、階乗を計算できることを意味します。x <= 20

関連する問題