473,473 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

While loop calculating the sum of all numbers from 1 - 1000

4 New Member
Expand|Select|Wrap|Line Numbers
  1. public class Main
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         int sum = 0;
  6.         while (sum <=1000)
  7.         {
  8.                 System.out.println(sum);
  9.                 sum++;
  10.         }
  11.  
  12.  
  13.     }
} am stuck here I have tried using a seperate interger but I cant seem to figure this out! Please help.
Apr 29 '14 #1
6 11896
chaarmann
785 Recognized Expert Contributor
Define a new integer like you have defined "sum", but call it "counter". Set it to start value 1 before entering the while loop. Inside the while loop, increase it by one.
Run the while loop as long as this counter is lesser or equal 1000 (condition after "while").
Also, inside the while loop, take the old value of "sum" and add the value of the counter to it.
Apr 29 '14 #2
hanslo91
4 New Member
am trying my best I am working within a leanring app and it keeps telling me I have the incorrect result. The new program that I put in after reading your comment is,


Expand|Select|Wrap|Line Numbers
  1. public class Main
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         int sum = 0;
  6.         int i = 1;
  7.  
  8.         while (i <=1000)
  9.         { 
  10.  
  11.                 System.out.println(sum);
  12.                 i++;
  13.                 sum = sum + i;
  14.         }
  15.  
  16.  
  17.     }
  18. }
Apr 29 '14 #3
Rabbit
12,516 Recognized Expert Moderator MVP
Please use code tags when posting code or formatted data.

You need to flip lines 12 and 13, otherwise you're skipping number 1.
Apr 29 '14 #4
hanslo91
4 New Member
Ok thanks Rabbit! I see that error now which I consitantly overlooked. I switched those two lines but now im having the issue where it isnt adding to what it should which is 500500.
Expand|Select|Wrap|Line Numbers
  1. public class Main
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         int sum = 0;
  6.         int i = 1;
  7.  
  8.         while (i <=1000)
  9.         { 
  10.  
  11.                 System.out.println(sum);
  12.  
  13.                 sum = sum + i;
  14.                 i++;
  15.         }
  16.  
  17.  
  18.     }
  19. }
Im sorry for asking so many questions but im not taking an actual class. If it is something really simple im missing please tell me its simple but dont tell me the code. I think im gonna review loops.
Apr 29 '14 #5
Rabbit
12,516 Recognized Expert Moderator MVP
The printout should be after line 13, otherwise you're printing out the value before the numbers are added.
Apr 29 '14 #6
hanslo91
4 New Member
Oh man I can't believe it! Thank you Rabbit, it worked and I can move onto the next course finally! Thanks again!
Apr 29 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: phenix151 | last post by:
I am tring to read numbers from the command line. they are to be input like this: 12.34 14.67 82 227 -17 I know how many numbers are to be input by asking. I want to put this in a for or a...
1
by: AJ Smith | last post by:
To Whom It May Concern: I have the following code: #include <cctype> //Provides isdigit #include <cstdlib> //Provides EXIT_SUCCESS #include <cstring> //Provides strchr #include <iostream>...
4
by: Buckaroo Banzai | last post by:
Hi, I'm trying to generate 3 random numbers inside a while loop. The problem I encountered is that I always get the same 3 numbers even after I reseed the number using srand((unsigned)time(NULL));...
1
by: maas | last post by:
hello i have a problem in the do while loop where the program will ask the user to enter yes and do an iteration, nut in my example it did not do?? here is the code import java.sql.*;...
5
by: jure87 | last post by:
hello everyone im new to this forum and need some help...i have a java code called flight.java and another code called checkflights.java, the purpose of this is to readin a text that is called...
2
by: cmb3587 | last post by:
I am having a problem with the validation of the account number and password. The beginning of the program asks for users account # then pwd. The program is then supposed to go to a checkID...
2
by: alireza6485 | last post by:
Hi, Could you please rewrite the program for me?I tried my best and the program still does not do what it has to do. I have to write a code that generates random speed and distance .it ask the...
4
by: shane3673 | last post by:
i need to write a basic c++ cmd program that uses the while loop to display the odd numbers and sum of the even numbers in between 2 numbers that the users types in. Im not sure how to go about this....
4
GraxGunz
by: GraxGunz | last post by:
(cpp newbie here) my program have to show the number of positive numbers, negative numbers, the sum of these numbers, and the average of these numbers, and the program exit when the input is 0 ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.