Loop repetition in programming logic algorithm

Asked

Viewed 531 times

0

I have doubts about this:

Write an algorithm that asks for the age of several people (USE REPEAT). Please report the total number of people under 25 and the total number of people over 50. The program ends when age is negative (Not to be used in counting).

I got this far but I stopped because I don’t know how to use it very well repeat.

Algoritmo "semnome"

var
    idade, cont, f1, f2: inteiro
    outro: caracter
    inicio
    cont <- 0
    f1 <- 0
    f2 <- 0

repita
    escreva("Informe a idade: ")
    leia(idade)
    cont <- cont + 1

    se (idade <= 25) entao
        f1 <- f1 + 1
    senao
        se ((idade >= 26) e (idade <= 51)) entao
            f2 <- f2 + 1
        fimse
    fimse

    escreval("Outra pessoa [S/N]: ")
    leia(outro)
ate (outro = "N")

escreval("Total de pessoas informadas: ", cont)
escreval("Faixa 1: ", f1)
escreval("Faixa 2: ", f2)

could help me?

  • This is Portugol?

  • Please insert the programming language.

1 answer

1

You can use the command interrompa to repeat after reading the data:

Follows the code:

se (idade < 0) entao
   interrompa
fimse

Following illustration: inserir a descrição da imagem aqui

see your code running perfectly: inserir a descrição da imagem aqui

Entire code used:

var
idade, cont, f1, f2: inteiro
outro: caracter

inicio
// Seção de Comandos 
cont <- 0
f1 <- 0
f2 <- 0

repita

escreva("Informe a idade: ")
leia(idade)

se (idade < 0) entao
   interrompa
fimse

cont <- cont + 1

se (idade < 25) entao
   f1 <- f1 + 1
senao se (idade > 50) entao
      f2 <- f2 + 1
fimse

escreval("Outra pessoa [S/N]: ")
leia(outro)

ate (outro = "N")

escreval("Total de pessoas informadas: ", cont)
escreval("Faixa 1: ", f1)
escreval("Faixa 2: ", f2)

fimalgoritmo
  • Truth even the negative thing. I misinterpreted the question. Thanks!

  • You’re only wrong in your response to <= 25and the senao se ((idade >= 26) e (idade <= 51)) entao

  • Normal! after copying his code fimse(because I had 2 without need), I went and it worked, then I went straight to stop and I didn’t pay attention to <= his.

  • "Please report the total number of people under 25 years of age": idade < 25... "and the total number of people over 50": idade > 50

  • 1

    Some people have trouble translating text into code, and that shouldn’t happen to programmers because they work with just exact things. When you say "less than 25", the 25 is not included, goes only up to the 24.

  • you are right... correction made!

  • It was only necessary to correct the < 26

Show 2 more comments

Browser other questions tagged

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