How to add all the numbers in the sequence in while?

Asked

Viewed 13,060 times

1

How to add 1 to 64 in while?

I wanted to add up 1+2+3+4+...+64 print the sum value when arriving at the end.

What I’ve tried so far:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int numero= 1;
    //int resultado;

   while(numero <= 64) {

    printf("%d\n", numero);
    numero++;

    }
return 0;


}
  • 1

    This is what you need soma += numero; (inside the loop)?

  • @cat would be yes. See Maniero’s answer

  • vlw by the help manow

  • @nazinhopb 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 done so. 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 (when you have enough score).

3 answers

10

If you want to add it up you should put it in the code. So you need a variable, obviously starting from 0, that takes the sum, and you need to add the new number to each step (the increment in each step you’ve already done), I imagine the impression should only occur at the end so it should stay out of the loop, something like that:

#include <stdio.h>

int main(void) {
    int numero = 1;
    int resultado = 0;
    while (numero < 65) resultado += numero++;
    printf("%d\n", resultado);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The ++ is increasing the variable numero at each step of the loop, it adds 1 to the variable itself, the same as numero = numero + 1. And the += is adding the result of the expression to the right in the variable resultado, sum in the variable itself, the same as resultado = resultado + numero. Note that the increment of numero only occurs after because he is a post-increment.

You can do with for, but I think it’s worse:

    int resultado = 0;
    for (int numero = 1; numero < 65; numero++) resultado += numero;
    printf("%d\n", resultado);
  • 1

    Nice. Simple and functional. I advise to explain the operation of the increment "number++". In this situation can get confusing for the OP.

  • Right now the answer calls for the use of the while cycle, but I’m curious why do you think the use of the cycle is worse in this case? The code became more cohesive, and the use of the variable numero is "exclusive" to the cycle. Due to the internal operation of the?

  • It’s really taste. Actually ideally in cases like this it would be nice to have the foreach

  • @Maniero in this case the ideal is to have no loop. I am elaborating the answer by "embedding" the loop within a formula o(1)

  • 1

    @Jeffersonquesado I think, but I didn’t want to give a different solution because I think the exercise is just the loop. Just do 32.5 * 64 which is (64 + 1) / 2 * 64.

  • how do I display the index of the last element?

  • 1

    Which index? Which element? There is none of this in this code.

Show 2 more comments

2

How about including at design level the loop, but not at code level?

Well, your question is clearly mathematical. As such, it could be solved mathematically.

You have the numbers 1, 2, 3..., n, for a n any. Be the value of that sum S.

What happens if we take this list and add it to the reverse of it?

1     +  2    +  3    + ... + (n-1) + n
n     + (n-1) + (n-2) + ... + 2     + 1
-------------------------------------------
(n+1) + (n+1) + (n+1) + ... + (n+1) + (n+1)

The first list goes S. Since it is a finite sum, reversing the elements will not alter the final result, so the reverse list is also valid S. This means that the sum of the two lists is 2S. Note that the term (n+1) is repeated n times, therefore:

2S = n*(n+1)

Hence:

S = n*(n+1)/2

Like n is a whole, n*(n+1) necessarily is an even number. Therefore dividing by 2 will result in an integer. To guarantee the value, it costs nothing to force the multiplication precedence before the division:

( n*(n+1) )/2

So, as one wishes to know the value for the sum up to 64, could do (64*65)/2 or leave it in the variable, which would be easier to change to another value in the future:

#include <stdio.h>

int main()
{
    int n = 64;
    printf("%d\n", ( n*(n+1) )/2);

    return 0;
}
  • 1

    +1 for the well-crafted answer. (by chance, the purely mathematical solution had been mentioned here formerly).

  • @Bacco yes, indeed. I always think it important to mention that even "naturally iterative" problems may have noniterative algorithms

  • Yes, a great addition and math class.

-1

You are printing all numbers with this program. You need to create a variable (in my case, "sum") that joins all numbers.

#include <stdio.h>
int main(){
    int numero = 0;
    int soma = 0;

    while(numero < 5) {
        numero++;
        soma = soma + numero;
        printf("%d\n", soma);
    }
}
  • 2

    What your answer adds to Maniero’s?

  • Now that I have noticed, I add nothing. I can delete the answer if you wish

Browser other questions tagged

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