Help simple c program with loop, not running through each integer.

Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to a simple C program that aims to calculate and print the cumulative sum of integers from 1 to 20. The focus is on debugging the code and understanding the behavior of loops in C programming.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant presents a C program that is intended to print the cumulative sum of integers from 1 to 20 but encounters an issue where it only prints "20 = 20".
  • Another participant identifies that a misplaced semicolon in the for loop is causing the loop to not execute the intended code block, suggesting that the loop should not end with a semicolon.
  • A later reply confirms that correcting the semicolon issue resolved the problem, indicating that the participant was previously unaware of this common mistake.
  • Another participant suggests trying to achieve the same result without using a loop, although no further details are provided on this approach.

Areas of Agreement / Disagreement

Participants generally agree on the identification of the semicolon issue as the cause of the program's incorrect output. However, the suggestion to try without a loop introduces a new perspective that remains unexplored in the discussion.

Contextual Notes

The discussion does not address the implications of removing the loop or how the program's output would change as a result. There are also no details on the specific implementation of the alternative approach suggested.

charmedbeauty
Messages
266
Reaction score
0

Homework Statement



I want to create a program that prints the sum of integers k for k(1-20)

ie,

k sum
1 1
2 3
3 6
4 10
5 .
6 .
.
.
.
.
.
20 .

in that format

Homework Equations





The Attempt at a Solution



#include <stdio.h>

int main(int argc,char * argv[]){
int i=0, sum=0, n=20;
for (i=0;i<n;++i);{

sum=sum+i;

printf("%d = %4d\n",i, sum);
}

return 0;
}

I don't want the user to enter an input (hence no scanf) I just want the program to run through integers 1-20 and print there sum.

when I run this it just prints 20 = 20??

help please!

Thanks.
 
Physics news on Phys.org
you put a semicolon in a bad spot

for ( ... ) ;{ ...code block... }

just runs the for loop but not the code block

try this:

for ( ...) { ...code block... }

this is a famous gotcha that many programmers do at one time or another
 
jedishrfu said:
you put a semicolon in a bad spot

for ( ... ) ;{ ...code block... }

just runs the for loop but not the code block

try this:

for ( ...) { ...code block... }

this is a famous gotcha that many programmers do at one time or another

Ohh wow that worked... I was stuck on that for a while... no pun intended:)
 
and now try without a loop
 

Similar threads

Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
9
Views
2K
Replies
22
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K