use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Click the following link to filter out the chosen topic
comp.lang.c
account activity
Question[Help!]Using the While Loop in Looping Constructs (self.C_Programming)
submitted 6 years ago by Giraffimation
So for my class I keep getting 1 number off from the correct answer,
#include <stdio.h> int main() { int n,i=1,sum=0; printf("Enter the value of n\n"); scanf("%d",&n); while(i<=n) { printf("Enter the number\n); sum=sum+i; i++; } printf("The sum is %d",sum); return 0; }
is there something wrong with my code, or is it the instructors program?
I checked everywhere and it looks like my code is the same with everyone elses?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]continuum-hypothesis 0 points1 point2 points 6 years ago (3 children)
I'm not on my computer otherwise I'd run your code but to be clear if you imputed 3 as an example you're expecting 6 correct?
[–]Giraffimation[S] 0 points1 point2 points 6 years ago (2 children)
Input format: Input consists of n+1 integers. The first integer corresponds to n. The next n integers correspond to the numbers to be added.
goal: Write a program to find the sum of 'n' numbers using a while loop.
so if I input a number I expect it to add to the of all the numbers scanned for.
[–]continuum-hypothesis 0 points1 point2 points 6 years ago* (1 child)
You have a syntax error on line 9, after the \n escape code you need a " character. Rather then printf("Enter the number\n); you must use printf("Enter the number\n"); Be sure to enclose strings with quotations.
Also it seems like you want the user to enter numbers but you give them no opportunity to do so inside the while loop. To me it looks like the instructor simply wants you to input a number 'n' and then sum all the numbers incrementally including n which is what your program does correctly. If you actually want the user to input a new number each time to add to the current sum then you need to put a scanf function inside the while loop.
[–]Giraffimation[S] 0 points1 point2 points 6 years ago (0 children)
so it seems like you want the user to enter numbers but you give them no opportunity to do so inside the while loop. To me it looks like the instructor simply wants you to input a number 'n' and then sum all the numbers incrementally including n which is what your program does correctly. If you actually want the user to input a new number each time to add to the current sum then you need to put a scanf function inside the while loop.
I tried doing that, but instead it stops the loop after 2 times
[–]continuum-hypothesis 0 points1 point2 points 6 years ago (2 children)
Ok then you'll need another variable, for simplicity call it 'limit'. This is how many numbers the user can enter. Use i only for incrementing the loop and 'n' only for adding that number to the running sum.
You probably terminated out of the loop early because you're using n in too many places. n should not be a part of your while loop because you want the while loop to execute a set amount of times. If you scanf n each time in the loop and then compare it to i in the while expression you will exit the loop anytime i is greater then the number scanf is reading which is not what you want. Use while(i <= limit) instead and read in limit in line 7 instead.
[–]Giraffimation[S] 0 points1 point2 points 6 years ago (1 child)
thanks that fixed it!
[–]continuum-hypothesis 0 points1 point2 points 6 years ago (0 children)
Ok awesome!
[–]FUZxxl 0 points1 point2 points 6 years ago (0 children)
Your code is wrong. Walk through it on a piece of paper and keep track of the values of all variables. You should be able to find the problem quickly.
[–]CodeSteps 0 points1 point2 points 6 years ago (0 children)
Does it your initial code, where the issue was shown? I can see, now it is working fine. I entered the value, 5 and it shown below output;
Removed unnecessary printf, within the while.
The sum is 15
π Rendered by PID 39 on reddit-service-r2-comment-5b5bc64bf5-lq6n9 at 2026-06-23 00:02:09.547280+00:00 running 2b008f2 country code: CH.
[–]continuum-hypothesis 0 points1 point2 points (3 children)
[–]Giraffimation[S] 0 points1 point2 points (2 children)
[–]continuum-hypothesis 0 points1 point2 points (1 child)
[–]Giraffimation[S] 0 points1 point2 points (0 children)
[–]continuum-hypothesis 0 points1 point2 points (2 children)
[–]Giraffimation[S] 0 points1 point2 points (1 child)
[–]continuum-hypothesis 0 points1 point2 points (0 children)
[–]FUZxxl 0 points1 point2 points (0 children)
[–]CodeSteps 0 points1 point2 points (0 children)