Advertisement
nadezhdavh

ToDo4

Feb 19th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ToDo4 {
  4. public static void main(String[] args) {
  5. Scanner s = new Scanner(System.in);
  6. System.out.println("Enter binary number: ");
  7. int binary = s.nextInt();
  8. int decimal = 0;
  9. int power = 0;
  10. while (binary != 0) {
  11. decimal += ((binary % 10) * Math.pow(2, power));
  12. binary = binary / 10;
  13. power++;
  14. }
  15. System.out.println("Decimal number: " + decimal);
  16. /*
  17. String bin = "1010";
  18. int decimal2 = Integer.parseInt(bin, 2);
  19. System.out.println(decimal2);
  20. */
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement