Metadata

  • πŸ“… Date :: 12-06-2025
  • 🏷️ Tags :: cpp project

Notes

Detailed Notes on Credit Card Validation Program in C++ using Luhn Algorithm

Overview of the Problem

In this tutorial, we are going to write a C++ program to validate if a credit card number is valid using the Luhn Algorithm. This algorithm helps in checking whether a credit card number follows the correct pattern and is potentially legitimate. The steps of the Luhn algorithm are broken down into a series of operations on the digits of the credit card number.

Luhn Algorithm Steps

To validate the credit card number, we follow these steps:

  1. Double Every Second Digit (from Right to Left)
    • Start from the rightmost digit (least significant digit) of the credit card number.
    • Double every second digit as you move left.
    • If doubling a digit results in a two-digit number, split the digits and sum them individually.
  2. Sum of Doubled Digits
    • Add the results of all the doubled digits from step 1.
  3. Sum of Odd Digits
    • For the remaining digits (odd-positioned digits from right to left), just add them without doubling.
  4. Calculate Total Sum
    • Sum the results from steps 2 and 3.
  5. Final Check
    • If the total sum from step 4 is divisible by 10, the credit card number is valid. If not, it’s invalid.

Example Explanation

Take a sample credit card number and break it into steps to validate it.

  • Example number: 4539 1488 0343 6467

Step 1: Double every second digit (from right to left):

  • For each second digit from right to left:
    • 7 doubled = 14 (split into 1 + 4)
    • 6 doubled = 12 (split into 1 + 2)
    • 4 doubled = 8
    • 6 doubled = 12 (split into 1 + 2)
    • 3 doubled = 6
    • 8 doubled = 16 (split into 1 + 6)
    • 8 doubled = 16 (split into 1 + 6)
    • 9 doubled = 18 (split into 1 + 8)

The doubled digits (with splits where necessary) are: 1, 4, 1, 2, 8, 1, 2, 6, 6, 1, 6, 1, 8.

Step 2: Sum the doubled digits:

  • 1 + 4 + 1 + 2 + 8 + 1 + 2 + 6 + 6 + 1 + 6 + 1 + 8 = 29

Step 3: Sum the odd-positioned digits (from right to left):

  • 4, 3, 0, 8, 4, 3, 5 = 21

Step 4: Add the results from Step 2 and Step 3:

  • 29 + 21 = 50

Step 5: Check if the total sum is divisible by 10:

  • 50 % 10 = 0 (which means it is divisible by 10)

Since the sum is divisible by 10, this credit card number is valid.


Step-by-Step Code Explanation

1. Creating Functions

We will create functions to:

  • get_digit(): This function calculates the value of a doubled digit, splitting it into two digits if necessary.
  • sum_odd_digits(): This function sums the odd-positioned digits (without doubling them).
  • sum_even_digits(): This function sums the even-positioned digits (after doubling them).

2. Main Function

  • Declare Variables:
    • card_number: This will hold the inputted credit card number.
    • result: Holds the result of adding the sums of even and odd digits.
  • User Input:
    • Ask the user to input the credit card number.
  • Calling Functions:
    • Call the sum_even_digits() and sum_odd_digits() functions to calculate the required sums.
  • Validate Credit Card:
    • If the result is divisible by 10, the card is valid. If not, it’s invalid.

Detailed Function Breakdown

  1. get_digit() Function: This function is used to handle the scenario when a digit is doubled, and the result is a two-digit number. The function splits this number into its two individual digits and returns the sum of those digits.
    • Example: If 9 is doubled to 18, the get_digit() function returns 1 + 8 = 9.
    Code:
int get_digit(int number) {
	return number % 10 + number / 10 % 10;
}
 
  1. sum_even_digits() Function: This function iterates over the credit card number (from right to left) and doubles every second digit. It calls get_digit() to handle splitting if necessary.
    • Example: For credit card number 4539 1488 0343 6467, it will process every second digit starting from the rightmost one.
    Code:
int sum_even_digits(const std::string& card_number) {
	int sum = 0;
	for (int i = card_number.size() - 2; i >= 0; i -= 2) {
		int digit = card_number[i] - '0'; // Convert char to int
		sum += get_digit(digit * 2); // Double and pass to get_digit
	}
	return sum;
}
 
  1. sum_odd_digits() Function: This function iterates over the credit card number and adds all odd-positioned digits without doubling them.

    Code:

    int sum_odd_digits(const std::string& card_number) {
        int sum = 0;
        for (int i = card_number.size() - 1; i >= 0; i -= 2) {
            sum += card_number[i] - '0'; // Add the odd digits
        }
        return sum;
    }
     

Main Program Flow

  1. Get Credit Card Number:
    • Ask the user to enter the credit card number.
  2. Invoke Functions:
    • Calculate the sum of even and odd digits using the respective functions.
  3. Check Validity:
    • If the total sum is divisible by 10, the card is valid. Otherwise, it’s invalid.

Final Code Example

#include <iostream>
#include <string>
 
int get_digit(int number) {
    return number % 10 + number / 10 % 10;
}
 
int sum_even_digits(const std::string& card_number) {
    int sum = 0;
    for (int i = card_number.size() - 2; i >= 0; i -= 2) {
        int digit = card_number[i] - '0';
        sum += get_digit(digit * 2);
    }
    return sum;
}
 
int sum_odd_digits(const std::string& card_number) {
    int sum = 0;
    for (int i = card_number.size() - 1; i >= 0; i -= 2) {
        sum += card_number[i] - '0';
    }
    return sum;
}
 
int main() {
    std::string card_number;
    std::cout << "Enter a credit card number: ";
    std::cin >> card_number;
 
    int result = sum_even_digits(card_number) + sum_odd_digits(card_number);
 
    if (result % 10 == 0) {
        std::cout << "Card number is valid." << std::endl;
    } else {
        std::cout << "Card number is not valid." << std::endl;
    }
 
    return 0;
}
 

Conclusion

This program demonstrates how to use the Luhn algorithm in C++ to validate credit card numbers. By following the steps of the algorithm, we ensure that only valid credit card numbers are accepted. The main concepts covered include:

  • String manipulation
  • Mathematical operations (like modulo and division)
  • Handling even and odd-positioned digits
  • Validating using conditional checks.

The program can be tested with different card numbers and it correctly identifies valid and invalid numbers.


References