I need to make a program where 10 randomly generated numbers that are stored in a vector are converted to binary and shown to the user. Not necessarily need to be stored in a vector.
I just can not do a loop doing this.
I was able to put together this algorithm that does the conversion of a single number, but otherwise I can not, neither I nor my colleagues who tried to help me.
It is also worth mentioning that I had done using the toBinaryString
method, but this is part of a list of exercises, and our teacher wants us to do it without using too many resources.
Here is the code I made, but I can not fit it into a for loop.
int decnum, aux, i=1, j, cont=0;
int binnum[] = new int[100];
Random any = new Random();
decnum = any.nextInt(10)+1;
aux = decnum;
while(aux != 0)
{
binnum[i++] = aux%2;
aux /=2;
}
System.out.print("O numero equivalente em binario de " + decnum + " é :\n");
for(j=i-1; j>0; j--)
{
System.out.print(binnum[j]);
}