Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args){
- int n;
- int temp;
- int n0,n1,n2,n3,n4,n5,n6;
- String binario;
- Scanner tastiera = new Scanner(System.in);
- System.out.println("Programma che converte numero in decimale in base binaria.");
- System.out.print("Inserisci un numero: ");
- n = tastiera.nextInt();
- temp = n;
- if(n<0||n>127){
- System.out.println("Errore: il numero deve essere compreso tra 0 e 127!");
- System.exit(0);
- }
- n0 = n%2;
- n/=2;
- n1 = n%2;
- n/=2;
- n2 = n%2;
- n/=2;
- n3 = n%2;
- n/=2;
- n4 = n%2;
- n/=2;
- n5 = n%2;
- n/=2;
- n6 = n%2;
- n/=2;
- binario = ""+n6+n5+n4+n3+n2+n1+n0;
- System.out.println("La rappresentazione binaria di "+temp+" e': "+binario);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement