Write a program to print factorial of a given number using function.
Answers
Answered by
2
Explanation:
include <stdio.h>
int fact(int);
void main() {
int no,factorial;
printf("Enter a number to calculate it's factorial\n"); scanf("%d",&no);
factorial=fact(no); ...
//printf("Factorial of the num(%d) = %d\n",no,fact(no));//another way of calling a function//comment above two lines if you want to use this. ...
int fact(int n)
Answered by
3
#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;
}
Similar questions
Science,
3 months ago
Social Sciences,
3 months ago
Hindi,
7 months ago
English,
7 months ago
Math,
11 months ago
Social Sciences,
11 months ago