luc071.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Write a program that receives a 16-digit Credit Card number and checks whether it is valid using the Luhn algorithm variant described.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char card[20];
int i, digit, sum = 0;
printf("Enter 16-digit Credit Card number: ");
scanf("%s", card);
/* Rule:
1. Start with rightmost-1 digit (index 14) and multiply every other digit by 2.
(These are indices 0, 2, 4, ..., 14)
2. Subtract 9 if result >= 10.
3. Add these results.
4. Add remaining digits (indices 1, 3, ..., 15).
5. If total sum is divisible by 10, it is valid.
*/
for (i = 0; i < 16; i++)
{
digit = card[i] - '0';
if (i % 2 == 0) // Indices 0, 2, 4... (Every other starting from left, which hits rightmost-1)
{
digit = digit * 2;
if (digit >= 10)
{
digit = digit - 9;
}
}
// Add to total sum (both modified and unmodified digits)
sum += digit;
}
printf("Total Sum: %d\n", sum);
if (sum % 10 == 0)
printf("The Credit Card number is Valid.\n");
else
printf("The Credit Card number is Invalid.\n");
return 0;
}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
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