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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago