Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The last digit of a credit card number is the check digit, which protects agains

ID: 3561185 • Letter: T

Question

The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, I will describe it for numbers with 8 digits instead of 16: Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number if 43589795, then you form the sume 5 + 7 + 8 + 3 = 23. Double each of the digits that were not included in the preceding step. Add all digits of the resulting numbers. For example, with the number given above, doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Adding all the digits in these values yields 1 + 8+1 + 8 +1 + 0 + 8 = 27. Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid. Write a program which accepts an 8 digit number, and prints out whether the number is valid or not. Once your program works, add a loop which allows the user to repeat the process until a negative value is entered.

Explanation / Answer

Program:

#include <iostream>
using namespace std;
/*
*digitSum computes the sum of the digits of given number n
*/

int digitSum(int n){
   int sum=0;
   while(n!=0){
       sum += n%10;
       n /= 10;
   }
   return sum;
}

int main() {
   int number;                               //number given by user
   int n;                                   //copy of above number on which computation is done  
   int sum1=0;                               //the sum according to first point of question
   int sum2=0;                               //the sum according to second point of question
  
   cout<<"Enter first number"<<endl;
   cin>>number;
  
   while(number>=0){                       //while no negative number is given as input procceed
      
       sum1=0;                                  //initialization for each new number
       sum2=0;
       n = number;
      
       for(int place=1;place<=8;place++){       //8 digits in a number

           if(place % 2 == 0)                      //digits on which second point applies lies at even places
               sum2 += digitSum(2 * (n % 10));
           else                                                         //digits on which first point applies lies at odd places
               sum1 += n % 10;
      
           n /= 10;                                                   //dividing number by 10 each time as
                                                                            //we can have desired digit at unit place      
       }
      
       if((sum1+sum2) % 10 == 0)                       //sum has 0 at unit place
           cout<<"Number is invalid"<<endl;
       else
           cout<<"Number is valid"<<endl;
      
       cout<<"Enter next number"<<endl;       //take next number to check
       cin>>number;
   }
   return 0;
}

Compile & Run

Output:

Enter first number
43589795
Number is invalid
Enter next number
11111111
Number is valid
Enter next number
-1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote