C recursive function to calculate Factorial

JackV

Im just beginning to learn C programming and figured i would start out at a pretty basic problem of calculating the factorial of a number. My code outputs the correct value up until the factorial of 13 and then gives me the wrong answer for when the input is >13. My code is:

#include<stdio.h>
long int factorial(int);

int main()

{
    int num;
    long int fact;
    printf("Please type the number you want factoralized: ");
    scanf("%d",&num);

    fact = factorial(num);
    printf("%d",fact);
    return 0;
}
long int factorial(int dig)
{
    long int facto;
    if (dig>1)
        facto = dig * factorial(dig-1);
    else if (dig=1)
        facto = 1;
    return facto;
}

When i input 13 it returns 1932053504 instead of the expected 6227020800

P.P

You are probably overflowing the LONG_MAX value on your platform which leads to undefined behaviour. You can use unsigned long (or unsigned long long) but they wouldn't hold for much longer either.

Your options are limited here. You could use libraries, such as GNU GMP that support arbitrarily large integers. Otherwise, you'll have to implement it yourself similar to GMP.

On another note,

else if (dig=1)

is not what you want. It should be

else if ( dig == 1 )

Or you can simply use else {...} here unless you intend to check against negative numbers.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Tail Recursive Factorial Function in Scala

분류에서Dev

C# Recursive function approach?

분류에서Dev

Writing a Factorial function with Lists

분류에서Dev

recursive function building in R

분류에서Dev

Recursive PHP Function

분류에서Dev

argument concatenation in a recursive function

분류에서Dev

Memoise on recursive function

분류에서Dev

Recursive function with IO

분류에서Dev

Returning a value from a recursive function

분류에서Dev

Robotics - Recursive function for fractal.

분류에서Dev

Measuring code complexity of a recursive function

분류에서Dev

Recursive call to a function inside a class

분류에서Dev

Writing a recursive function for renaming terms in lambda calculus

분류에서Dev

Using malloc and free in recursive file scanning function

분류에서Dev

Python recursive function returns None instead of value

분류에서Dev

Recursive function not looping through all the children

분류에서Dev

Jump out of deeply recursive function call

분류에서Dev

PHP recursive function not setting nested arrays

분류에서Dev

Trouble with type matching and IO in a recursive function

분류에서Dev

2 dimensional maze solver recursive function

분류에서Dev

UL LI to HTML menu structure with recursive function

분류에서Dev

Running into "Maximum call stack" errors on a recursive function

분류에서Dev

Recursive function with local variable not executing if statement

분류에서Dev

Javascript recursive function - call to asynchronous function at the leave level

분류에서Dev

Calculate the length of a side of triangle in C

분류에서Dev

Segmentation fault in recursive Binary Search Algorithm in C

분류에서Dev

Recursive multi-process program in c

분류에서Dev

How to throw an exception from a recursive function in scala that returns Int

분류에서Dev

Quick Sort Python Recursion - how does the recursive function work

Related 관련 기사

  1. 1

    Tail Recursive Factorial Function in Scala

  2. 2

    C# Recursive function approach?

  3. 3

    Writing a Factorial function with Lists

  4. 4

    recursive function building in R

  5. 5

    Recursive PHP Function

  6. 6

    argument concatenation in a recursive function

  7. 7

    Memoise on recursive function

  8. 8

    Recursive function with IO

  9. 9

    Returning a value from a recursive function

  10. 10

    Robotics - Recursive function for fractal.

  11. 11

    Measuring code complexity of a recursive function

  12. 12

    Recursive call to a function inside a class

  13. 13

    Writing a recursive function for renaming terms in lambda calculus

  14. 14

    Using malloc and free in recursive file scanning function

  15. 15

    Python recursive function returns None instead of value

  16. 16

    Recursive function not looping through all the children

  17. 17

    Jump out of deeply recursive function call

  18. 18

    PHP recursive function not setting nested arrays

  19. 19

    Trouble with type matching and IO in a recursive function

  20. 20

    2 dimensional maze solver recursive function

  21. 21

    UL LI to HTML menu structure with recursive function

  22. 22

    Running into "Maximum call stack" errors on a recursive function

  23. 23

    Recursive function with local variable not executing if statement

  24. 24

    Javascript recursive function - call to asynchronous function at the leave level

  25. 25

    Calculate the length of a side of triangle in C

  26. 26

    Segmentation fault in recursive Binary Search Algorithm in C

  27. 27

    Recursive multi-process program in c

  28. 28

    How to throw an exception from a recursive function in scala that returns Int

  29. 29

    Quick Sort Python Recursion - how does the recursive function work

뜨겁다태그

보관