Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with:
IBM proposed an algorithm for validating credit card numbers. The algorithm is used to determine if a card number is entered correctly or if a credit card is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly known as the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388576018402626):
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. Here are the three functions that you are required to write:
Your class Val looks like (10 points):
import java.util.Scanner;
public class Val {
public static
void main(String[] args) {
Scanner sc= new Scanner(System.in);
String x= sc.next();
int res1=AnaLR(x);
int res2=AnaRL(x);
int sum=res1+res2;
System.out.println(sum);
boolean bool=Div10(sum);
if(bool)
System.out.println("Credit Card " + x + " " + " is valid ");
else
System.out.println("Credit Card " + x + " " + " is not valid
");
}
Here are sample runs of the program:
SAMPLE 1:
Enter a credit card number as a long integer: 4246345689049834
4246345689049834 is invalid
SAMPLE 2:
Enter a credit card number as a long integer: 4388576018410707
4388576018410707 is valid
HINT: code for AnaLR():
public static int AnaLR(String num){
int total = 0;
for(int i = num.length() - 1; i >= 0; i -= 2){
total += Integer.parseInt(num.charAt(i) +"");}
return total;
}
Here is my code so far:
import java.util.Scanner;
public class Document8 {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String x= sc.next();
int res1=AnaLR(x);
int res2=AnaRL(x);
int sum=res1+res2;
//System.out.println(sum);
//boolean bool=Div10(sum);
boolean bool=false;
if(bool)
System.out.println("Credit Card " + x + " " + " is valid ");
else
System.out.println("Credit Card " + x + " " + " is not valid
");
}
public static int AnaRL(String x){
int total=0;
for(int i = x.length() - 2; i >= 0; i -= 2){
total += Integer.parseInt(x.charAt(i)
+"");}
return total;
}
public static int AnaLR(String num){
int total = 0;
for(int i = num.length() - 1; i >= 0; i -=
2){
total += Integer.parseInt(num.charAt(i) +"");}
return total;
}
}
To finish AnaRL() I was told to Extract every odd value from the string. Multiply by 2. If its doubled value <= to 9 add it. If it is >= 10 then subtract 9 from the final value but I have no idea how to do that or finish the program for that matter. I am new to programming. If you could help me finish my program in the simplest way would be very much appreciated.
package luhncheck;
import java.util.Scanner;
public class LuhnCheck {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.print("Enter a credit card number as a long integer:
");
Scanner scanner = new Scanner(System.in);
checkLuhn(scanner.nextLine().trim());
}
private static void checkLuhn(String CardNumber) {
int[] ints = new int[CardNumber.length()];
for (int i = 0; i < CardNumber.length(); i++) {
ints[i] = Integer.parseInt(CardNumber.substring(i, i + 1));
}
for (int i = ints.length - 2; i >= 0; i = i - 2) {
int j = ints[i];// take second digit
j = j * 2; // double it
if (j > 9) { // if it is in double digit then take reminder and
add 1 to get single digit number
j = j % 10 + 1;
}
ints[i] = j;
}
int sum = 0;
// add all odd digits and doubled values of each second digit
for (int i = 0; i < ints.length; i++) {
sum += ints[i];
}
if (sum % 10 == 0) {
System.out.println(CardNumber + " is a valid ");
} else {
System.out.println(CardNumber + " is an invalid");
}
}
}
package HomeworkLib1; import java.util.Scanner; public class Val{ public static void main(String[] args) { int n=2; while(n>0) { Scanner sc= new Scanner(System.in); System.out.println("Enter card no: "); String x= sc.next(); int res1=AnaLR(x); int res2=AnaRL(x); int sum=res1+res2; System.out.println(sum); boolean bool=Div10(sum); if(bool) System.out.println("Credit Card " + x + " " + " is valid "); else System.out.println("Credit Card " + x + " " + " is not valid "); n--; } } //End of main public static int AnaLR(String num){ int total = 0; for(int i = num.length() - 2; i >= 0; i -= 2){ int x = Integer.parseInt(num.charAt(i) +""); x *= 2; if(x<10) //if x is single digit number total += x; else { //if x is double digit number x = x/10 + x%10; //add digit at tens place and digit at unit place total += x; //Add to total } } return total; } public static int AnaRL(String x){ //sum of odd places int total=0; for(int i = x.length() - 1; i >= 0; i -= 2){ total += Integer.parseInt(x.charAt(i) +"");} return total; } public static boolean Div10(int x) { boolean f = false; if(x%10 == 0) //If divisible f = true; return f; } }
Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...
Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards 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 is scanned correctly by a scanner. Almost all credit card numbers...
Credit card numbers adhere to certain constraints. First, a valid credit card number must have between 13 and 16 digits. Second, it must start with one of a fixed number of valid prefixes : 1 • 4 for Visa • 5 for MasterCard • 37 for American Express • 6 for Discover cards In 1954, Hans Peter Luhn of IBM proposed a “checksum” algorithm for validating credit card numbers . 2 The algorithm is useful to determine whether a card...
Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards 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...
Please answer in Visual Studio 2019 c# format. Not
python. Thank you.
Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...
Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...
Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...
Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of N-squared. I.E. No nested for loops in rowIsLatin and colIsLatin. Only nested loop allowed in goodSubsquare. public class Sudoku { public String[][] makeSudoku(String s) { int SIZE = 9; int k = 0; String[][] x = new String[SIZE][SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++)...
I am trying to run a couple of functions in my main and It won't output anything if there is anything that you see wrong please help me out. I have to use simple, sometimes slower ways because we can use what we haven't learned yet. Here is my code. package a3; import java.util.Scanner; public class LoopsAndImages { public static void main(String[] args){ String test = "A rabbit has a carrot"; Boolean numbers = hasMoreEvenThanOdd("12344"); System.out.println(test); System.out.println(hideLetterA(test)+" This is the...
import java.util.Scanner; public class SieveOfEratosthenes { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a number"); int num = sc.nextInt(); boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false;...
I want it in C++
4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...