Write a C program to find factorial of a given number without using recursion.
int main() {
int n, res = 1;
/* get the input from user */
printf("Enter your input:");
scanf("%d", &n);
/* calculate factorial for the given input */
while (n > 0) {
res = res * n;
n--;
}
/* print the result */
printf("Result: %d\n", res);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input:5
Result: 120
Enter your input:5
Result: 120
No comments:
Post a Comment