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

c++ coding Step1: Read in credit card number as a series of digits into an array

ID: 3871531 • Letter: C

Question

c++ coding

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,n2;

cout << "Number for sum1 are ";
if (n % 2 == 0){
     n2 = 0;
}
else {
    n2 =1;
}
for (int i = n2; i<n-1; i=i+2){
      if ( 2*A[i] > 9){
          cout << A[i] << " ";
          n1 = 2*A[i];
          while(n1>0){
             sum = sum + n1 % 10;
             n1 = n1 /10;
          }
          
      }
      else {
         sum = sum+ 2 *A[i];
      }
     
     
}
cout << endl;
return sum;    
}

int sum2(int A[], int n){
int sum = 0;
int n2;
if (n % 2 == 0){
     n2 = 1;
}
else {
    n2 =0;
}
cout << "Number for sum2 are ";
for (int i = n2; i<n-1; i=i+2){
      cout << A[i] << " ";
      sum = sum+A[i];
}
cout << endl;
return sum;
}

int main(){

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


   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++;
   }
   cout << "Credit card number is:";
   for (int i = 0; i<count; i++){
       cout << A[i];
   }
   cout << endl;
   s1 = sum1(A,count);
   cout << "Sum1 is " << s1 << endl;
  
   s2 = sum2(A,count);
   cout << "Sum2 is " << s2 << endl;
   s3 = (s1 + s2) * 9;

   checksum = s3 %10;
   cout << "Checksum is " << checksum << endl;
   cout << "Last digit on credit card is " << A[count-1] << endl;
   if (checksum == A[count-1])
       cout << "checksum " << checksum << " and the last digit "<< A[count-1] << " are same: Valid card number" << endl;
   else
       cout << "checksum " << checksum << " and the last digit "<< A[count-1] << " are not same: Invalid card number" << 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