Visualg - Prog for limits

Asked

Viewed 941 times

1

I am doing the program of the following exercise:

Create an algorithm that reads the lower and upper bounds of a range and print all odd numbers in the open range and its sum (USE THE FOR). Suppose the entered data is for a decreasing interval.

My question is, how do I make him not display the numbers typed in? Why should I print the range opened, and in this, it should not display the numbers typed.

algoritmo "APS08"
var
   inf, sup, soma:inteiro
   i:inteiro
inicio

      escreva("Digite o limite inferior: ")
      leia(inf)
      escreva("Digite o limite superior: ")
      leia(sup)

      para i de inf ate sup faca passo -1
           se (i%2 = 1) entao
              escreva(i)

              soma <- soma + i
           fimse
      fimpara

      escreval("")
      escreval("Soma: ",soma)

fimalgoritmo

Can someone explain to me how I do?

[RESOLUTION]

algoritmo "APS08"
var
   inf, sup, soma:inteiro
   i:inteiro
inicio

      escreva("Digite o limite inferior: ")
      leia(inf)
      escreva("Digite o limite superior: ")
      leia(sup)

      para i de sup-1 ate inf-1 passo -1 faca
           se (i%2 = 1) entao
              escreva(i)

              soma <- soma + i
           fimse
      fimpara

      escreval("")
      escreval("Soma: ",soma)

fimalgoritmo
  • 2

    how so "do not display typed numbers"? wants to hide the command leia? How will you know what is being typed? If you want to take it off the screen later, just use the command limpatela

  • No friend. Suppose the lower limit is 5 and the upper limit is 10. It will display all odd ones from 5 to 10 (5,7,9,11,13,15,17,19,21,23,25). The 5 and 25 should not appear. Do you understand? And should still be showing in descending order.

  • 1

    I get it, you can treat it on para, do not start from inf but inf+1 and the same idea in ate, and if you want to reverse the inveta order also where the para

  • I made the change, it was para i de inf+1 ate sup-1 passo -1 and solved the problem. As for the descending order, nothing. When I reversed the order, it stopped displaying.

  • 1

    If you reverse the order, you also need to reverse the step: para i de sup-1 ate inf+1 passo 1

  • I already found the problem. That good old "error between the chair and the monitor". I appreciate your help, and I will edit the post with the resolution.

  • 2

    you can post the solution as the answer which is most correct

  • Okay. In the next posts, I’ll do it. Thank you :)

Show 3 more comments

1 answer

0

The inference limit is incorrect. See a test:

inserir a descrição da imagem aqui

How is a open interval, only values between 2 and 9 (inclusive) should be considered. 1 should not enter.

To correct, just change from "-" to "+" at the bottom. Example:

Of:

para i de sup-1 ate inf-1 passo -1 faca

To:

para i de sup-1 ate inf+1 passo -1 faca

Follows another form:

algoritmo "APS08"
var
   inferior, superior, somatorio: inteiro
   ultimo, primeiro:  inteiro
   i:inteiro
inicio
   escreva("Digite o limite inferior: ")
   leia(inferior)
   escreva("Digite o limite superior: ")
   leia(superior)

   //encontrar o último ímpar da sequência
   se (superior % 2 = 0) entao //se par
      ultimo <- superior - 1
   senao
      ultimo <- superior - 2
   fimSe

   //encontrar o primeiro ímpar da sequência
   se (inferior % 2 = 0) entao //se par
      primeiro <- inferior + 1
   senao
      primeiro <- inferior + 2
   fimSe

   escreval()
   escreva ("Intervalo aberto:")
   para i de ultimo ate primeiro passo -2 faca //os ímpares alternam de 2 em 2
      escreva (i)
      somatorio <- somatorio + i
   fimpara

   escreval("")
   escreval("Somatório: ", somatorio:1)
fimAlgoritmo

inserir a descrição da imagem aqui

Browser other questions tagged

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