Decimal to Binary
Convert a decimal integer to its binary representation.
package DSA.Functions;
import java.util.Scanner;
public class decimaltoBinary { public static int dtb(int n) { int binary = 0; int pow = 0; while (n > 0) { int c = n % 2; binary = (int) (binary + c * Math.pow(10, pow)); pow++; n = n / 2; } return binary; }
public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { System.out.println("Enter the decimal number"); int a = sc.nextInt(); int b = dtb(a); System.out.println("The Binary is : " + b); } catch (Exception e) { System.out.println(e); } }}