Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C++ program to find factorial of a number|in 7 ways

Posted on October 16, 2019September 28, 2020

Table of Contents

  • C++ program to find factorial of a number|in 7 ways
    • Find factorial of a number – using stranded method
    • Find factorial of a number – using for loop
    • Find factorial of a number – using while loop
    • Find factorial of a number – using do-while loop
    • Find factorial of a number – using function
    • Find factorial of a number – using recursion
    • Find factorial of a number – using the pointer

C++ program to find factorial of a number|in 7 ways

In this tutorial, we will discuss the C++ program to find factorial of a number|in 7 ways

C++ program to find factorial of a number
Factorial calculation

In this post, we are going to learn how to find the factorial of a number of the given number

What is the factorial of given numberĀ  (n)?

The factorial of a number (n) is the product of all positive integers from 1 upto n (n is the given number).

It is simply denoted by n!.

Example

if you want to find the factorial for number 5, you can follow this method.
Factorial of 5 will be 5*4*3*2*1=120
So, 5!=120.

Program 1

Find factorial of a number – using stranded method

#include <iostream>
#include <conio.h>
using namespace std;
    int main()
{
    int i,num=6,fact=1;
    for(i=1; i<=num; i++)
    {
        fact=fact*i;
    }
   cout<<"Factorial of "<<num<< " is: "<<fact;
    getch();
    return 0;
}

When the above code is executed it produces the following output

Factorial of 6 is: 720

In this program

  1. Integer variables i,num and fact are declared and initialized
  2. the program finds the factorial of a number using for loop
  3. Then, the program is displayed the factorial of a number using cout<<

Program 2

Find factorial of a number – using for loop

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

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

    return 0;
}

When the above code is executed it produces the following output

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

The program allows the user to enter a value and it finds and displays factorial of the given number using for loop in C++ language

Program 3

Find factorial of a number – using while loop

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

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

When the above code is executed it produces the following output

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

The program allows the user to enter a value and it finds and displays factorial of the given number using the while loop in C++ language

Program 4

Find factorial of a number – using do-while loop

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

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

When the above code is executed it produces the following output

Enter the number for find factorial: 4
4
Factorial of 4 is : 24

The program allows the user to enter a value and it finds and displays factorial of the given number using the do-while loop in C++ language

Program 5

Find factorial of a number – using function

#include <iostream>
#include <conio.h>
using namespace std;
int fact();
int main()
{

    cout<<"Enter the number for find factorial\n";
fact();

}
  int fact(){
       int i,num,fact=1;
        cin>>num;
        i=1;
while(i<=num){
    fact=fact*i;
    i++;
    }
    cout<<"Factorial of"<< num<<" is :"<<fact;
    getch();
    return 0;
}

When the above code is executed it produces the following output

Enter the number to find factorial
6
Factorial of 6 is:720

The program allows the user to enter a value and it finds and displays factorial of the given number using the function in C++ lamguage

Program 6

Find factorial of a number – using recursion

#include <iostream>
#include <conio.h>
using namespace std;
long int findFact(int num);

int main()
{
    int num;
    cout<<"Enter a positive integer: ";
    cin>>num;
    cout<<"factorial of "<<num<<" is "<<findFact(num);
getch();
    return 0;
}
long int findFact(int num){
if (num>=1)
    return num*findFact(num-1);
else
    return 1;

}

When the above code is executed it produces the following output

Enter a positive integer: 6
factorial of 6 is: 720

The program allows the user to enter a value and it finds and displays factorial of the given number using the recursive function in C++ language

Program 7

Find factorial of a number – using the pointer

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

void Factorial(int, long int*);
int main()
{
    long int fact;
    int num;

    cout<<"Enter a integer to find factorial\n";
   cin>>num;
    Factorial(num,&fact);
    cout<<"factorial of "<<num<<"is: "<<fact;
    getch();
    return 0;
}
void Factorial(int n, long int *fact){
int i;

*fact=1;
for(i=1; i<=n; i++)
    *fact=*fact*i;

}

When the above code is executed it produces the following output

Enter a integerto find factorial
6
factorial of 6 is: 720

The program allows the user to enter a value and it finds and displays factorial of the given number using the pointer in C++ language

Suggested for you

Function in C++ language

For loop in C++ language

While loop in C++ language

Do-while loop C++ language

Pointer in C++ language

 

Similar post

C program to find factorial of a number

Java program to find factorial of a number

Python program to find factorial of a number

C++ program to find factorial of a number

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes