Sum of whole numbers from 1 to 100

Asked

Viewed 13,025 times

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 see numero and index could be the same variable

  • 1

    How about this? escreva("A soma dos inteiros de 1 à 100 é: 5050")

  • 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

3 answers

4


There is too much variable and some things that don’t make sense. It’s simple, write the number, add it and increment to go to the next.

Var
    soma, index : inteiro
Inicio
    index <- 1
    soma <- 0
    enquanto (index < 101) faca
        escreva (index, " + ")
        soma <- soma + index
        index <- index + 1
    fimenquanto
    escreva ("A soma dos inteiros de 1 à 100 é: ", soma)
Fimalgoritmo

I put in the Github for future reference.

1

Thanks! I had just got it when your msg arrived. I will leave below as my resolution and sorry for the trivial question.

Var
   numero, soma: inteiro
Inicio
      numero <- 0
      soma <- 0
      enquanto (numero < 100) faca
               numero <- numero + 1
               soma <- soma + numero
               se (numero = 100) entao
                  escreval (numero)
               senao
                    escreva (numero, " + ")
               fimse
      fimenquanto
      escreva ("A soma dos inteiros de 1 à 100 é", soma)
Fimalgoritmo

0

algoritmo "somados100numerosinteiros"
var soma,cont, index:inteiro
inicio
soma <- 0
cont <- 1

escreva (cont, " + ")
enquanto (cont<101) faca
soma <- soma + cont
cont <- cont + 1
index <- index + 1
escreva (cont, " + ")
fimenquanto
escreval ("0 = ",soma)
fimalgoritmo

Browser other questions tagged

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