Converse between Decimals and Binaries in a vector, with randomly generated elements

Asked

Viewed 412 times

0

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 will need to be stored in a vector.

I just can’t make a loop doing this.

I managed to assemble at most this algorithm that converts a single number, but otherwise I’m not getting it, neither I nor my colleagues who tried to help me.

It is also worth saying that I had done using the method toBinaryString, but this is part of a list of exercises, and our teacher wants us to do the hand, without using too many resources.

Here’s the code I made, however, I can’t fit it in 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]);
}
  • Can we use functions/methods to solve this? Or do you need to solve in main?

  • It needs to be in the main, because as the teacher did not teach methods, he thinks that we do not know and, therefore, we can not use.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.