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

In summary, The conversation is about creating a program that prints the sum of integers from 1 to 20 in a specific format. The user does not want to enter any input and wants the program to run through the integers and print their sum. The attempt at a solution involved using a for loop, but the code was not executing properly due to a misplaced semicolon. Removing the semicolon allowed the program to run correctly.
  • #1
charmedbeauty
271
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
  • #2
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
 
  • #3
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:)
 
  • #4
and now try without a loop
 
  • #5




Hello,

Thank you for sharing your code and explaining your issue. From what I can see, the problem lies in the placement of your curly braces in the for loop. Currently, your loop is only running the statement inside the curly braces once, as it is acting as the body of the loop. To fix this, you can remove the semicolon after the for loop and move the curly brace down to include the printf statement. This will ensure that the loop runs through each integer and prints the correct sum.

Additionally, I noticed that your initial value for the loop is set to 0, which will result in your first sum being 0 instead of 1. To fix this, you can change the initial value to 1.

Your updated code should look something like this:

#include <stdio.h>

int main(int argc, char *argv[]) {
int i = 1, sum = 0, n = 20;
for (i = 1; i <= n; ++i) {
sum = sum + i;
printf("%d = %d\n", i, sum);
}
return 0;
}

I hope this helps. Keep up the good work with your programming skills!
 

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

1. Why is my loop not running through each integer?

There could be several reasons why your loop is not running through each integer. Some common causes could be incorrect loop syntax, incorrect variable initialization, or an infinite loop. Double-check your code and make sure all variables are properly defined and the loop conditions are correct.

2. How can I fix my loop to run through each integer?

To fix your loop, first make sure that the loop conditions are correct and the loop is not infinite. If that doesn't solve the issue, try debugging your code by printing out the values of your variables at each step in the loop. This can help identify where the problem is occurring and how to fix it.

3. Can using a different loop structure help with my issue?

It could be helpful to try using a different loop structure, such as a while loop or a do-while loop, to see if that solves the issue. Each loop structure has its own advantages and it's important to understand how they work in order to choose the best one for your specific problem.

4. How can I make my loop more efficient?

To make your loop more efficient, you can try using break or continue statements to skip unnecessary iterations. Additionally, you can consider using a different data structure or algorithm to improve the performance of your loop.

5. Can you provide an example of a simple c program with a loop?

Sure, here is an example of a simple program that uses a for loop to print the numbers 1 to 10:

#include int main(){ int i; for(i = 1; i <= 10; i++) { printf("%d ", i); } return 0;}

This program will print the numbers 1 to 10, each on a separate line.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
931
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top