Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Here is exercise that tells us to sum numbers as entered by a user,but i don't know how to do the summation the way the exercise tell me

1. ## Here is exercise that tells us to sum numbers as entered by a user,but i don't know how to do the summation the way the exercise tell me

Create an application that will determine and display the sum of numbers as entered by the user. The summation must take place so long the user wants to. When the user ends the program, the summation must be displayed as follows:

num1 + num2 + num3 +…+ numN = sum

Example

Say the user has entered 6 numbers. The output must be displayed as follows:

10 + 15 + 25 + 45 + 20 + 5 = 120

2. ## Re: Here is exercise that tells us to sum numbers as entered by a user,but i don't know how to do the summation the way the exercise tell me

What have you tried? Post the code for your current attempt with any specific questions you have about it.

Be sure to wrap your code with code tags:

[code]
[/code]

to get highlighting and preserve formatting.

3. ## Re: Here is exercise that tells us to sum numbers as entered by a user,but i don't know how to do the summation the way the exercise tell me

```
package summationapp;
import java.util.Scanner;

/**
*
* @author SETH
*/
public class SummationApp {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// declare variables
int num,sum=0;
Scanner sc = new Scanner(System.in);

//get a value
System.out.print("Please enter a positive integer or a number less than 1 to end program: ");
num = sc.nextInt();

//iterate whilst a user wants to
while(num>0)
//accumulate the sum
sum = sum + num;

System.out.print("Please enter a positive integer or a number less than 1 to end program: ");
num = sc.nextInt();

//display the sum
System.out.println("The sum is " + sum);```

4. ## Re: Here is exercise that tells us to sum numbers as entered by a user,but i don't know how to do the summation the way the exercise tell me

Did you have any questions?
What happens when you compile the program?
If there are no compiler errors, what happens when you execute the program?

5. ## Re: Here is exercise that tells us to sum numbers as entered by a user,but i don't know how to do the summation the way the exercise tell me

When i execute the program i can't display the summation as shown in the statement

6. ## Re: Here is exercise that tells us to sum numbers as entered by a user,but i don't know how to do the summation the way the exercise tell me

Please post the program's output so we can see what is wrong?

Also please post the code for the complete program.