Visualg algorithm: disregard number 0

Asked

Viewed 653 times

1

I’m solving the following exercise:

Write an algorithm that averages the numbers typed by the user, if they are even. Finish reading if the user type zero (0)

However, when I type 0 to finish, it is counting 0 as even number, and this dividing by a number more.

algoritmo "semnome"

var
n,np,m,nnp:real
inicio
repita
escreval ("entre com um numero")
leia (n)
se n%2=0 entao
np<-np+1
nnp<-nnp+n
fimse
ate n=0
m<- nnp/np
escreval ("a media dos numeros é",m)
fimalgoritmo

1 answer

4

Only add a criterion in the condition:

If the number not zero (is other than zero) and if the remainder of the division by 2 is equal to zero.

Just to leave a few hints: watch out for code formatting, indentation. This is important. Give really useful names to variables, nobody likes having to read the whole code to understand what the variable np means in scope and, believe me, you won’t like to do that when reading your old code either.

algoritmo "semnome"

var
numero, qtd, media, soma: real

inicio

repita
   escreval ("entre com um numero")
   leia (numero)
   se (numero diferente de 0) E (numero % 2 = 0) entao
      qtd <- qtd + 1
      soma <- soma + numero
   fimse
ate numero = 0

media <- soma/qtd

escreval ("a media dos numeros é", media)
fimalgoritmo

Browser other questions tagged

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