C Program to Calculate Factorial
This C program prompts the user to enter a number and calculates its factorial using a while loop. The factorial is computed by multiplying the number by all positive integers less than it until it reaches 1.#include <stdio.h> int main() { int n, factorial; factorial = 1; printf("Enter n: "); scanf("%d", &n); while(n > 1) { factorial = factorial * n; n = n - 1; } printf("Factorial = %d\n", factorial); return 0; }
Leave a Comment