Site icon Codeforcoding

Cpp program to find factorial using function

CPP program to find factorial using Function

In this tutorial, we will discuss Cpp program to find factorial using Function

There are many ways to calculate a factorial using C++ programming language.

In this tutorial, we are going to learn about how to calculate factorial of a given number using the C++ function

 

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

 

Factorial of number of n

 

Example

factorial of 5 is

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

 

factorial of 4 is

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

 

The factorial of a positive number of n is given below

factorial of n is

n!=n*(n-1)*....2*1

The user can provide numbers as they wish and get the factorial according to their input

C++ code to find factorial using function

Program 1

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    cout << "Enter the number to find factorial" << endl;
    void factorial();
    factorial();//call the metod 
    getch();
    return 0;
}

void factorial(){//define a user define method to find factorial
int fact=1,n,i;
cin>>n;//get input from user
for(i=1; i<=n; i++){
   fact=fact*i;//find factorial using for loop
}
   cout<<"Factorial of "<<n<<" is: "<<fact;
     //display factorial
}

 

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

Enter the number to find factorial
7
Factorial of 5 is : 5040

 

C++ code to find factorial using function with return

Program 2

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

int fact(int); //function prototype
int main()
{
   int num,factorial;//declare essential variable as int
    cout<<"Enter the number to find factorial\n";
    cin>>num;

    factorial=fact(num);//call the fact function

    cout<<"Fadtorial of "<<num<<"is: "<<factorial;

    getch();
    return 0;
}
int fact(int num)
{
    int i,f=1;
    for(i=1; i<=num; i++)
        f=f*i;//find factorial using for loop
    return(f);
}

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

Enter the number to find factorial
5
Factorial of 5 is : 120

 

C++ code to find factorial using Tenary operator

Program 3

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

int fact(int); //function prototype
int main()
{
   int num;
    cout<<"Enter the number to find factorial\n";
    cin>>num;//get input from user


    cout<<"Fadtorial of"<<num<<"is"<<fact(num);

    getch();
    return 0;
}
int fact(int num)
{
    return (num==1 || num==0) ? 1:num*fact(num-1);
}

 

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

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

Suggested for you

Java code to find factorial of a number

Java code to find factorial of a number using method

Find factorial of a number in C

C Program to find factorial of a number using Function

Find factorial of a number in CPP

Find factorial of a number in Python

For loop in C++ language

 

Java code to find factorial using method
C program to find factorial using function
Exit mobile version