3
I am trying to create an algorithm that adds the numbers of the interval 1-100. In addition the program prints each element of the sum and at the end the result.
That is, 1 + 2 + 3 + 4 + 5 + 6...
Important: Using pseudo-language (English). But I am doing something wrong in my logic and the result does not correspond to reality.
What would be?
Var
// Seção de Declarações das variáveis
numero, soma, index : inteiro
Inicio
// Seção de Comandos, procedimento, funções, operadores, etc...
numero <- 1
index <- 2
escreva (numero, " + ")
soma <- numero + index
enquanto (index < 101) faca
numero <- numero + 1
index <- index + 1
escreva (numero, " + ")
soma <- soma + index
fimenquanto
escreva ("A soma dos inteiros de 1 à 100 é: ", soma)
Fimalgoritmo
As a response to the above code Visualg delivers:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 + 37 + 38 + 39 + 40 + 41 + 42 + 43 + 44 + 45 + 46 + 47 + 48 + 49 + 50 + 51 + 52 + 53 + 54 + 55 + 56 + 57 + 58 + 59 + 60 + 61 + 62 + 63 + 64 + 65 + 66 + 67 + 68 + 69 + 70 + 71 + 72 + 73 + 74 + 75 + 76 + 77 + 78 + 79 + 80 + 81 + 82 + 83 + 84 + 85 + 86 + 87 + 88 + 89 + 90 + 91 + 92 + 93 + 94 + 95 + 96 + 97 + 98 + 99 + 100 + A soma dos inteiros de 1 à 100 é: 5151
What is the result that is coming out? Because
index
is it 2? From what I seenumero
andindex
could be the same variable– Costamilam
How about this?
escreva("A soma dos inteiros de 1 à 100 é: 5050")
– Victor Stafusa
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero