C program to find factorial of number using function
Problem description
In this program take number as input & then it show the factorial of given number as output.
Problem solution
1.take a number as input.
2.declare the void Fact(int) function globally if you want or declare the function prototype into the main() function.
3.after taking input call the function into main() .
4.then define the function definition.
5.after taking function definition for loop started in this function definition.
6. In for loop i=1 & fact=1 then fact=1. after this loop is continue and incremented by1 until the n is grater than equal to i. continuity of loop is depend on what number take as input .
Program code:
void Fact(int);//function prototype declearation
int i,fact=1;//global variable declearation
int main()
{
int n;
printf("\n enter the number:");
scanf("%d",&n);
Fact(n);//function call
}
//function definition
void Fact(int n)
{
for(i=1; i<=n; i++)
{
fact=fact*i;
}
printf("\n factorial of %d is=%d",n,fact);
}
Program explanation:
1. for example we take 5 as input.
2.we declare the void Fact(int) function globally.
3.after taking function call we give the function definition outside the main function.
4. in function definition for loop started.In for loop i=1 & fact=1 then fact=1. after this loop is continue and incremented by1 until the n is grater than equal to i.
OUTPUT
Comments
Post a Comment
please subscribe my blog and let me suggest how I improve this site