Factorial of a number.

Factorial of a Number

For any positive number n, its factorial is given by:
factorial = 1*2*3*4....n
If a number is negative, factorial does not exist and factorial of 0 is 1.
This program takes an integer from a user. If user enters negative integer, this program will display error message and if user enters non-negative integer, this program will display the factorial of that number.

Source Code to Find Factorial of a Number

#include <stdio.h>
 
int main()
{
  int c, n, fact = 1;
 
  printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);
 
  for (c = 1; c <= n; c++)
    fact = fact * c;
 
  printf("Factorial of %d = %d\n", n, fact);
 
  return 0;
}

No comments:

Post a Comment