A credit card number has a number of digits and the last one (the rightmost) is
ID: 3563841 • Letter: A
Question
A credit card number has a number of digits and the last one (the rightmost) is a checksum.
For example, if the credit card number is 347, the 7 is a check digit.
The check digit is made up as follows. Add up the digits at all other positions and find the remainder
after division by 10.
In the above example, the digits 3 and 4 add up to 7 and the remainder after division by 10 is 7. So
the final credit card number is 347.
Write a program that can compute the check digit after accepting all other digits from the keyboard.
Then print the final number including all digits in the correct sequence. That is, the digits should
appear in the order they were input followed by the check digit. Assume that only digits will be
input.
Sample Executions
Enter card number: 34
347
>>> ================================ RESTART ================================
>>>
Enter card number: 2352
23522
>>>
Explanation / Answer
Code:
#!/usr/local/bin/python2.7
'define the sum_digits function with user input as parameter
def sum_digits(n):
sum = 0
#while loop that split the given number and adds it to
#the sum integer
while n:
sum += n % 10
n /= 10
return sum
'Requesting the user to enter the input
print "Enter card number ",
num=input();
'call the sum_digits function and store the value in
' result
result=sum_digits(num);
r=result%10;
'convert the result into string inorder to append the check sum value
s=str(num);
'append the check sum to the existing number
s+=str(r);
'print the card number
print "The resultant card number is: " , s;
--------------------------------------------------------------------
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.