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 : 6 Comments


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

  1. toure

    #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

    it’s very nice ya,was helpful

  3. selvam

    very very useful C++ Program in my project

  4. sayooj

    good

  5. ghost

    #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

    i not satisfied

Leave a Reply

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

Free email signup