You are viewing our Forum Archives. To view or take place in current topics click here.
Java programming please help.
Posted:

Java programming please help.Posted:

UK_cHaDd3rZ
  • Ladder Climber
Status: Offline
Joined: Sep 05, 201212Year Member
Posts: 384
Reputation Power: 14
Status: Offline
Joined: Sep 05, 201212Year Member
Posts: 384
Reputation Power: 14
Hello TTG, I am learning the java language for computer programming. I really need help on what seems to be an easy bit of coding. I am learning from these tutorials on a android IDE called aide.

Tutorial: Use a while loop to calculate the sum of all numbers from 1 to 1000 . Hint: Add each number to sum in each loop iteration, like sum = sum + i .
This is my code. Thanks.

 public class Main
{
   public static void main(String[] args)
   {
      int sum = 0;
      // Use a while loop to calculate the sum of 1 to 1000
     while (sum != 1000) { sum = sum + 1; }
      System.out.println(sum);
   }
#2. Posted:
UnrealEgg
  • Powerhouse
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
Status: Offline
Joined: May 30, 201014Year Member
Posts: 438
Reputation Power: 49
If you think about it logicaly this is what is currently happening:
0 = 0 + 1
1 = 1 + 1
2 = 2 + 1
3 = 3 + 1
4 = ....

All you are doing is incrementing sum, not adding like the question asks you to do (1 + 2 + 3 + 4 ...)

Solutions:
You can use something similar to what you have no but instead do the following.



public class Main
{
   public static void main(String[] args)
   {
      int sum = 0; // Use a while loop to calculate the sum of 1 to 1000
      int total = 0;
     while (sum <= 1000) {
          total += sum;
          ++sum;
     }
      System.out.println(sum); //Should be 500,500 for 1000
   }




Alternativly, you could have a recursive function which does it for you too. Using this function you would call the maximum number so sumOfNumbers(1000) for example.


int sumOfNumbers(int number){
     if(number == 1)
         return 1;
     return number + sumOfNumbers(number - 1);
}


Last edited by UnrealEgg ; edited 1 time in total
#3. Posted:
var
  • TTG Senior
Status: Offline
Joined: Dec 24, 201212Year Member
Posts: 1,498
Reputation Power: 79
Status: Offline
Joined: Dec 24, 201212Year Member
Posts: 1,498
Reputation Power: 79
int counter = 1;
int sum = 0;
while(counter <= 1000){
sum+=counter; //same as saying sum = sum + counter
counter++;
}
#4. Posted:
Tickld
  • Prospect
Status: Offline
Joined: Mar 15, 201113Year Member
Posts: 616
Reputation Power: 24
Status: Offline
Joined: Mar 15, 201113Year Member
Posts: 616
Reputation Power: 24
Didn't have a lot of time to check if this is correct but this is what I did.

//i the number for exiting the loop and adding to sum

int i;
i = 1;

//sum is the number which will be used to illustrate //the answer.

int sum;
sum = 0;

//It needs to be 1001 so it includes 1000, if it is
//(i<1000) it will stop on 999.
while (i < 1001)
{
sum=sum+i;
i++;
}

TextIO.putln("sum");

TextIO.putln is just an addon/different method that I use to make things easier for me, it is the same as System.out.println(sum);
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.