Question

In: Computer Science

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 to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. 2. Now add all single-digit numbers from Step 1. 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37 3. Add all digits in the odd places from right to left in the card number. 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38 4. Sum the results from Step 2 and Step 3. 37 + 38 = 75 5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid. Write a program that prompts the user to enter a credit card number as a long integer. Display whether the number is valid or invalid. Design your program to use the following methods: Use STRING as an input. Add methods

Solutions

Expert Solution

The complete solution of your question given below,

using System;

using System.Collections;

class HelloWorld {

static void Main() {

    string str;

    int len,oddsum=0,evensum=0,i,j,sum=0;

    Console.WriteLine("enter a credit card number");

        str=Console.ReadLine();

        len=str.Length;

        if(len==16) // Check length of the CC number is 16 or not

        {

            for(i=len-1;i>=0;i--) // Reading number from right to left

            {

                int val = (int)Char.GetNumericValue(str[i]);

                if(i%2!=0) // Find out the position of number is odd or even

                {

                    oddsum=oddsum+val;

                }

                else

                {                   

                    val=val*2; // Double the value of every second digit

                    if(val>9) // Check the result is two digit

                    {                      

                        String num=val.ToString();

                        int n1=(int)Char.GetNumericValue(num[0]);

                        int n2=(int)Char.GetNumericValue(num[1]);

                        int res=n1+n2;

                        evensum=evensum+res;

                    }

                    else

                    evensum=evensum+val;

                }

            }

            sum=oddsum+evensum; // Sum the oddsum and evensum results

            if(sum%10==0) //Check the result is divisible by 10 or not

            Console.WriteLine("valid");

            else

            Console.WriteLine("invalid");

        }

        else

        {

            Console.WriteLine("Enter valid CC number");

        }       

}

}

note : Read the comments for better understanding of the solution


Related Solutions

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...
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,...
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...
I need to show this using SQL: Credit card numbers should include asterisks in place of...
I need to show this using SQL: Credit card numbers should include asterisks in place of all digits preceding the last four digits, which will be left visible, regardless of credit card length
I need java code for this ..thx Question 2 Credit card numbers are not completely random...
I need java code for this ..thx Question 2 Credit card numbers are not completely random sequences; they follow certain rules depending on the card issuer. A MasterCard number must meet these criteria: • Begin with 51, 52, 53, 54, 55, or something in the range 222100-272099 • 16 digits in length • Satisfy the Luhn formula, created by IBM scientist Hans Peter Luhn in the 1950s Here’s how the Luhn formula works: Double every other digit going backwards, starting...
Propose an algorithm in C to match numbers / tokens (words) from one array to another...
Propose an algorithm in C to match numbers / tokens (words) from one array to another array and pull out the matching numbers.
Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375...
Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375 4657 is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning depends on the type of card.) The last digit it special; it is computed from the previous digits and...
1. (2 pts each) Consider the following algorithm: procedure polynomial(c, a0,a1,…an: real numbers) power≔1 y≔a0for i=1...
1. (2 pts each) Consider the following algorithm: procedure polynomial(c, a0,a1,…an: real numbers) power≔1 y≔a0for i=1 to n   power≔power*cy≔y+ai*power return y (Note: y=ancn + an-1 cn-1 +. . . + a1C +a0 so the final value of y is the value of the polynomial at x=c) a. Use the algorithm to calculate f(3), where f(x)=2x2+3x+1 at x=3. Show the steps of working through the algorithm – don’t just plug 3 in for x in f(x). b. How many multiplications and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT