Programming Shortcut

Write, Run and execute Your programs with simple and esay way by Programming Shortcut

Monday, February 4, 2019

Decimal to binary in java with explanation

Hello Friends! Welcome to Programming Shortcut only programming,
Today we'll discuss how to convert a decimal to binary in java. there are two methods to convert decimal to binary:

1. Decimal to binary in java with array using while loop,

2. Decimal to binary in java without array using while loop.


Decimal to binary in java


import java.util.Scanner;
public class DecimaltoBinary  {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int a[] = new int[1000];
        System.out.println("Enter a Decimal number to convert binary");
        int decimalnum = in.nextInt();
        int temp = decimalnum; //the taken number to save take temp variable.
        int n = 0;
        while (temp > 0) { // checking the number to till '0'
            int rem = temp % 2; // taking remainder of number
            temp = temp / 2; // after taking remainder update the number
            a[n] = rem;  // store the remainder into a array
            n++;
        }

//print the array in reverse order
        System.out.print("The binary of " + decimalnum + " is: ");
        for (int i = n - 1; i >= 0; i--) {
            System.out.print(a[i]);
        }
        System.out.println("\n");
    }
}

Output:
Enter a Decimal number to convert binary
23
The binary of 23 is: 10111


Decimal to binary in java without array


import java.util.Scanner;
public class DecimaltoBinary  {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter a Decimal number to convert binary");
        int num = input.nextInt();
        int n = num;
        String temp = "";
        while (n > 0) {
            int rem = n % 2;
            n = n / 2;
            temp = temp + rem;
        }
        String bin = new StringBuffer(temp).reverse().toString();

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

Output:
Enter a Decimal number to convert binary
24
The binary number of 24 is: 11000

Note: if adding in the String value store in the last of string value

 For example:
 String frist= "Programming";
 String last="Shortcut";
 String name= frist+last;
 System.out.println(name);

 output: ProgrammingShotcut

I make this programming and hope you are like the post if are you like the post you can please comment and Share the post to reach more people.
If any doubt about this java programming code or input & output please comment below
If you have another question about java programming send an email by contacting us form are given on the page right side.
I am Try to Help in your Programming Language by Programming Shortcut

Please share your experience about this post,
Thank You!

Programming Shortcut is collections in java best programs with example, best c programs with example and best c++ programs with example, best way to learn java, c and c++

About Programming Shortcut

This is a programming learning website that can help you learn and when your write your programming code. And all programming code is compiled run programming code in the following programming language C, c++, Java, php and Javascript Languages. find your programming read, understand the programming then copy and past and run your program and save your golden time to typing the program becuse you learn more in leas time. All Programming is codeing is ease to understand the programming