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

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 do you find Multiple of 10 greater than 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;
       }
    }
}
    
asked by anonymous 24.02.2015 / 14:29

2 answers

5

To find the multiple of 10 closest, you can use 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 integer part of the division quotient by 10, and then the nearest multiplier is the successor to the quotient multiplied by 10.

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

    
24.02.2015 / 14:50
2

I know it's old but it has a simpler way ...

if(soma%10==0){
    multiplo = soma;
}else{
    multiplo = (soma-(soma%10))+10 
}
    
03.12.2016 / 16:41