For example, 8546 9234 8771 452X (X is the check digit) Add up each digit: 8+5+4
ID: 3548317 • Letter: F
Question
For example,
8546 9234 8771 452X
(X is the check digit)
Add up each digit:
8+5+4+6+9+2+3+4+8+7+7+1+4+2+5=75
For this to be evenly divisible by 10, you would need to add 5 to make the total result equal 80. So the check digit or X would equal 5.
For this program you need to write two functions. The First function verifies that a credit card number is valid. Ignore spaces in the credit card number, but other non-numeric characters will result in the number being invalid as will fewer/more than 16 digits. Write a Second function that randomly generates a VALID card Number
Finally, write a main function that creates 10 randomly generated numbers and validates them. Next, allow the user to enter card numbers and have the program tell the user whether their card number is valid or not.
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
string number;
int T=10;
while(T--)
{
number="";
cout<<"Generating a number"<<endl;
int i;
for(i=1;i<=16;i++){
number=number+(char)(48+rand()%10);
}
cout<<number<<endl;
int sum=0;
for(i=0;i<number.length()-1;i++)
{
int ascii=(int)(number[i]);
if(ascii>=48 && ascii<=57)
{
sum=sum+(int)(number[i]-48); //Taking the sum of the first 15 digits
}
}
int last_digit=(int)(number[number.length()-1]-48);
if((sum+last_digit)%10==0) //Checking whether the last digit + the sum calculated is divisible by 10
{
cout<<"Valid"<<endl;
}
else
{
cout<<"Not valid"<<endl;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.