Good come on, you’re making a loop with the variable int i
that in the first iteration of for
has the value of 1.
You also assumed that int soma
has a value of 0.
Your loop will rotate until int i
reach the value of 10, remembering that you started it as 1 for (i = 1;
your loop will rotate while int i
is less than or equal to 10 i <= 10;
We will now simulate your loop step by step.
First Iteration
sum = sum + i; Soma = 0 | int i = 1 | Resultado = 1
Second Iteration
sum = sum + i; Soma = 1 | int i = 2 | Resultado = 3
Third Iteration
sum = sum + i; Soma = 3 | int i = 3 | Resultado = 6
Fourth Iteration
sum = sum + i; Soma = 6 | int i = 4 | Resultado = 10
Fifth Iteration
sum = sum + i; Soma = 10 | int i = 5 | Resultado = 15
Sixth Iteration
sum = sum + i; Soma = 15 | int i = 6 | Resultado = 21
Seventh Iteration
sum = sum + i; Soma = 21 | int i = 7 | Resultado = 28
Eighth Iteration
sum = sum + i; Soma = 28 | int i = 8 | Resultado = 36
Nona Iteração
sum = sum + i; Soma = 36 | int i = 9 | Resultado = 45
Tenth Iteration
sum = sum + i; Soma = 45 | int i = 10 | Resultado = 55
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, soma = 0;
for (i = 1; i <= 10; i++)
{
soma = soma + i;
}
printf("Soma %d\n", soma);
getchar();
return 0;
}
You are accumulating the counter in the variable
soma
.– ptkato