1import java.util.*;
2import java.util.Scanner;
3public class IntegerToBinary
4{
5 public static void main(String[] args)
6 {
7 int num;
8 String str = "";
9 Scanner sc = new Scanner(System.in);
10 System.out.println("Please enter the a number : ");
11 num = sc.nextInt();
12 // convert int to binary java
13 while(num > 0)
14 {
15 int y = num % 2;
16 str = y + str;
17 num = num / 2;
18 }
19 System.out.println("The binary conversion is : " + str);
20 sc.close();
21 }
22}
1public static String makeBinaryString(int n) {
2 StringBuilder sb = new StringBuilder();
3 while (n > 0) {
4 sb.append(n % 2);
5 n /= 2;
6 }
7 sb.reverse();
8 return sb.toString();
9}
1public class DecimalToBinaryExample2{
2public static void toBinary(int decimal){
3 int binary[] = new int[40];
4 int index = 0;
5 while(decimal > 0){
6 binary[index++] = decimal%2;
7 decimal = decimal/2;
8 }
9 for(int i = index-1;i >= 0;i--){
10 System.out.print(binary[i]);
11 }
12System.out.println();//new line
13}
14public static void main(String args[]){
15System.out.println("Decimal of 10 is: ");
16toBinary(10);
17System.out.println("Decimal of 21 is: ");
18toBinary(21);
19System.out.println("Decimal of 31 is: ");
20toBinary(31);
21}}
1Integer.toString(100,8) // prints 144 --octal representation
2
3Integer.toString(100,2) // prints 1100100 --binary representation
4
5Integer.toString(100,16) //prints 64 --Hex representation