Enter 2 numbers and find the sum of the intermediates

0

I need to enter 2 numbers and add the intermediate numbers that are multiples of 3 including the 2 numbers entered, but it does not work with all the numbers, the if condition comes into conflict when it starts or ends in a multiple of 3 and does not I get a correct result

int a;
int b;
int i;
int suma;

System.out.println("Ingrese el primer valor");
a=s.nextInt();
System.out.println("Ingrese el segundo valor");
b=s.nextInt();

suma=0;
i=a;

do{
    r=i%3;
    if(r==0){
        suma=suma+i;
    }
    i=i+1;
}while(i<=b);    

System.out.println("El total es "+suma);
    
asked by Kevin Velasco 26.10.2017 в 00:12
source

1 answer

0

I test your code with the cases:

  • First multiple number of 3, Second no:

    int num1 = 3;
    int num2 = 11;
    
  • I get 18 (3 + 6 + 9)

  • First number is not multiplo, Second if:

    int num1 = 1;
    int num2 = 9;
    
  • I get 18 (3 + 6 + 9)

  • The 2 numbers are multiples of 3:

    int num1 = 3;
    int num2 = 12;
    
  • I get 30 (3 + 6 + 9 +12)

  • The 2 numbers are not multiples:

    int num1 = 1;
    int num2 = 11;
    
  • I get 18 (3 + 6 + 9)

    It does not take into account any number if it is not a multiple of 3, so your code goes well.

      

    0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54 ...

        
    answered by 26.10.2017 / 05:58
    source