Computer Science, asked by amisha2850, 1 year ago

Write the program in c to find factorial of a number

Answers

Answered by mehrin20
4

Factorial program in C using a for loop

#include <stdio.h>

int main()

{

int c, n, fact = 1;

printf("Enter a number to calculate its factorial\n");

scanf("%d", &n);

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

fact = fact * c;

Answered by sushiladevi4418
2

Answer:

Write the program in C to find the factorial of a number.

Explanation:

#include <stdio.h>

int main()

{

   int n, i;

   unsigned long long factorial = 1;

   Printf("Enter an integer: ");

   Scanf("%d",&n);

   // show error if the user enters a negative integer

   if (n < 0)

       printf("Error! Factorial of a negative number doesn't exist.");

   else

   {

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

       {

           factorial *= i;              // factorial = factorial*i;

       }

       printf("Factorial of %d = %llu", n, factorial);

   }

   return 0;

}

Output :-

Enter an integer: 5

Factorial of 5 = 120

Similar questions