on-internet
← Community

C program for calculating factorial

Stack Overflow ·

i have written a small function which calculates factorial for a number in C as follows: int factNnumbers(int n) { if(n == 1) return 1; else return (n*factNnumbers(--n)); } I call the function shown above as: factNnumbers(takeInputN()); where the function to take the input (takeInputN) is defined as: int takeInputN() { int n; printf("\n\nHow many numbers ?? \n "); scanf("%d", &n); return n; } If I change one line in my factorial code as shown below , my program works perfectly. Otherwise with the above code it prints the factorial of the number input -1 (example if number input is 5, it will p

Comments

Sign in or sign up to comment and upvote.

No comments yet.