[Help] Program : How to validate credit card number

This is a discussion on [Help] Program : How to validate credit card number within the C Programming forums, part of the General Programming Boards category; I'm beginer and I have a workshop. I need to code a program to check validate of Master Card. Ex: ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Question [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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The real question is, why are you writing a CC# generator?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    16
    no, it only is my homework.plz help me

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    So what's the problem?
    - extracting digits
    - addition
    - multiplication.

    You've got clear easy steps, make an effort.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    NO.
    Start with "hello world" like everyone else, and work your own way towards the solution.

    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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);
        // your code goes here
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    16
    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
    Last edited by Salem; 05-17-2009 at 10:01 AM. Reason: Use [code][/code] tags when posting code.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Use the appropriate constant suffix, eg.

    Code:
    master_card=master_card%1000000000000000LL;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    16
    i think my homework is check for Master Card only. thk you very much

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    1
    kingofdcp


    Good trick its working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21