Read an even number

0

I'm once again in doubt, now the doubt is related to exercise over Desvio de condicional Simples using only the command IF

  

After reading an integer value, compute and display your square only if the read value is even. Otherwise, the program should be terminated without displaying anything.

I am in doubt as to what my IF condition would indicate that the calculation will only be performed if the number entered by the user is even!

I declare three variables

  • The number to be typed
  • The variable that will present the calculation (in this case the number squared)
  • The variable to indicate that the value is even!
  • And my second question is: Should I declare a variable to indicate that the value must be par ... Or if I only indicate two variables!

        
    asked by anonymous 07.05.2018 / 22:27

    1 answer

    1

    You only need two variables.

    You can do this:

    int calculo = numero * numero;
    if (calculo % 2 == 0) System.out.println(calculo);
    // O operador % retorna o resto da divisão. 0 seria numero par, e 1 impar!
    // O if não precisa de estar com chaves por conter apenas uma instrução.
    

    And you can do even with one variable only!

    if ((numero * numero) % 2 == 0) System.out.println(numero * numero);
    
        
    07.05.2018 / 22:35