Calculations

Cpp program to find factorial of a number

Cpp program to find factorial of a number

In this tutorial, we will discuss the concept of Cpp program to find factorial of a number

Factorial is a product of all positive descending integer begins with a specified number (n) and calculates upto one

There are many ways to calculate factorial using C++ language. some of them are described as below.

Example

factorial of 5 is

5!=5*4*3*2*1=120

 

factorial of 4 is

4!=4*3*2*1=24

 

factorial of n is

n!=n*(n-1)*....2*1
Factorial of nth number

Cpp program to find factorial

Find factorial of a number using for loop

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,factorial=1;
    cout<<"Enter number for find factorial: ";
    cin>>num;
    for(int i=1; i<=num; i++){
        factorial*=i;  //factorial=factorial*i
    }
    cout << "factorial of "<<num<<" is: "<<factorial<< endl;
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter number for find factorial: 6
Factorial of 6 is:720

This program intakes input (a positive integer) from the user and calculates the factorial of the given number using For loop and displays the result.

 

Find factorial  of a number using while loop

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,factorial=1;
    cout<<"Enter number for find factorial: ";
    cin>>num;
    int i=1;
   while(i<=num){
        factorial*=i;  //factorial=factorial*i
         i++;
    }
    cout << "factorial of "<<num<<" is: "<<factorial<< endl;
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter number for find factorial: 7
Factorial of 6 is:5040

This program intakes input (a positive integer) from the user and calculates the factorial of the given number using while loop and displays the result.

Find factorial of a number using the do-while loop

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num,factorial=1;
    cout<<"Enter number for find factorial: ";
    cin>>num;
    int i=1;
    do{
        factorial*=i;  //factorial=factorial*i
         i++;
    }
   while(i<=num);
    cout << "factorial of "<<num<<" is: "<<factorial<< endl;
    getch();
    return 0;
}

When the above code is compiled and executed, it produces the following results

Enter number for find factorial:8
factorial of 8 is: 40320

This program intakes input (a positive integer) from the user and calculates the factorial of the given number using Do-while loop and displays the result.

Find factorial of a number using C++ recursion

#include <iostream>
#include <conio.h>
using namespace std;
int factorial(int n);
int main()
{
    int num;
    cout<<"Enter number for find factorial: ";
    cin>>num;
    cout << "factorial of "<<num<<" is: "<<factorial(num)<< endl;
    getch();
    return 0;
}
int factorial(int n)
{
    if(n>1)
        return n*factorial(n-1);
        else
          return 1;
}

When the above code is compiled and executed, it produces the following results

Enter number for find factorial: 6
factorial of 6:720

This program intakes  input (positive integer) from the user and calculates the factorial of the given number using

recursion and displays the result.

 

Suggested for you

C++ Operator

C++ if statements

C++ for loop

C++ while loop

C++ do-while loop

 

Similar post

find factorial of a number using the function in C++

find factorial of a number using function in C

 

Java code to calculate factorial of a number
C program to find factorial of a number
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago