From 8fbd60dc788cab5564c16059f09b115998fc8634 Mon Sep 17 00:00:00 2001 From: Swathy M Date: Sat, 2 May 2026 21:41:20 +0530 Subject: [PATCH] Enhance comments and variable initialization in factorial.c Added comments for clarity and improved code readability. --- math/factorial.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/math/factorial.c b/math/factorial.c index 3435b2f963..b770276f58 100644 --- a/math/factorial.c +++ b/math/factorial.c @@ -1,30 +1,36 @@ -#include +/* + Program to compute factorial of large numbers using array storage. + Handles values beyond standard integer limits. +*/ +#include //for basic functions like printf,scanf etc int main() { - int a[200], n, counter, temp, i; - a[0] = 1; - counter = 0; + int a[200], n, counter, temp, i; //initialize variables and arrays + a[0] = 1; //first element of the array to be 1 + counter = 0; //to track number of digits in result printf("Enter a whole number to Find its Factorial: "); - scanf("%d", &n); - if (n < 0) + scanf("%d", &n); //take input number and store to variable n + if (n < 0) //factorial not defined for negatives printf("Cannot Calculate factorials for negative numbers."); else { for (; n >= 2; n--) { - temp = 0; + temp = 0; //for carry during multiplication for (i = 0; i <= counter; i++) { - temp = (a[i] * n) + temp; - a[i] = temp % 10; - temp = temp / 10; + temp = (a[i] * n) + temp; //multiply digit and add previous carry + a[i] = temp % 10; //store last digit of result + temp = temp / 10; //update carry } + //store remaining carry digits in array while (temp > 0) { a[++counter] = temp % 10; temp = temp / 10; } } + //print factorial in msd order for (i = counter; i >= 0; i--) printf("%d", a[i]); } return 0;