.Write a program to find the factorial of a number.
Answers
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
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 *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}
Output
Enter an integer: 10
Factorial of 10 = 3628800
Answer:
C program to find factorial of a number
long factorial(int);
int main() { int n;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
printf("%d! = %ld\n", n, factorial(n));
return 0; }
long factorial(int n) { int c; long r = 1;
for (c = 1; c <= n; c++) r = r * c;
return r; }
Explanation: