Sum of numbers

I am supposed to write a program that asks the user for a positive non-zero integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. (if a user enters 5 the program would add 1,2,3,4,5 and print out 15.)

Yesterday I wrote this code:

import javax.swing.JOptionPane;

publicclass Test

{

publicstaticvoid main(String[] args)

{

int done = 0;

do{

String input;

double n;

double s = 0;

input = JOptionPane.showInputDialog("Enter a number: ");

n = Double.parseDouble(input);

s = ((1+number)*(number/2));

if (n < 1)

{

JOptionPane.showMessageDialog(null,"The number you have entered" +

" is not a positive nonzero" +

" integer value.");

}

else

{

JOptionPane.showMessageDialog(null, s);

}

}while (done == 1);

}

}

but my professor said that I should use increment or decrement operators (++ --) instead of using my formula:

s = ((1+number)*(number/2))

So today I wrote:

import javax.swing.JOptionPane;

publicclass Testtwo

{

publicstaticvoid main(String[] args)

{

int number;

int sum;

String input;

input = JOptionPane.showInputDialog("Enter a number: ");

number = Integer.parseInt(input);

while (number < 1)

{

input = JOptionPane.showInputDialog("The number must be a POSITIVE " +

"NONZERO integer value: ");

number = Integer.parseInt(input);

}

sum = ((number) + (number--));

JOptionPane.showMessageDialog(null, sum);

}

}

I don't get any errors, and when I type in 3, I get the correct answer of 6 (1+2+3 = 6) but when I type in 5, I get an answer of 10.

Any insight to what I am doing wrong would be greatly appreciated.

[3220 byte] By [wksmdta] at [2007-10-3 9:22:25]
# 1
you arent really loopingnumber = 3 so:sum = ((number) + (number--));sum = 3 + 3you get 6
mkoryaka at 2007-7-15 4:36:04 > top of Java-index,Java Essentials,New To Java...
# 2

>I don't get any errors, and when I type in 3, I get the correct answer of 6 (1+2+3 = 6)

That was just luck.

>but when I type in 5, I get an answer of 10.

This line: sum = ((number) + (number--));

which can be written without all the parentheses, like this:

sum = number + number--

Says add number to number and store the result in sum, and then decrement number. So, if number equals 3, those steps are:

sum = 3 + 3

number = number -1

and if number equals 5, the steps taken by that line of code are:

sum = 5 + 5

number = number - 1

To get the result you want, you can use a for loop that loops from 1 to less than or equal to number. Use a variable called sum, and add i to it every time through the loop.

7studa at 2007-7-15 4:36:04 > top of Java-index,Java Essentials,New To Java...
# 3
> you get 6Which is the correct answer. ;-)
CaptainMorgan08a at 2007-7-15 4:36:04 > top of Java-index,Java Essentials,New To Java...
# 4
also your loop condition is wrongnumber is not < 1 right way, so the body executes once, and exits
mkoryaka at 2007-7-15 4:36:04 > top of Java-index,Java Essentials,New To Java...
# 5

hi wksmdt,

when you use the -- (postdecrement) Operator in your term number + number--, it is applied AFTER adding number + number. Maybe try this

while (number > 0) {

sum += number--;

}

E.g. number = 5

After 1. Loop sum = 5, number =4,

after 2. Loop sum = 9, number = 3,

after 3. Loop sum = 12, number = 2,

after 4. Loop sum = 14, number = 1,

after 5. Loop sum = 15, number = 0

end of loop.

KrumreinAlexa at 2007-7-15 4:36:04 > top of Java-index,Java Essentials,New To Java...
# 6

import javax.swing.JOptionPane;

public class Testtwo

{

public static void main(String[] args)

{

int number = 1;

int sum = 0;

String input;

input = JOptionPane.showInputDialog("Enter a number: ");

number = Integer.parseInt(input);

while (number < 1)

{

input = JOptionPane.showInputDialog("The number must be a POSITIVE " +

"NONZERO integer value: ");

number = Integer.parseInt(input);

}

while (number > 0) {

sum += number--;

}

JOptionPane.showMessageDialog(null, sum);

}

}

Is there a quicker/more efficient way to write this? It will be on my next hand written exam, and It seems difficult to remember.

wksmdta at 2007-7-15 4:36:04 > top of Java-index,Java Essentials,New To Java...
# 7

The easiest thing to understand is a for loop that runs from 1 to 'number' and each time through the loop adds 'i', which is the loop counter, to a variable called sum.

As an exercise, write a for-loop that sums the numbers from 1-10 and uses System.out.prinln() to display the result.

7studa at 2007-7-15 4:36:04 > top of Java-index,Java Essentials,New To Java...