Output of loop seems inaccurate

Just a basic question as I’m only a student who got curious. Shouldn’t the output of:

for (int num = 0; num < 5; num++) {
System.out.print(num + 1);
}

be "13"?

The output shows "12345" instead.

>Solution :

Your function is not actually summing, its asking for a print out of each sequential "num" with 1 added to it. If you wanted to actually sum, you’d change your code to the following:

int sum = 0;
for (int num = 0; num < 5; num++) {
sum = sum + (num + 1);
}
System.out.print(sum)

Leave a Reply