A bank performs a few checks on an account number before accepting it.
Design a class AccountVerifier to accept one 10-digit account number and perform the following tasks:
Check whether the account number has exactly 10 digits. If not, display INVALID FORMAT and stop.
Starting from the right, double every second digit and leave the other digits unchanged. If a doubled value is greater than 9, subtract 9 from it. Add the resulting digits.
If the total is divisible by 10, display Checksum Valid; otherwise, display Checksum Invalid.
For example: 4388576018
4 × 2 = 8
8 × 2 = 16 → 16 – 9 = 7
5 × 2 = 10 → 10 – 9 = 1
6 × 2 = 12 → 12 – 9 = 3
1 × 2 = 2
Now add: 8 + 3 + 7 + 8 + 1 + 7 + 3 + 0 + 2 + 8 = 47
47 is not divisible by 10.
Hence, Checksum Invalid.
Add up the number’s original digits. If the result has more than one digit, add its digits again and repeat until a single digit remains. This final single digit is the Digital Root.
If the Digital Root is odd, display “Root: Odd”
If even, display “Root: Even”
Test your program with the following data and some random data:
Example 1
INPUT: Account Number: 1000000016
OUTPUT:
Account Number: 1000000016
Checksum: Valid
Digital Root: 8 (Root Even)
Example 2
INPUT: Account Number: 4388576018
OUTPUT:
Account Number: 4388576018
Checksum: Invalid
Digital Root: 5 (Root Odd)
Example 3
INPUT: Account Number: 123456789
OUTPUT:
Account Number: 123456789
INVALID FORMAT
import java.util.Scanner;
class AccountVerifier{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Account Number: ");
long accNum = in.nextLong();
System.out.println("Account Number: " + accNum);
int sum = 0;
int len = String.valueOf(accNum).length();
if(len != 10){
System.out.println("INVALID FORMAT");
return;
}
long temp = accNum;
for(long i = 1; i <= 10; i++){
int d = (int)(temp % 10);
if(i % 2 == 0){
d *= 2;
if(d > 9)
d -= 9;
}
sum += d;
temp /= 10;
}
if(sum % 10 == 0)
System.out.println("Checksum: Valid");
else
System.out.println("Checksum: Invalid");
long root = accNum;
while(root > 9){
sum = 0;
for(long i = root; i > 0; i /= 10){
sum += i % 10;
}
root = sum;
}
System.out.print("Digital Root: " + root);
if(root % 2 == 0)
System.out.println(" (Root Even)");
else
System.out.println(" (Root Odd)");
}
}