Well come on, you're looping with the variable int i
which 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 run until int i
reaches the value of 10, remembering that you started it as 1 for (i = 1;
your loop will run while int i
is less than or equal to 10 i <= 10;
Let's 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
Ninth Iteration
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;
}