Skip to content

luc071.c

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.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)

Actions

Raw View on GitHub

💡 You can print or save this file by opening Raw and using your browser.

Source Code

#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;
}