A simple Java source code to convert Decimal to Binary

This example will show how to convert Decimal Number to Binary Number programmatically using Java. Note: this source code uses a step by step way (unorthodox) of converting it without using any library or methods 🙂

/*  From Techie.Click
    By JP  */
import java.util.Scanner;
public class Decimal2binary {
    public static void main(String[] args) {
        int decnum, rem, num;
        String bin="";
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a decimal number (Positive): ");
        decnum=in.nextInt();
    	num = decnum;
    	System.out.print("Binary form is: ");
    	while(num > 1) {
    		rem = num % 2;
    		num = num / 2;
    		bin = rem + bin;
    	}
    	bin = num + bin;
    	System.out.print(bin);
    }
}

A simple Java source code to convert Decimal to Binary

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.