Thread: HOw to find factorial of a given number in C++ Language?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    12

    HOw to find factorial of a given number in C++ Language?

    Hi,

    I am working on a program that asks a user to enter a # between 1 to 50 and finds the factorial of that. It uses recursive function to find it.

    Here is what I got so far:

    Code:
        include <iostream>
    
        using namespace std;
    
        //function prototypes
    
        int getNum();
        int fact(int);
        void printResult(int, int);
    
    
        int main()
        {
        int num;
        int ans;
    
        num = getNum();
        ans = fact(num);
        printResult(num, ans);
    
        cout << endl << endl;
        }
        int getNum()
        {
        	int num;
        	cout << "\nEnter a number:";
        	cin >> num;
        	return num;
        }
        int fact(int num)
        {
        	
        	if (num == 0)
        		return 1;
        	else 
        		return num * fact(num - 1);
        	
        }
        void printResult(int num, int ans)
        {
        	cout <<"\nfactorial of" << num << "is : " << ans;
        	
        }
    Its not working, can some please help me solve this problem.

    Thanks

    edit: sorry, I have posted this again, because previously I accidentally posted on C section.
    Last edited by redworker; 05-11-2011 at 04:54 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your code looks correct (except for the part where you are calling "printResults", but your function declaration is "printResult"). What isn't working for you?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    12
    it gives me an error
    Last edited by redworker; 05-11-2011 at 04:53 PM.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    12
    Ah cant believe I did that mistake. Ok now I fixed the code but when I enter 20, it says factorial is -2102132736
    but when I enter lower number like 5, it gives me correct answer 120. Whats wrong with 20?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by redworker View Post
    Ah cant believe I did that mistake. Ok now I fixed the code but when I enter 20, it says factorial is -2102132736
    but when I enter lower number like 5, it gives me correct answer 120. Whats wrong with 20?
    In 32-bit systems signed int can hold values from -2147483648 to 2147483647. Factorial of 20 is 2432902008176640000.
    Devoted my life to programming...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The answer hasn't changed from before. 20! is not representable in the int data type, since it is a 19-digit number. You need to pick a data type that can handle numbers of that size.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Factorial in C language.
    By redworker in forum C Programming
    Replies: 3
    Last Post: 05-11-2011, 11:30 PM
  2. problem with large number factorial calculation
    By afr_c2011 in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2011, 10:49 AM
  3. request for C code for factorial of any wish number
    By neverthought in forum C Programming
    Replies: 17
    Last Post: 05-28-2010, 06:27 AM
  4. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  5. I find myself caring less and less about the language...
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 05-24-2008, 02:26 AM