08 - int digit =(int(num 10 num = num 10...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 import java.util.* public class CCValidator { public static void main (String [] args) { Scanner sc = new Scanner(System.in) System.out.print("Enter potential CC number: ") long input = sc.nextLong() boolean isValid = validate(input) System.out.println("Card is valid: " + isValid) } public static boolean validate (long num) { int sum = 0 boolean positionIsOdd = true while (num > 0)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { int digit = (int)(num % 10) num = num / 10 System.out.println("Current digit: " + digit) if (positionIsOdd) { sum = sum + digit } else { digit = digit * 2 if (digit > 9) // two-digit value { digit = digit - 9 } sum = sum + digit } positionIsOdd = !positionIsOdd // Flip odd/even status System.out.println("Sum is now " + sum) } return (sum % 10 == 0) } }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online