Forked from anonymous/gist:72c4a0ca325bea3d1850
Last active
January 21, 2016 17:37
-
-
Save baelter/f0117ba5a0e8c8a6cd2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Personnummer { | |
public static void main(String[] args) { | |
String pNr; //deklarerar personnr | |
System.out.print("Ange ditt personnummer (10 siffror): "); | |
Scanner kbd = new Scanner(System.in); //uppfattar vad som skrivs på tangentbord | |
pNr = kbd.nextLine(); | |
int sum; | |
for (int index=0; index <10; index++){ | |
int digit = Character.getNumericValue(pNr.charAt(index)); // Hämta siffran på plats "index" | |
int result; | |
if (index % 2 == 0){ // mod: är det ett jämt tal? | |
result = digit * 2; | |
if (result > 9) { | |
int a = result % 10; | |
int b = (result/10) % 10; | |
result = a+b; | |
} | |
} else { | |
result = digit * 1; | |
} | |
sum += result; | |
System.out.print("Resultat steg " + index + " : " + result); | |
} | |
System.out.print("Summa: " + sum); | |
if (sum % 10 == 0) { | |
System.out.print("Godkänd"); | |
} else { | |
System.out.print("Fejk"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment