Advertisements
Advertisements
Question
Write a program to input a number and display it into its Binary equivalent.
Sample Input: (21)10
Sample Output: (10101)2
Answer in Brief
Solution
import java.util.*;
public class Q10
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print(“Enter the decimal number: ");
long decimal = in.nextLong();
long binary = 0, m = 1, rem = 0;
while(decimal > 0) {
rem = decimal % 2;
binary = binary + (rem * m);
m *= 10;
decimal /= 2;
}
System.out.printIn(“Equivalent binary number: " + binary);
}
}
shaalaa.com
Is there an error in this question or solution?