Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have another question. Please, Let me know if this is overstepping the rules f

ID: 3838217 • Letter: I

Question

I have another question. Please, Let me know if this is overstepping the rules for posting. Here is the work I can't find a way to code:

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 :

1.. Double every second digit from right to left. If doubling of a digit results in a 2-digit number, add up the two digits to get a single-digit number.

Look at this 16 - digit number:   4 3 8 8 5 7 6 0 1 8 4 0 2 6 2 6

4 * 2 = 8

8 * 2 = 16 -> 1 + 6 = 7

5  * 2 = 10 -> 1 + 0 = 1

6  * 2 = 12 -> 1 + 2 = 3

1  * 2 = 2

4  * 2 = 8

2  * 2 = 4

2  * 2 = 4

2. Add all single-digit numbers from Step 1. In this case the sum is 37

3. Add all digits in the odd places from right to left in the card number, in this case 38

4. Sum the results from Step 2 & 3 , in this case, 37 + 38 = 75

E. Display if the number is valid or not.

It's to be in three different .javas. One that gives the mean, median and mode. One that sums up the digits and a "Credit card Validation"

I don't really know how to separate the questions into different parts as you would need the first table given regardless and the answers to solve the next question (From the way I see it that is.) I need it all in Java. Thank you once again in advance

Explanation / Answer


import java.util.*;
import java.io.*;
public class CreditCardValidation {
   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       long cardnumber;
       String countcheck;
       String cardtostring="";
       while(true){
       System.out.println("Enter Credit card number:");
      
       cardnumber=sc.nextLong();
           countcheck=String.valueOf(cardnumber);
           if(countcheck.length()==16)
              
               break;
       }
       cardtostring=String.valueOf(cardnumber);
       long cardarr[]=new long[cardtostring.length()];
       for(int i=0;i<cardarr.length;i++){
           cardarr[i]=cardtostring.charAt(i)-'0';
       }
       long righttoleftsum=0;
       long leftotrightsum=0;
       long digit=0;
       long digit2=0;
for(int j=cardarr.length-2;j>=0;j-=2){
   digit= cardarr[j]*2;
   if(digit<=9){
       righttoleftsum+=digit;
   }else{
       String toval=String.valueOf(digit);
       long v1=toval.charAt(0)-'0';
       long v2=toval.charAt(1)-'0';
       righttoleftsum+=v1+v2;
   }
     
     
}
System.out.println("sum of rigt to left fomr second place:"+righttoleftsum);
for(int k=cardarr.length-1;k>=0;k-=2){
   leftotrightsum+= cardarr[k];

}
System.out.println("sum of right to left from first place:"+leftotrightsum);
long totalsum=leftotrightsum+righttoleftsum;
System.out.println("Total sum:"+totalsum);
if(totalsum%10==0)
   System.out.println("card number is valid:");
else
System.out.println("card number is invalid:");
   }
}

output:
for given input:4388576018402626
Enter Credit card number:
4388576018402626
sum of rigt to left fomr second place:37
sum of right to left from first place:38
Total sum:75
card number is invalid:

output:
for valid number
Enter Credit card number:
1234
Enter Credit card number:
12345678
Enter Credit card number:
4388576018410707
sum of rigt to left fomr second place:29
sum of right to left from first place:41
Total sum:70
card number is valid:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote