Tuesday, 13 September 2016

Java Program to Convert Decimal to Binary?

Below example shows how to convert Decimal number into binary ,as we Know that for converting decimal to binary we divide the number by 2 untill the quotient become zero or one ,and then we reverse the remainder .

public class DecimalToBinary {

public static void getBinary(int num){
int local = num;
int[] arr = new int[num];
int place=0;

while(num > 0){
int temp = num % 2;
arr[place++] = temp;
num = num / 2;
}
System.out.print("BINARY OF "+local+" : ");
for(int j = place-1 ;j >= 0 ;j--){
System.out.print(arr[j]);
}

}
public static void main(String[] args) {
DecimalToBinary.getBinary(112);
}

}

Program Output :-

BINARY OF 112 : 1110000



No comments:

Post a Comment