Program to find factorial of a number in c using function

Program to find factorial of a number in c using function

C program to find factorial of a number using function

#include<stdio.h>
#include<stdio.h>
void fact(int);
main()
{
int n;
printf("\n Enter the number to be find factorial");
scanf("%d",&n);
fact(n);
}
void fact(int n)
{
int i, f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n Factorial of %d = %d",n,f);
}
 Output

How factorial is calculated 

Factorial of number is calculated by 

n*(n-1)*(n-2)*(n-3)....…..........................1

For example factorial 5 is

5*(5-1)*(5-2)*(5-3)*(5-4)

5*4*3*2*1

120

Here in program 

User have to enter number to be find factorial the value is store in the variable n.

void fact(int); 
It is a Function prototype 

fact(n);
It is a Function call
 And, 

for(i=1;i<=n;i++)

The loop is continuous from 1 to n

f=f*i

It multiply number as 

1*(n-1)*(n-2)*(n-3)............n 

printf("\n factorial of %d = %d",n,f);

The part helps to print value of f which is factorial of n


Also, check

C program to find factorial of a number - Using recursive function

C program to find factorial of a number - Using for loop

C program to find factorial of a number - Using do while loop

C program to find factorial of a number - Using while loop




Post a Comment

0 Comments