Tuesday 5 March 2013

calculate factorial and double factorial of a given number-


#include <iostream.h>
using namespace std;
//factorial function
int factorial(int n)
{
int res = 1,i;
for (i=1;i<=n;i++)
{
 res *= i;
}
return res;
}
//double factorial function
int dfact(int n)
{
int i;double res=1.0;
for(i=n;i>=1;i-=2)
{
res *=i;
}
return res;
}
int main( )
{
int n;
  cout << "Enter the number=";
  cin >> n;
  cout << n << "! = " <<factorial(n) << endl; //factorial output
  cout << n << "!! = " <<dfact(n) << endl; //double factorial output
  system("pause");
}

No comments:

Post a Comment