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.
 avatar
AdhamElshabasy
c_cpp
25 days ago
252 B
3
Indexable
C Basics
#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