C++ Program to find Factorial of a Number using Recursion and Loop

By | 25.12.2016

Factorial of a Number using Recursion and loop


Write a C++ Program to find Factorial of a Number using Recursion and loop. Here’s simple Program to find Factorial of a Number using Recursion and loop in C++ Programming Language.


Factorial of any number is the product of an integer and all the integers below it for example factorial of 4 is
4! = 4 * 3 * 2 * 1 = 24


Factorial of a Number using For Loop

Here is source code of the C++ Program to find Factorial of a Number using for loop. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/*  C++ Program to find Factorial of a Number using for loop  */

#include<iostream>
using namespace std;

int main()
{
    int i, n, fact=1;

    cout<<"Enter any positive number :: ";
    cin>>n;

    for(i=1;i<=n;i++)
    {
        fact=fact*i;
    }
    cout<<"\nFactorial of Number [ "<<n<<"! ] is :: "<<fact<<"\n";

    return 0;
}

Output : :


/*  C++ Program to find Factorial of a Number using for loop */

Enter any positive number :: 6

Factorial of Number [ 6! ] is :: 720

Process returned 0

Factorial of a Number using recursion


Here is source code of the C++ Program to find Factorial of a Number using Recursion. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/*  C++ Program to find Factorial of a Number using Recursion  */

#include<iostream>
using namespace std;

int fact(int a);

int main()
{
    int fact(int);
    int f, n;

    cout<<"Enter any positive number :: ";
    cin>>n;

    f=fact(n);
    cout<<"\nFactorial of Number [ "<<n<<"! ] is :: "<<f<<"\n";

   return 0;
}

int fact(int a)
{
    if(a==1)
    {
       return(1);
    }
    else
    {
        return(a*fact(a-1));
    }
}

Output : :


/*  C++ Program to find Factorial of a Number using Recursion  */

Enter any positive number :: 7

Factorial of Number [ 7! ] is :: 5040

Process returned 0

Above is the source code for C++ Program to find Factorial of a Number using For Loop and Recursion which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

5 4 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments