-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckDigit.java
More file actions
36 lines (30 loc) · 1.29 KB
/
CheckDigit.java
File metadata and controls
36 lines (30 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.Scanner;
/*
* Aks the user to input 4 digit code.
* If the sum of 4 numbers is 9 or less, the total of those numbers is outputed as check digit.
* If the sum is 10 or more, then the remainder of devision of sum by 10 is outputed.
*/
public class CheckDigit{
public static void main(String[] args){
try{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first digit of code: ");
int digit1 = Integer.parseInt(scanner.nextLine());
System.out.print("Enter second digit of code: ");
int digit2 = Integer.parseInt(scanner.nextLine());
System.out.print("Enter third digit of code: ");
int digit3 = Integer.parseInt(scanner.nextLine());
System.out.print("Enter fouth digit of code: ");
int digit4 = Integer.parseInt(scanner.nextLine());
scanner.close();
int total = digit1 + digit2 + digit3 + digit4;
if(total <= 9){
System.out.println("Check digit: " + total);
}else{
System.out.println("Check digit: " + total % 10);
}
}catch(NumberFormatException exeption){
System.out.println("Digit should be a number!");
}
}
}