Java Program to convert an Integer into Binary
Java Program to convert an Integer into Binary
Integers
- Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed two’s complement integer. Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647. Its default value is 0.
Binary Numbers
- A binary number is a number expressed in the base-2 numeral. Binary consists of only 2 digits 0 and 1.
- Two operators are used in the conversion of integers to binary modulo and division to convert the given input into binary numbers.
Sample code
import java.util.Scanner;
public class integertobinary
{
public static void main(String[] args)
{
int n, i;
int[] a = new int[10];
System.out.println("Enter the number to convert : ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (i = 0; n > 0; i++)
{
a[i] = n % 2;
n = n / 2;
}
System.out.println("Binary of the given number = ");
for (i = i - 1; i >= 0; i--)
{
System.out.print(a[i]);
}
}
}
Output
Enter the number to convert : 123
Binary of the given number = 1111011