The program should ask the user for the digits (between 13 and 16) of his credit
ID: 3536812 • Letter: T
Question
The program should
ask the user for the digits (between 13 and 16) of his credit card, and tell him if
his
(when the first number is 4 for Visa cards
5 for Master cards
37 for American Express cards
6 for Discover cards) is valid or not valid.
As a reminder, LUHN algorithm, take first, third, fifth, seventh,nineth, eleventh, thirteenth, fifteenth(when applies), digits. multiply it by 2 and IF the number obtained is bigger than 10 you ad the digits obtained (7 --> 7*2=14 --> 1+4=5)
You'll now get a new card number,
7175280148901 will become 5155480188902
5+1+5+5+4+8+0+1+8+8+9+0+2= 56
therefore 7175280148901 isn't valid and 7175280148901
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char d[20];
printf("Enter the credit card no: ");
scanf("%s",d);
int n=0;
int l= strlen(d);
int i=0;
int sum=0;
while(i<l)
{
n=d[i]-'0';
if(i%2 == 0)
{
n=n*2;
if(n>9) { n=n-9; }
}
sum=sum+n;
i++;
//if you want to print the new number, uncomment the following two printfs
//printf("%d",n);
}
//printf(" ");
if(sum%10 == 0 )
{ printf("The number is VALID "); }
else
{ printf("The number is INVALID "); }
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.