It has been a long time since I wroteC language, and my memory has become quite vague. After recalling and flipping through books, I managed to write a runnable program. The last program was from September 15. [C Language Program 10] Calculation of Factorial of Positive Integers
After further corrections and improvements byKimi, the following program code was produced. I have looked at the code several times, and I can still understand what functions, formal parameters, and actual parameters are.
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h> // Standard Input Output Library#include <stdlib.h> // General Utility Library (includes abs, malloc, etc.)#include <math.h> // Declaration of Mathematical Functions
// Calculate the factorial of n, returning unsigned long long typeunsigned long long fac(int n){ int i; unsigned long long f = 1; n = abs(n); // Allow negative input, treat as positive for (i = 1; i <= n; ++i) f *= i; return f;}
int main(void){ int x, y; unsigned long long c1, c2;
printf("Please enter numbers x, y (negative numbers will be treated as positive): "); scanf("%d%d", &x, &y);
c1 = fac(x); // Call factorial function c2 = fac(y); // Call factorial function
printf("The difference of the factorials of the two numbers is: %llu\n", c1 - c2); return 0;}
Running Result:
请输入数字 x, y(负数将按正数处理): 53两个数阶乘之差是: 114
请输入数字 x, y(负数将按正数处理): 2019两个数阶乘之差是: 2311256907767808000
When calculating 100! − 99! , both have exceededunsigned long long representation range (≥2⁶⁴), resulting in modulo 2⁶⁴ wraparound; coincidentally obtaining the same remainder, thus the difference is0. This is not a true mathematical equality, but a pseudo-zero result caused by finite word length overflow.
请输入数字 x, y(负数将按正数处理): 10099两个数阶乘之差是: 0 (错误答案)