C Program to find factorial of a number using call by reference


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


Call by Reference

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

Example: How to find factorial of a number using call by reference in c.

#include<stdio.h>

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

int main() {

    int n, f = 1;
    printf("Enter a number:");
    scanf("%d", &n);

    fact(n, &f);
    printf("Factorial is:%d", f);

    return 0;
}

Output:

Enter a number:4                                                                                                                       
Factorial is:24