Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 26-08-2009
Member
 
Join Date: Jun 2009
Posts: 3,859
How to calculate factorial of a number in C++

I am having problem coding in C++. I want to calculate the factorial of a number that the user inputs but the problem is that whatever logic I try to do, it fails. So I am here to know what are the possibilities and how do you calculate factorial of a user specified number in C++?
Reply With Quote
  #2  
Old 26-08-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: How to calculate factorial of a number in C++

You can calculate the factorial of a number in C++ using an external function as below and calling that function in your main code:

Code:
int calfactorial (int x)
{
 int finalx = 1;
 if (x==1) return 1;
 for (int i = 1; i <= x; ++i) finalx = finalx * i;
 return finalx;
}
Reply With Quote
  #3  
Old 26-08-2009
Member
 
Join Date: May 2008
Posts: 685
Re: How to calculate factorial of a number in C++

Here is how factorial is calculated in C++:

Code:
# include <iostream.h>
int factorial (int);
int main()
{
    int result; 
    cout << "Enter your number: ";
    cin >> result;
    cout << "Factorial of " << result << "is "<< factorial (result) << endl;
}

int factorial (int n)
{
   int fact = 1;
   if  (n <= 1)
	 return 1;
   else
	 fact  = n * factorial (n - 1);
   return fact;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to calculate factorial of a number in C++"
Thread Thread Starter Forum Replies Last Post
How to create factorial of numbers using sub procedure in vbscript Gurseerat Software Development 1 04-02-2012 12:21 PM
n factorial program in C MaryJ Software Development 5 24-09-2010 09:59 PM
c# program for factorial roodiii Software Development 5 19-12-2009 02:42 PM
How to write java program to find factorial of number? Balamohan Software Development 5 28-11-2009 10:14 PM
What is the Program to find-out factorial of given number? Sheenas Software Development 5 27-11-2009 01:42 PM


All times are GMT +5.5. The time now is 12:06 AM.