Monday 12 April 2010

Convert "Decimal" Numbers into "Binary" Numbers.

import java.util.Scanner;

public class DecimalToBinary {

public static void main(String[] args) {

System.out.println("Enter a decimal number: ");
Scanner scanStr = new Scanner(System.in);

String num = null;

//Check for an integer number entered by the user
if(scanStr.hasNextInt())
{
//Convert the decimal number to binary String
num = Integer.toBinaryString(scanStr.nextInt());
}

System.out.println("The decimal number in binary is: "+num);
}
}

No comments:

Post a Comment