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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

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. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
    Last edited by Norm; November 16th, 2021 at 02:32 PM. Reason: Added / in ending code tag

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. GUI Exercise
    By david1 in forum AWT / Java Swing
    Replies: 22
    Last Post: May 3rd, 2014, 05:12 PM
  2. [SOLVED] Exercise 137
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 19th, 2014, 11:43 AM
  3. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  4. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  5. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM