Submission
Status:
[PPPPPPPPPP]
Score: 100
User: Jokul
Problemset: ตรวจบัตรเครดิต
Language: c
Time: 0.001 second
Submitted On: 2025-03-04 15:18:14
#include <stdio.h>
int main() {
long long int c, result = 0;
int n[16], i;
scanf("%lld", &c);
// Extract each digit of the credit card number into the array
for (i = 15; i >= 0; i--) {
n[i] = c % 10; // Get the last digit
c = c / 10; // Remove the last digit
}
// Apply the Luhn algorithm
for (i = 14; i >= 0; i--) { // Start from the second last digit
if (i % 2 == 0) { // Even index (1st, 3rd, 5th, etc. from the right)
n[i] *= 2; // Double the digit
if (n[i] > 9) {
n[i] -= 9; // Subtract 9 if the result is greater than 9
}
}
}
// Calculate the sum of all digits
for (i = 0; i < 15; i++) {
result += n[i];
}
// Calculate the expected last digit
int check_digit = (10 - (result % 10)) % 10;
// Compare the calculated check digit with the last digit of the credit card number
if (check_digit == n[15]) {
printf("yes\n"); // Valid credit card number
} else {
printf("no\n"); // Invalid credit card number
}
return 0;
}