Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading