Write a Java program to convert a binary number to decimal number


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2. public class Javaexcercise {
  3.  
  4. public static void main(String[] args)
  5. {
  6. Scanner sc = new Scanner(System.in);
  7. long binaryNumber, decimalNumber = 0, j = 1, rem;
  8. System.out.print("Enter a binary number: ");
  9. binaryNumber = sc.nextLong();
  10.  
  11. while (binaryNumber != 0)
  12. {
  13. rem = binaryNumber % 10;
  14. decimalNumber = decimalNumber + rem * j;
  15. j = j * 2;
  16. binaryNumber = binaryNumber / 10;
  17. }
  18. System.out.println("Decimal Number: " + decimalNumber);
  19. }
  20. }

Result

java techstudy.org

     





Related Topics




Popular Posts