luc071.c (1630B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a program that receives a 16-digit Credit Card number and checks whether it is valid using the Luhn algorithm variant described. 9 */ 10 11 /* Let Us C, Chap- 15 (Strings), Qn No.: C(c) */ 12 13 /* This file is auto-generated by a bot. */ 14 /* This code is not compiled; it is for reference only. */ 15 16 17 #include <stdio.h> 18 #include <string.h> 19 #include <stdlib.h> 20 21 int main() 22 { 23 char card[20]; 24 int i, digit, sum = 0; 25 26 printf("Enter 16-digit Credit Card number: "); 27 scanf("%s", card); 28 29 /* Rule: 30 1. Start with rightmost-1 digit (index 14) and multiply every other digit by 2. 31 (These are indices 0, 2, 4, ..., 14) 32 2. Subtract 9 if result >= 10. 33 3. Add these results. 34 4. Add remaining digits (indices 1, 3, ..., 15). 35 5. If total sum is divisible by 10, it is valid. 36 */ 37 38 for (i = 0; i < 16; i++) 39 { 40 digit = card[i] - '0'; 41 42 if (i % 2 == 0) // Indices 0, 2, 4... (Every other starting from left, which hits rightmost-1) 43 { 44 digit = digit * 2; 45 if (digit >= 10) 46 { 47 digit = digit - 9; 48 } 49 } 50 51 // Add to total sum (both modified and unmodified digits) 52 sum += digit; 53 } 54 55 printf("Total Sum: %d\n", sum); 56 57 if (sum % 10 == 0) 58 printf("The Credit Card number is Valid.\n"); 59 else 60 printf("The Credit Card number is Invalid.\n"); 61 62 return 0; 63 }