How to sum all the numbers in the while sequence?

1

How to add from 1 to 64 in while ?

I wanted to make the sum 1+2+3+4+...+64 print the value of the sum by 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;


}
    
asked by anonymous 15.02.2018 / 16:43

3 answers

9

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

#include <stdio.h>

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

The ++ is incrementing the variable numero at each step of the loop, it adds 1 in the variable itself, the same as numero = numero + 1 . And += is adding the result of the expression to the right in the variable resultado , it adds in the variable itself, the same as resultado = resultado + numero . Note that the increment of numero only occurs later because it 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);
    
15.02.2018 / 17:00
1

How about designing the loop, but not at the 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 this sum S .

What happens if we take this list and add it back?

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 is worth S . Since it is a finite sum, reversing the elements will not change the final result, so the reverse list is also worth 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

Because n is an integer, n*(n+1) is necessarily an even number. Therefore, dividing by 2 will result in an integer. To ensure the value, it costs nothing to force the multiplication precedence prior to splitting:

( n*(n+1) )/2

So, if you want to know the value for the sum up to 64, you 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;
}
    
18.02.2018 / 03:42
-1

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

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

    while(numero < 5) {
        numero++;
        soma = soma + numero;
        printf("%d\n", soma);
    }
}
    
15.02.2018 / 17:11