0
Good afternoon guys, all right?
I’m continuing my C exercises, and I got stuck in one more. Here is the statement of the same:
Write an algorithm that takes a keyboard number and tells you if it is divisible by 8, by 5, by 2 or if it is not divisible by any of these. Check for each of the numbers. Example: Number: 8 Divisible by 8 Divisible by 2
My code:
inicio
escreva("Digite um número: ")
leia(num)
d2 <- (num%2)
d5 <- (num%5)
d8 <- (num%8)
soma <- d2+d5+d8
se (soma = 0) entao
escreval("Número não é divisível por nenhuma das opções (2, 4 e 5)")
senao
se (d2 = 0) entao
escreval("Número divisível por 2")
senao
se (d5 = 0) entao
escreval("Número divisível por 5")
senao
se (d8 = 0) entao
escreval("Número divisível por 8")
fimse
fimse
fimse
fimse
fimalgoritmo
Only, it only if I type "8" for example, it says it is divisible only by 2. And when I type 40, it said it is not divisible by any of the options.
Can someone help me with this?
So, about the first
se
I must usese (soma <> 0) entao
? And in relation to the interweaving ofse/senao
I shouldn’t use thesenao
and yes thefimse
according to each repeat opening?– tspereirarj
I made the changes, and really now he’s telling it right by which the one is divisible, straight. However, he’s printing the
se (soma<>0) entao
in all cases.– tspereirarj
I returned the first
se
for what was first:se (soma=0) entao
and it is running correctly. The only one however is the number 80, which is printed ALL messages fromse
.– tspereirarj
@Tspereirarj it rotates all messages from
se, porque todos dividem o número, tem várias formas de resolver, a ideal seria ir "juntando" a string conforme fosse tendo as divisões. Por exemplo, entrou no senão (é divisível por algum número), faz o seguinte:
res <- "this number is divisible by: "e ir concantenando por quem é divisível nessa variável res e dar um
` in it at the end.– Felipe Avelar