C++ Program to find factorial of a number using call by Reference


In this program, You will learn how to find factorial of a number using call by Reference in C++.


Call by Reference

void fact(int n,int *f){}


Example: How to find factorial of a number using call by Reference in C++

Run it Now
#include<iostream>
using namespace std;

void fact(int n, int *f) {
    int i;
    for (i = 1; i <= n; i++) {
        *f = *f*i;
    }
}

int main() {

    int n, f = 1;
    cout << "Enter a Number :";
    cin>>n;

    fact(n, &f);
    cout << "\nFactorial is :" << f;

    return 0;
}

Output:

Enter a Number :4
Factorial is :24