Algorithm to calculate the sum of the numbers typed in portugol

Posted on

Question :

I’m using Portugol Studio to study algorithms in English. And I have a difficulty in an issue that the teacher has passed.

The question is as follows:

  

Create an algorithm that prompts the user to enter 10 numbers
  any integers. Print the sum of the numbers you typed.

My algorithm looks like this:

programa
{
    inteiro cont=0, numero, soma=0
    funcao inicio()
    {
        enquanto (cont<3)
        {
            cont++
            escreva ("Digite um número inteiro: ")
            leia (numero)
            limpa()
        }
        soma = soma+numero
        escreva ("nA soma é: ", soma)
    }
}

But it does not return the exact sum.

    

Answer :

the part

soma = soma+numero

Must be inside the loop while {}

Looking like this:

programa
{
inteiro cont=0, numero, soma=0
    funcao inicio()
    {
enquanto (cont<3)
{
cont++
escreva ("Digite um número inteiro: ")
leia (numero)
soma = soma+numero//aqui
limpa()
}

escreva ("nA soma é: ", soma)
    }
}

So each time he asks for the number he sums it up in the variable sum and only after he finishes the 2 laps in the while it shows the value of sum.

    

Only the last number entered is being summed.

soma = soma+numero must be inside the block enquanto , this way whenever the user enters a number, this number will be added with the value existing in soma .

programa
{
    inteiro cont=0, numero, soma=0
    funcao inicio()
    {
        enquanto (cont<3)
        {
            cont++
            escreva ("Digite um número inteiro: ")
            leia (numero)
            limpa()                
            soma = soma + numero
        }

        escreva ("nA soma é: ", soma)
    }
}

    

Leave a Reply

Your email address will not be published. Required fields are marked *