Saturday, November 23, 2019

Write a C program to calculate the factorial of a given number


#include<stdio.h>

int main()

{

  int k, n;              

  int fact = 1;         


printf("Enter an input number to find its factorial \n");

scanf("%d", &n);



   if(n==0)

  {

      printf("Entered number must be above zero \n");

  }

  else

  {


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

     fact = fact * k;

  }
printf("Factorial of the given number is %d = %d\n", n, fact);



return 0;
}

OUTPUT:
Enter an input number to find its factorial
6
Factorial of the given number is 6 = 720

Enter an input number to find its factorial
0
Entered number must be above zero

No comments:

Post a Comment