bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc071.c

luc071.c (1439B)


      1 /* Write a program that receives a 16-digit Credit Card number and checks whether it is valid using the Luhn algorithm variant described.
      2 */
      3 
      4 /* Let Us C, Chap- 15 (Strings), Qn No.: C(c) */
      5 
      6 /* This file is auto-generated by a bot. */
      7 /* This code is not compiled; it is for reference only. */
      8 
      9 
     10 #include <stdio.h>
     11 #include <string.h>
     12 #include <stdlib.h>
     13 
     14 int main()
     15 {
     16     char card[20];
     17     int i, digit, sum = 0;
     18 
     19     printf("Enter 16-digit Credit Card number: ");
     20     scanf("%s", card);
     21 
     22     /* Rule:
     23        1. Start with rightmost-1 digit (index 14) and multiply every other digit by 2.
     24           (These are indices 0, 2, 4, ..., 14)
     25        2. Subtract 9 if result >= 10.
     26        3. Add these results.
     27        4. Add remaining digits (indices 1, 3, ..., 15).
     28        5. If total sum is divisible by 10, it is valid.
     29     */
     30 
     31     for (i = 0; i < 16; i++)
     32     {
     33         digit = card[i] - '0';
     34 
     35         if (i % 2 == 0) // Indices 0, 2, 4... (Every other starting from left, which hits rightmost-1)
     36         {
     37             digit = digit * 2;
     38             if (digit >= 10)
     39             {
     40                 digit = digit - 9;
     41             }
     42         }
     43         
     44         // Add to total sum (both modified and unmodified digits)
     45         sum += digit;
     46     }
     47 
     48     printf("Total Sum: %d\n", sum);
     49 
     50     if (sum % 10 == 0)
     51         printf("The Credit Card number is Valid.\n");
     52     else
     53         printf("The Credit Card number is Invalid.\n");
     54 
     55     return 0;
     56 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror