Question
This code (in c++) is used to read an 8-digit card number and tell if it is valid or not, then display the correct check
This code (in c++) is used to read an 8-digit card number and tell if it is valid or not, then display the correct check digit. However, the check digit does not come out right. Any help would be appreciated
#include using namespace std;
int main()
{ int n; while (1) { int sum1 = 0; int sum2 = 0; int sum3 = 0;; int i = 0; cout << " Enter the credit card number: "; cin >> n; if (n == -1) break; else { while (n > 0) { if (i % 2 == 0) { sum1 = sum1 + n % 10; i = i + 1; } else { sum2 = ((n % 10) * 2); while (sum2 > 0) { sum3 = sum3 + sum2 % 10; sum2 = sum2 / 10; } i = i + 1; } n = n / 10; } if ((sum1 + sum3) % 10 == 0) cout << ("Valid Credit Card Number "); else { int i; for (i = 1; i <= 9; i++) { if ((sum3 + sum1 + i) % 10 == 0) break; } cout << "Not a Valid Credit Card Number. The check digit number to be added is " << i; } } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started