C Program to Calculate the Factorial of a number using Function.
In this example, we will calculate the factorial of a number using function. Here we have used functions with Arguments and return value to perform this operation. We have declared a user-defined function fact which is called in the main function when printing the result.
What is a Factorial of a Number?
The factorial of a number is the product of all positive integers less than, equal to n. Factorial is denoted by the symbol '!'. Formula of factorial is n!=n x (n-1)!
For example:
6!= 6 x 5 x 4 x 3 x 2 x1
= 720
PROGRAM TO CALCULATE THE FACTORIAL OF A NUMBER USING FUNCTION
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n;
printf("Enter the number : ");
scanf("%d",&n);
printf("Factorial of %d is : %d",n,fact(n));
getch();
}
int fact(int b)
{
int i,f=1;
for(i=1;i<=b;i++)
{
f=f*i;
}
return(f);
}
OUTPUT OF FACTORIAL OF A NUMBER USING FUNCTION.
Enter the number : 6
Factorial of 6 is : 720
NOTE: You Can Comment Your Code if you have solved differently and we will pin it in the comment section. Let's Learn together.
MOTTO:
You Learn, I Learn together We Learn.
Comments
Post a Comment
Please do not spam any link