# Thread: [Help] Program : How to validate credit card number

1. ## [Help] Program : How to validate credit card number

I'm beginer and I have a workshop.

I need to code a program to check validate of Master Card.

Ex:

Check this card number: 5368235896831135

Step 1: Starting from the second digit from the right and moving towards the left, multiply every digit by 2.

5368235896831135
^ ^ ^ ^ ^ ^ ^ ^

3*2=6
1*2=2
8*2=16
9*2=18
5*2=10
2*2=4
6*2=12
5*2=10

Step 2: Sum the digits from Step 1, if the value is bigger than 9, we must add 2 digits together. Ex : 16 = 1+6 = 7

So we have : 6 + 2 + (1+6) + (1+8) + (1+0) + 4 + (1+2) + (1+0) = 33

Step 3
: Sum all of the digits not originally multiplied by 2 (without begining number from right: 5)

So we have: 3+8+3+8+6+3+1=32

Step 4: Sum together the results from Step 2 and Step 3.

33 + 32 = 65

Step 5
: Subtract the sum from the next highest multiple of 10

70 - 65 = 5 [check digit]

Use datatype: _int64 master_card;

scanf("%l64d", &master_card); for reading 16 digits of master card

Thk 2. The real question is, why are you writing a CC# generator?

Quzah. 3. no, it only is my homework.plz help me 4. So what's the problem?
- extracting digits
- multiplication.

You've got clear easy steps, make an effort. 5. yes but I'm beginer so i can't write it with C programing. Can anyone help me write a full program to learn :-s 6. NO.

When you genuinely get stuck with your own attempt, then we'll help.

But this "dump your assignment" and "gimme the code to learn from" neither washes, nor works. 7. ok thks.

I can't understand:

Use datatype: _int64 master_card;

scanf("%l64d", &master_card); for reading 16 digits of master card

can you give me ex? 8. What don't you understand?

The scanf part?
Or the I64 part?

FWIW, it seems your tutor has told you that line of code so that you wouldn't have to think too much.

Just use the code given, and post some code to solve step 1 of your assignment.

Code:
```#include <stdio.h>
int main ( ) {
_int64 master_card;
scanf("%l64d", &master_card);
return 0;
}``` 9. when i use:
master_card=master_card%1000000000000000;
I use it to remove beginning number (number)

Dev-C++ said: integer constant is too lager for "long" type.

What should i do?

I Wrote my program

Code:
```#include<stdio.h>
#include<conio.h>
main()
{
__int64 master_card;
int S1, S2, S, CDigit, number1, number2;
S1=S2=S=CDigit=0;
printf("Enter Credit Card Number: \n");
scanf("%I64d", &master_card);
master_card=master_card/10;
for(int i=1;i<=13;i++)
{

number1=master_card%10;
number1=number1*2;
if (number1>9) number1=number1-9;
S1=S1 + number1;
master_card=master_card/10;
number2=master_card%10;
S2=S2+ number2;
};
S=S1+S2;
CDigit=10-(S%10);
if (CDigit==5) printf("Your card is validation");
else printf("Your card is not validation");
getch();
}```
Can you edit to right for me. I think i am wrong at for 10. Use the appropriate constant suffix, eg.

Code:
`master_card=master_card%1000000000000000LL;` 11. thk man but i think i have finished.

Code:
```#include<stdio.h>
#include<conio.h>

main()
{
__int64 master_card;
int S1, S2, S, CDigit, number1, number2, temp;
S1=S2=S=CDigit=0;
printf("Enter Credit Card Number: \n");
scanf("%I64d", &master_card);
for(int i=1;i<=8;i++)
{
master_card=master_card/10;
number1=master_card%10;
number1=number1*2;
if (number1>9) number1=number1-9;
S1=S1 + number1;
master_card=master_card/10;
number2=master_card%10;
S2=S2+ number2;
};
S=S1+S2;
CDigit=10-(S%10);
if (CDigit==5) printf("\nYour card is validation");
else printf("\nYour card is not validation");
getch();
}``` 12. > if (CDigit==5) printf("\nYour card is validation");
Aren't you supposed to compare with the last digit of the card?

Other than that, good job. 13. i think my homework is check for Master Card only. thk you very much 14. kingofdcp

Good trick its working Popular pages Recent additions 