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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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