Converting Indo-Arabic numbers to Java Roman numbers

Asked

Viewed 113 times

0

Good afternoon, I am with a certain difficulty in programming a converter for Roman numbers, I managed to separate the houses in thousand, hundred, ten and unit as follows

 if(numericos==1000){

      while(numericos<=milhar){
      milhar = 1;

      }
     v[0]=milhar;
  }

  if(numericos>100){

      while(centenas<=numericos){
      centenas = centenas + 100;

      }
  centenas= centenas/100-1;
  numericos= numericos%100;
  v[1]=centenas;
  } 


  if(numericos>10){

      while(dezenas<=numericos){
      dezenas = dezenas + 10;
      numericos=numericos%10;
      }
     dezenas = dezenas/10-1;
     v[2]=dezenas;
  }
  if(numericos>1){

      while(unidades<=numericos){
      unidades = unidades + 1;
      }
       unidades = unidades - 1;
       v[3]=unidades;
  }

but I can’t think of a conversion logic without putting trocentos ifs in the code Can someone please help me?

  • 1

    Here has several different algorithms :-)

1 answer

-1

I usually use these contents a lot to separate the units, tens, hundreds, etc.

    int inteiro = 1239;
    int unidade = inteiro % 1000 % 100 % 10;
    int dezena = inteiro % 1000 % 100 / 10;
    int centena = inteiro % 1000 / 100;
    int milhar = inteiro / 1000;

    System.out.println(unidade);
    System.out.println(dezena);
    System.out.println(centena);
    System.out.println(milhar);

    //saída:
    //9
    //3
    //2
    //1

Browser other questions tagged

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