-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorials.c
49 lines (38 loc) · 1.07 KB
/
factorials.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#define MAX_DIGITS 200 // Maximum number of digits in result
void multiply(int result[], int n) {
int carry = 0, i;
for (i = 0; i < MAX_DIGITS; i++) {
int digit = result[i] * n + carry;
result[i] = digit % 10;
carry = digit / 10;
}
}
int findRightmostNonZeroDigit(int n) {
int result[MAX_DIGITS] = {0};
result[0] = 1; // Initialize result to 1
// Calculate factorial of n and store digits in result array
int i;
for (i = 1; i <= n; i++) {
multiply(result, i);
}
// Find rightmost non-zero digit in result array
for (i = MAX_DIGITS - 1; i >= 0; i--) {
if (result[i] != 0) {
return result[i];
}
}
// If no non-zero digit found, return 0
return 0;
}
int main() {
int n;
// Input value of n
printf("Enter the value of n: ");
scanf("%d", &n);
// Find rightmost non-zero digit of n!
int digit = findRightmostNonZeroDigit(n);
// Output result
printf("The rightmost non-zero digit of %d! is %d\n", n, digit);
return 0;
}