Algorithm to calculate the sum of the numbers typed in portugol

Asked

Viewed 13,835 times

3

I’m using Portugol Studio to study algorithms in Portuguese. And I’m having a hard time with a question that the teacher passed.

the question is as follows:

Develop an algorithm that requires the user to enter 10 numbers any integers. Print the sum of the typed numbers.

My algorithm is 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 I don’t get the exact sum back.

  • ---------- Boy! Thanks guys! I was breaking my head here with this sum... Something so simple... Nor did I realize that the variable had to stay inside the loop! So she keeps adding the values... Vlw personal!

  • I just don’t understand why its loop goes from 0 to 2 if the exercise asks to elaborate an algorithm where the user enters with 10 integers.

  • It’s because I reduced the size to get the result... so I don’t have to take the test with the 10 values! Actually the teacher asked with 100 values! then I decrease to 10 and consequently to 2 hahaha

2 answers

3


the part

soma = soma+numero

Must be inside the loop while{}

Thus remaining:

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 every time he asks for the number he makes the sum of it in the variable sum and only after finishing the 2 rounds on while he shows the sum value.

2

Only the sum of the last number typed is being made.

soma = soma+numero needs to be inside the block enquanto, this way whenever the user enters a number, this number will be summed with the existing value 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)
    }
}

Browser other questions tagged

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