Question

In: Computer Science

Many types of identification numbers, including credit card numbers, must satisfy the Luhn Algorithm in order...

Many types of identification numbers, including credit card numbers, must satisfy the Luhn Algorithm in order to be considered "valid". This algorithm verifies the number by performing the following operation: starting from the right-most digit, double every 2nd digit. If this doubling causes that digit to be greater than 9, subtract 9 from it. Now add up all the new digits of the number (including the digits that weren't doubled). If the sum is evenly divisible by 10, then the number is valid, otherwise it is not. For example, for the number 2395129857 this would be:

2 3 9 5 1 2 9 8 5 7

2x2 3 9x2 5 1x2 2 9x2 8 5x2 7 (doubling every 2nd digit from right)

4 3 18 5 2 2 18 8 10 7

4 3 9 5 2 2 9 8 1 7 (subtract 9 from any digit greater than 9)

4 + 3 + 9 + 5 + 2 + 2 + 9 + 8 + 1 + 7 = 50

Since 50 is divisible by 10, this number satisfies the Luhn criteria, and is therefore valid.

a) Write a C function called check_luhn that takes a single integer as input, and returns 1 if the number is valid according to the Luhn algorithm, or 0 if not. HINT: Start from the right-most digit, and use simple C operators to find both the digit and the remaining number once that digit is removed. Then process that number if necessary and add it to the sum.

b) Using the function written in part (a), write a C program to print all of the "valid" numbers between 10000000 and 99999999

c) Please provide a short description (in bullet points) of an explanation of how the program works

Solutions

Expert Solution

// C program to display all the valid numbers between 10000000 and 99999999 using luhn algorithm

#include <stdio.h>
#include <stdlib.h>

// function declaration
int check_luhn(int number);

int main()
{
// set start counter to 10000000
int start = 10000000;
printf("Valid Numbers between 10000000 and 99999999:\n");

// loop from 10000000 to 99999999(inclusive)
while(start <= 99999999)
{
// if start is a valid number, display the number
if(check_luhn(start))
printf("\n%d",start);
start += 1; // increment start by 1
}

return 0;
}

// function to return 1 if number is valid else return 0
int check_luhn(int number)
{
int lastDigit = number%10; // get the last digit of the number and store it in lastDigit
number = number/10; // get the number after removing the last digit

// initialize sum to 0
int sum = 0, digit;
int isEven = 1; // set isEven to 1 (to determine if the digit is even or not starting with the second last)

// loop that continues till number > 0
while(number > 0)
{
digit = number%10; // get the last digit of the number
number /= 10; // remove the last digit from the number
if(isEven) // check if this is an even digit
{
isEven = 0; // negate isEven
digit *= 2; // double the digit
if(digit > 9) // if digit > 9, subtract 9 from digit
digit -= 9;
}else // not an even digit, negate isEven
isEven = 1;

sum += digit; // add digit to sum
}

// if sum +lastDigit is divisible by 10, it is a valid number
if((sum+lastDigit)%10 == 0)
return 1;
else // not a valid number
return 0;
}

//end of program

Output: (Some of the numbers)


Related Solutions

IN C# In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers....
IN C# In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402626): 1. Double every second digit from right...
In order for a class action to be certified by the court it must satisfy the...
In order for a class action to be certified by the court it must satisfy the court that it posses five key elements or criteria. Identify and briefly discuss each of these elements. What is the purpose of workers' compensation legislation and how does this legislation modify tort law? Explain how the same set of facts can give rise to liability in tort and in contract. Does negligence require perfect conduct by the defendant to excuse liability? Explain. Describe the...
Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid...
Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid is shown below:We assume that there are 16 digits in a credit card number.Start from the first digit of the credit card number as in position 1; second digit as in position 2; so on until the last digit as in position 16;If a digit is in even numbered position, take the digit itself as its representative;If a digit is in odd numbered position,...
11.4 What are the basic requirements that a trainee must satisfy in order to get the...
11.4 What are the basic requirements that a trainee must satisfy in order to get the most out of training?
A6-5. Suppose you always use your credit card for purchases. Your credit card limit must then...
A6-5. Suppose you always use your credit card for purchases. Your credit card limit must then be thought of as part of your money holdings. A6-6. In the long run, an economy that is open to capital flows can have investment greater than national saving. A6-7. A central bank that targets inflation would conduct an expansionary monetary policy when faced with a recessionary gap. A6-8. If the policy response discussed in A6-7 is mistimed, it risks becoming pro-cyclical rather than...
Identify precautions for accepting the types of payments: cash, check, credit card, and debit card.   
Identify precautions for accepting the types of payments: cash, check, credit card, and debit card.   
The following algorithm is widely used for checking whether a credit or debit card number has...
The following algorithm is widely used for checking whether a credit or debit card number has been entered correctly on a website. It doesn't guarantee that the credit card number belongs to a valid card, but it rules out numbers which are definitely not valid. Here are the steps: Check that the long number has exactly 16 digits. If not, the long number is not valid. If the long number has 16 digits, drop the last digit from the long...
Question 3: Credit contracts a) What information must be disclosed to satisfy the requirements of initial...
Question 3: Credit contracts a) What information must be disclosed to satisfy the requirements of initial disclosure under the Credit Contracts and Consumer Finance Act 2003? Refer to a section in the Act to support your answer. b) To who do the oppression parts of the Credit Contracts and Consumer Finance Act 2003 apply? Refer to a section in the Act to support your answer. c) Hamish is a lawyer who owns a home with expansive sea views in an...
Econometrics Question (a) What properties must a time series {yt} have to satisfy in order for...
Econometrics Question (a) What properties must a time series {yt} have to satisfy in order for it to be a covariance stationary process? Let {ut} be a sequence of i.i.d. N(0,1) variables. Let yt= α+βt+ut, and yt = yt -yt-1. (b) Is {yt} covariance stationary? Justify your answer. (c) Would your answer to part (b) change if {ut} were a moving average of order 1? (d) Is {yt} covariance stationary? Justify your answer.  = change in, won't allow a...
Econometrics Question (a) What properties must a time series {yt} have to satisfy in order for...
Econometrics Question (a) What properties must a time series {yt} have to satisfy in order for it to be a covariance stationary process? Let {ut} be a sequence of i.i.d. N(0,1) variables. Let yt= α+βt+ut, and yt = yt -yt-1. (b) Is {yt} covariance stationary? Justify your answer. (c) Would your answer to part (b) change if {ut} were a moving average of order 1? (d) Is {yt} covariance stationary? Justify your answer.  = change in, won't allow a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT