import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class DecimalToBinary { public static void main(String args[]) throws Exception, IOException { int a; int i = 0; int b[] = new int[10]; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter any Decimal value: "); a=Integer.parseInt(br.readLine()); while (a != 0) { i++; b[i] = a % 2; a = a / 2; } System.out.println("the binary value for the given decimal is: "); for (int j = i; j > 0; j--) { System.out.print(b[j]); } } }
OUTPUT:
C:\java>javac DecimalToBinary.java C:\java>java DecimalToBinary Enter any Decimal value: 12 the binary value for the given decimal is: 1100
In output C:\java is a folder name. Program Tested in Windows 7 Command Prompt and java version "1.7.0_03"