Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. public static void main(String[] args){
  2. int n;
  3. int temp;
  4. int n0,n1,n2,n3,n4,n5,n6;
  5. String binario;
  6. Scanner tastiera = new Scanner(System.in);
  7.  
  8. System.out.println("Programma che converte numero in decimale in base binaria.");
  9. System.out.print("Inserisci un numero: ");
  10. n = tastiera.nextInt();
  11. temp = n;
  12. if(n<0||n>127){
  13. System.out.println("Errore: il numero deve essere compreso tra 0 e 127!");
  14. System.exit(0);
  15. }
  16.  
  17. n0 = n%2;
  18. n/=2;
  19. n1 = n%2;
  20. n/=2;
  21. n2 = n%2;
  22. n/=2;
  23. n3 = n%2;
  24. n/=2;
  25. n4 = n%2;
  26. n/=2;
  27. n5 = n%2;
  28. n/=2;
  29. n6 = n%2;
  30. n/=2;
  31.  
  32. binario = ""+n6+n5+n4+n3+n2+n1+n0;
  33. System.out.println("La rappresentazione binaria di "+temp+" e': "+binario);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement