Write a program that prompts the user to enter a credit card number as a long integer and Display whether that card is valid or invalid.
Credit card numbers follow certain patterns.
A credit card number must have between 13 and 16 digits. It must start with:
- 4 for Visa cards
- 5 for Master cards
- 37 for American Express cards
- 6 for Discover cards
The problem can be solved by using Luhn algorithm.
Luhn check or the Mod 10 check, which can be described as follows (for illustration,
consider the card number 4388576018402626):
Step 1. Double every second digit from right to left. If doubling of a digit results in a
two-digit number, add up the two digits to get a single-digit number (like for 12:1+2, 18=1+8).
Step 2. Now add all single-digit numbers from Step 1.
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
Step 3. Add all digits in the odd places from right to left in the card number.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Step 4. Sum the results from Step 2 and Step 3.
37 + 38 = 75
Step 5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid.
Input : 379354508162306 Output : 379354508162306 is Valid Input : 4388576018402626 Output : 4388576018402626 is invalid
5196081888500645 is valid
This article is contributed by Vishal Kumar Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- A Program to check if strings are rotations of each other or not
- Write a program to print all permutations of a given string
- Write a program to reverse an array or string
- Given a number, find the next smallest palindrome
- Program to validate an IP address
- Find Excel column name from a given column number
- How to turn off a particular bit in a number?
- Given a binary string, count number of substrings that start and end with 1.
- Factorial of a large number
- Function to find Number of customers who could not get a computer
- Build Lowest Number by Removing n digits from a given number
- Converting Decimal Number lying between 1 to 3999 to Roman Numerals
- Transform One String to Another using Minimum Number of Given Operation
- Minimum number of bracket reversals needed to make an expression balanced
- Form minimum number from given sequence
- 1's and 2's complement of a Binary Number
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Find all strings formed from characters mapped to digits of a number
- All combinations of strings that can be used to dial a number
- Count number of substrings with exactly k distinct characters
Improved By : nitin mittal