WAP to find the factorial of a given number.
Assume we want to calculate the factorial of 5. We can write it as 5! and it can be calculated as follows:
5! = 5 x 4 x 3 x 2 x 1
= 120
Therefore, the factorial of 5 is 120.
5! = 5 x 4 x 3 x 2 x 1
= 120
Therefore, the factorial of 5 is 120.
#include <stdio.h>
#inlude<conio.h>
void main()
{
int n, i;
long fact = 1;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
// shows 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)
{
fact=fact * i;
}
printf("Factorial of %d = %ld", n, fact);
}
getch();
}
Output
Enter an integer: 5
Factorial of 5 = 120


0 Comments