1/ Write the pseudocode of Method Luhn in the word document that you will submit
ID: 3779044 • Letter: 1
Question
1/ Write the pseudocode of Method Luhn in the word document that you will submit along with the java file of this lab;
2/ Implement Method Luhn in the file challenge2.java and add relevant code in the main method to test it; and
3/ Provide two examples of input and the corresponding output for Method Luhn–one valid and one invalid credit card number.
Activity 1. [60 points] Method Luhn. You will have to implement the Luhn Checksum Validation algorithm to check whether a credit card number is valid or fake. This is a widely used algorithm that checks that you enter a valid credit card number when you are trying to make a purchase. Short description of the algorithm [Think Like a Programmer an Introduction to creative Problem solving by V. A. Spraulj: Using the original number, double the value of every other digit, starting with the leftmost one, as shown below. Then add the values of the individual digits together (if a doubled value now has two digits, add the digits together see below) The identification number is valid if the sum is divisible by 10 (i.e., the sum has to be a multiple of 10 Example of execution of the algorithm Suppose you want to check that your credit card number is valid. You credit card number is 8273 1232 7351 0569. Let's see how to check it: 8 2 7 3 1 2 3 2 5 6 9 First, you are going to double every other number, starting with the first number (here, it is number 8) 8 x 2 2 7 x 2 3 1 x 2 2 3 x 2 2 7 x 2 3 5 x 2 1 0 x 2 5 6 x 2 9 And you obtain 16 2 14 3 2 2 6 2 14 3 10 1 0 12 9 But we do not want double digits, so for every number that now has double digits, we add these digits 1 6 2 1 t 4 3 6 1 4 3 1 0 1 0 1 2 9 And we obtain 7 2 5 2 2 6 2 5 3 1 1 0 5 3 9 Now we add all of these numbers: 56 is not a multiple of 10, so the credit card number was a fake.Explanation / Answer
import java.util.*;
public class Creditcard {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter credit card number:");
String str=sc.next(); //reading input number as string
int len=String.valueOf(str).length(); //finding the length of the string
int zeroAscii=(int)'0'; //finding ascii value of zero
int index=1;
int sum=0;
int numascii;
while(len>1){ //loop condition because we are omiting one value i.e first
numascii=(int)(str.charAt(index)); //ascii value of second digit
sum=sum+(zeroAscii-numascii); //adding all values
index++;
len--;
}
int num=(int)(str.charAt(0)); //taking stariting digit
int mul=num*2; //doubling the digit
int num2=0;
int total;
while(mul>0){ //condtion to add mul if it is two digits
mul=mul%10;
num2=num2+mul;
mul=mul/10;
}
total=num2+sum;
if(total%10==0){ //checking whether number is multiplication of 10 or not
System.out.println("credit card number is true:");
}
else{
System.out.println("credit card number is false");
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.