Factorial program is one of the simple programs that every beginner program needs to understand. Here you find the simplest way for the factorial program in c.
Simple Factorial Program In C Using Function
Source Code:
// simple factorial program in c
#include <stdio.h>
// declation of fact function
int fact(int num);
// body of factorial function
int fact(int num){
//declration of variables
int i;
//for loop to perform factorial
for(i = num - 1; i < num && i != 0; i--){
num = num * i;
}
//returning the factorial
return num;
}
// main function
void main(){
// delaration of variables
int num = 0, facto = 0;
// number input statement for fatorial
printf("\nEnter number for factorial:");
scanf("%d",&num);
// calling fact and and assigne return fact
// to the facto variable
facto = fact(num);
//statement to print the number with their factorial
printf("\nFactorial of %d is %d.",num,facto);
}
Input:
Enter number for factorial:4
Output:
Factorial of 4 is 24.