Calculation of the multiple of 10 greater than or equal to a value

Asked

Viewed 3,760 times

4

use module 10; Example:

7  4  8  9  3  1  0  7  2 <== Dados
x  x  x  x  x  x  x  x  x
2  1  2  1  2  1  2  1  2 <== Peso
=  =  =  =  =  =  =  =  =
14 4  16 9  6  1  0  7  4 <= Resultado
1+4=5 1+6=7
//resultado da multiplicação, cujo valor for maior que 10 (dez), os dígitos do resultado devem ser
//somados, resultando um valor menor que 10 (dez);

5 4 7 9 6 1 0 7 4 = 43 <== Resultado Final
//Somatório do resultado das multiplicações = 43

So far so good. Now comes my problem. How to find Multiple of 10 greater or equal to the sum of 43?

My current code:

for (int i = 0; i <= soma; i += 10) {
    if ((i % 10) == 0) {
       if (i >= soma) {
          Multiplo = i;
       }
    }
}
  • 2

    I do not understand what is the expected result.

  • 1

    The figure you expect to find is 50 ? I didn’t quite understand what you asked.

  • That’s right, I need to find, 50 see my Cód. for (int i = 0; i <= sum; i += 10) { if ((i % 10) == 0) { if (i >= sum) { Multiple = i; } } }

  • 1

    @Manufacturers Monealanamendes Whenever you have a pole code to help people understand the problem and how far you’ve come.

  • It is not necessary to check whether i mod 10 is equal to 0, because you always increment the variable with 10.

2 answers

5


To find the nearest 10 multiples, you can make use of the following pseudo-code:

int multiplo;
if(quociente % 10 != 0)
{
  int quociente = soma / 10;
  multiplo = (quociente+1)*10;
} else
{
  multiplo = soma;
}

The first variable will take only the entire portion of the division quotient by 10, and then the nearest multiplier is the successor of the quotient multiplied by 10.

I avoided declaring them in the same line to avoid any problem with simultaneous declaration of variables, since one depends on the other, if the dependency does not exist, error occurs.

  • Well I’d come up with a POG, kkk But this Cód of yours is much leaner. Thanks, it worked out here. Right now I have to calculate the 2nd and 3rd field of the digital line. Af

  • Haha, no problem at all.

  • @mutlei, it seems to me that this response fails when the sum is already multiplied by 10.

  • @Jjoao truth... Correcting.

4

I know it’s old, but there’s a simpler way....

if(soma%10==0){
    multiplo = soma;
}else{
    multiplo = (soma-(soma%10))+10 
}
  • 1

    Why the downvote?

  • It was just to register another calculation alternative.

  • 1

    @Darioteodoro, your answer is completely valid. The comment of the extension was directed to the user who voted negatively in your reply.

  • @ramaral I do not know the reason, but I suppose who negative was because only presented the code, but did not explain the calculation, out if they look well the variables had (I edited) accents.

  • 1

    @Guilhermenascimento, maybe that’s why. I don’t think that’s reason enough, the code is easy to understand. In addition it must be taken into account that the AR is new around here.

  • @And that there is no problem in creating variables with accent.

  • @jbueno I’m not used to c#, I just commented this because I imagine that in most languages the string to create a variable is [a-z][a-z0-9_]

Show 2 more comments

Browser other questions tagged

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