package com.tutorial4u;
import java.util.Scanner;
public class DecimaltoBinary {
public static void main(String[] args) {
int decimal;
System.out.print("Enter any decimal number : ");
Scanner sc = new Scanner(System.in);
decimal = sc.nextInt();
System.out.println("Decimal "+decimal+" =
Binary " + decimalToBinaryConverter(decimal));
}
private static String decimalToBinaryConverter(int decimal) {
String binary = "";
if (decimal == 0) {
return "0";
}
while (decimal > 0) {
binary = (decimal % 2) + binary;
decimal = decimal / 2;
}
return binary;
}
}
Enter
any decimal number : 11
Decimal
11 = Binary 1011
No comments:
Post a Comment