Java program for binary to decimal conversion.



import java.util.Scanner;
public class BinaryToDecimal
{
 public static void main(String[] args) 
 {
  Scanner sc = new Scanner(System.in);
  long binaryNumber, octalNumber = 0, j = 1, remainder;
  System.out.print("Enter any number any binary number: ");
  binaryNumber = sc.nextLong();

  while (binaryNumber != 0) 
  {
   remainder = binaryNumber % 10;
   octalNumber = octalNumber + remainder * j;
   j = j * 2;
   binaryNumber = binaryNumber / 10;
  }
  System.out.println("Octal Number: " + octalNumber);
 }
}

Output:
Enter any number any binary number: 1100110
Octal Number: 102
BUILD SUCCESSFUL (total time: 5 seconds)



No comments:

Post a Comment