Calculations

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
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