PROGRAM TO CALCULATE FACTORIAL USING RECURSIVE METHOD IN C++.

#include<iostream.h>        //Including i/o header files

int fact(int n);            //Prototype of a function to calculate factorial

void main()
{
int n;
char ch;
do
{
//Taking the number whose factorial is to be calculated
cout<<"Enter the number whose factorial is to be calculated:\n";
cin>>n;
//Passing the number to the function for calculation of factorial
int p=fact(n);
//Printing the calculated value
cout<<"The calculated value is\t"<<p<<endl;

cout<<"Do you wanna continue?(Y/N)\t";
cin>>ch;
cout<<endl;

}while(ch=='Y'||ch=='y');
}

int fact(int n)                    //Definition of function to calculate factorial
{
if(n==0) return(1);                //BAD CASE
if(n>0)  return (n*fact(n-1));
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s