11.05.2012

User Define Function- Factorial

Q. Write a function fact to calculate the factorial value of any number.

Ans.

/*c program for make function fact to calculate factorial value of any number*/
#include<stdio.h>
int fact(int );
int main()
{

 int num,f;
 printf("Enter any number : ");
 scanf("%d", &num);
 f = fact(num);
 printf("Factorial value of %d is %d",num,f);
 return 0;
}

int fact(int n)
{
 int z=1;
 for(; n>=1; n--)
    z = z * n;
 return(z);
}

The output of above program would be:


Output of calculating factorial value using user define function C program
Figure: Screenshot for find factorial value using
user define function C program

2 comments:

  1. Replies
    1. z store the value of factorial, so according to program we want the display the result of factorial hence we used z

      Delete