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

Write a program to read credit card number and match the sum of credit card numb

ID: 3890058 • Letter: W

Question

Write a program to read credit card number and match the sum of credit card number

Step1: Read in credit card number as a series of digits into an array the number -1 is used to indicate the end of series, no need to read in -1 into the array The maximum size of the array is set to 20 Step2: Find sum1 (main should call a function passing array and size) lgnoring the last (right most digit) check digit of the credit card number, and moving left double the value of every second digit and find the sum of these doubled numbers. If the result of the doubling operation is a two digit number, you should add the digits of the doubled number before finding the sum. Step 3: find sum2 (main should call a function passing array and size) Find the sum of all other numbers (last digit is not included in this sum as well). Step 4: Calculate check sum Compute the total of sum1 and sum2 and multiply the result by 9, checksum is found by extracting the rightmost digit

Explanation / Answer

#include<iostream>

using namespace std;

int sum1(int A[], int n){
int sum = 0;
int n1;

for (int i = 0; i<n-1; i=i+2){
      if ( 2*A[i] > 9){
          n1 = 2*A[i];
          while(n1>0){
             sum = sum + n1 % 10;
             n1 = n1 /10;
          }
          
      }
      else {
         sum = sum+ 2 *A[i];
      }
     
}

return sum;    
}

int sum2(int A[], int n){
int sum = 0;
for (int i = 1; i<n-1; i=i+2){
      sum = sum+A[i];
}
return sum;
}

int main(){

   int A[20];
   int a;
   int s1,s2,s3;
   int last_digit;


   cout << "Enter digits and -1 to stop" << endl;
   int count = 0;
   for (int i = 0; i<20; i++){
        cin >> a;
        if (a == -1)
           break;
        A[i] = a;
        count++;
   }
  
   s1 = sum1(A,count);
   cout << "Sum1 = " << s1 << endl;
  
   s2 = sum2(A,count);
   cout << "Sum1 = " << s2 << endl;
   s3 = (s1 + s2) * 9;

   last_digit = s3 %10;
   if (last_digit == A[count-1])
       cout << "Valid crad num" << endl;
   else
       cout << "Invalid crad num" << endl;
   
   return 0;
}

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