In: Computer Science
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
public class Main
{
//method to convert decimal into binary
public static int decimalToBinary(int decimalnum)
{
int binarynum = 0, rem, temp = 1;
while (decimalnum!=0)
{
rem = decimalnum %2;
decimalnum = decimalnum / 2;
binarynum = binarynum + rem*temp;
temp = temp * 10;
}
//return number
return binarynum;
}
public static void main(String[] args)
{
//2d array declaration
int[][] mulTable = new int[10][10];
int r = 1;
int c = 1;
//fill the binary array
for(int a = 0; a < mulTable.length; a++)
{
for(int b = 0; b < mulTable[a].length; b++)
{
int num = r * c;
mulTable[a][b] = decimalToBinary(num);
c++;
}
r++;
c = 1;
}
//display the binary array
for(int a = 0; a < mulTable.length; a++)
{
System.out.printf("%4d:", mulTable[a][0]);
for(int b = 0; b < mulTable[a].length; b++)
{
System.out.printf("%8d", mulTable[a][b]);
}
System.out.print("\n");
}
}
}
OUTPUT: