Q:

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

belongs to collection: C++ Number Solved Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Number Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to Print Multiplication Table of a giv... >>
<< Write a C++ Program to Check a number is Prime or ...