C++ program to calculate the factorial of a number using recursion.

Sunday, April 4th, 2010

Write a program to calculate the factorial of a number using recursion.

#include<iostream.h>
#include<conio.h>
void main()
{
	int n,fact;
	int rec(int); clrscr();
	cout<<"Enter the number:->";
	cin>>n;
	fact=rec(n);
	cout<<endl<<"Factorial Result are:: "<<fact<<endl;
	getch();
}
rec(int x)
{
	int f;
	if(x==1)
		return(x);
	else
	{
		f=x*rec(x-1);
		return(f);
	}
}
Avatar Image

Author Name :
Ranjith

Total : 8 Comments


8 Responses to “C++ program to calculate the factorial of a number using recursion.”

  1. toure says:

    #include
    #include

    using namespace std;

    int main(int argc, char *argv[])
    {
    int fact=1,num=1;
    cout <>num;
    if (num<=0);
    num=1;
    for(int count=1;count<=num;count++);
    { fact=*count
    ;
    cout <<fact;

    system("PAUSE");
    return EXIT_SUCCESS;
    }

  2. Deepak c varghese says:

    it’s very nice ya,was helpful

  3. selvam says:

    very very useful C++ Program in my project

  4. sayooj says:

    good

  5. ghost says:

    #include
    using namespace std;
    int main()
    {
    int n,fact;
    int rec(int);
    cout<”;
    cin>>n;
    fact=rec(n);
    cout<<endl<<"Factorial Result are:: "<<fact<<endl;
    return 0;
    }
    int rec(int x)
    {
    int f;
    if(x==1)
    return(x);
    else
    {
    f=x*rec(x-1);
    return(f);
    }
    }

  6. anandh says:

    i not satisfied

  7. pankaj says:

    #include
    #include
    void fact(int)
    void main()
    {
    int num;
    cout<>num;

    fact(num);
    getch();
    }
    void fact(int n)
    {
    long int sum=1;
    {
    for(int temp=1; temp<=n; temp++)
    sum=sum*temp;
    }
    cout<<"factorial of given no. is:";
    }

  8. annonymous says:

    not satisfied..!@#$%^&*()

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup