Question: Write the following code in C: The program will check something calle Luhns checksum. It is a checksum invented and patented by Hans Peter Luhn,

Write the following code in C:

The program will check something calle Luhns checksum. It is a checksum invented and patented by Hans Peter Luhn, a researcher at IBM, in 1954. It is a basic integrity check that can usually detect accidental errors in numbers such as credit cards, or identification numbers. For example, most credit card and bank card numbers use this checksum, and many national identification numbers, including Canadian SIN numbers use it. The checksum works as follows: If a sequence of decimal digits is given, let us say dkdk1 . . . d2d1, if we index digits from right to left, then we calculate the Luhns checksum as follows:

1. Starting at the right we transform each second digit d2, d4, etc., by multiplying it by 2. If this product is a two-number, i.e., if it is greater than 9, then we add those two digits and replace the results with this sum.

2. We add all digits of the number, the odd-positioned digits, and the transformed even-positioned digits. If the result is divisible by 10, the number passes the checksum test, otherwise not.

We can consider an example: 1234567890123411

After transformations in the step 1, we get digits: 2 2 6 4 1 6 5 8 9 0 2 2 6 4 2 1 and when we add them up, we get 60, so the number passes the test. Your program needs to test a sequence of such numbers.

Input: The first line of input contains a single integer T (1 T 100), the number of test cases. Each of the following T lines contains a single test case consisting of a number given as a string of base-10 digits (09). The length of each string is between 2 and 50, inclusive, and numbers may have leading (leftmost) zeros. Output: For each test case, output a single line containing PASS if the number passes the Luhn checksum test, or FAIL if the number fails the Luhn checksum test.

For example, for sample input:

00554

999

1234567890123411

the output should be:

PASS

FAIL

PASS

Since you know that the test numbers are not longer than 50 digits, you should read them as strings of up to 50 digits. This can be done using scanf("%50s", specification and a character array of appropriate length. Since the digits read in this way are ASCII digits, you will copy them to an int array in which each digit is transformed to it actual value. For example, this can be done with expression ch - 0 where ch is the char value of the digit. The array of integers created in this way, should be passed to a function with prototype: int luhn_checksum(int len, int a[]); where len is length of array a of digits that needs to be checked. If the checksum passes, the function should return 1, otherwise it should return 0.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!