Cristiano Magro
2025-10-04 a70c2252becc1e607305909a2230257847c57455
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
public class Kary {
    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Errore: devi fornire due numeri interi come argomenti.");
            return;
        }
 
        int numero = Integer.parseInt(args[0]);
        int base = Integer.parseInt(args[1]);
 
        if (base >= 2 && base <= 16) {
 
            System.out.println("Converto " + numero + " in base " + base);
 
            // converto l'intero numero nella corrispondente base, assumo che numero sia in base 10.
 
            while (numero > 0) {
                int resto = numero % base;
 
                char cifra = (char) (resto < 10 ? '0' + resto : 'A' + (resto - 10));
 
                System.out.println(cifra);
 
                numero = numero / base;
            }
 
        } else {
            System.out.println("La base deve essere tra 2 e 16. Non valido: " + base);
        }
    }
}