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)
Difficulty Beginner (index: 1 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Array
  • Pointers

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

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (luc071.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;
    }