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 12 of 12

Thread: Consecutive number addition

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Consecutive number addition

    Hi everyone,

    Sorry for posting here if this is not the correct place, but I have a question I'm kinda stumped on.

    I need to create a program to add together a row of numbers, something like this:

    (123456789) = 45

    I'm trying to figure out how to do this but am at a loss. Any ideas? thanks in advance.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Consecutive number addition

    How are the numbers stored? Are they supposed to be user input?

    What exactly are you stuck on, writing the algorithm or the code?

    --- Update ---

    Thread moved from "Whats wrong with my code?"

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    If given a input which contains an upperbound, you could something along the lines like this:

    Int UpperBound = input
    Int Sum
     
    For(int i; i < UpperBound; i++){
       Sum += i
    }
     
    Output sum

    I'm sorry for the pseudo-programming language I used.. I'm on my iphone and I'm a beginner myself so I don't know the syntax on top of my head. I would give you a working example if I could but I gave you a way to work it out yourself.

    Goodluck and let me know how it went.

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Consecutive number addition

    That pseudocode is wrong and you should not provide working examples. Too many newbies just copy and paste examples without thinking.

  5. #5
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Consecutive number addition

    Sorry guys, I should have been more specific. The user will input 9 consecutive numbers, and the output needs to be the sum of the numbers.

  6. #6
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Consecutive number addition

    do they put in each number at a time like
    1
    3
    4
    or all at once like 5235324?

  7. #7
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Consecutive number addition

    Quote Originally Posted by derekxec View Post
    do they put in each number at a time like
    1
    3
    4
    or all at once like 5235324?
    it would be all at once, as in :

    123456789

    the sum is 45.

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Consecutive number addition

    So decide how you would solve the problem without a computer. Say for example someone handed you a sheet of paper with the numbers printed on it.
    What steps would you take to solve the problem yourself?
    Come up with your plan of attack and then implement that plan in code.
    For example: Get the first number. Set total to this number.
    Get the next number. Add it to the total.
    Continue doing this until there are no numbers left.
    etc.

    Trying to provide hints without writing the code for you, but I am not exactly sure what it is you are having trouble with

  9. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Consecutive number addition

    I've coded your problem and I will list the steps I took so you can program it.
    note that type conversions may be needed.

    1. you read the number into a variable
    2a. make a loop
    2b. in the loop take the first digit from the variable. There's a function for that.
    2c. add that digit to a sum variable
    2d. do the loop 9 times
    3. print out the sum.

    If there's something you don't understand, or you want me to post the solution. feel free to ask.

  10. #10
    Banned
    Join Date
    Jul 2013
    Posts
    49
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: Consecutive number addition

    try the pseudocode given above and make sure you get the loops right....best of luck

  11. #11
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Consecutive number addition

    Sorry for my poor explanations guys. I've been working on this, and made a little progress. this is what I have so far:

    import java.util.Scanner;
    class Adding
    {
        public static void main(String[] args)
        {
    int[] scores = new int[10];
    Scanner numbers = new Scanner(System.in);
    System.out.println("Enter 10 numbers: ");
    for (int x=0; x<scores.length; x++)
    {
    if (numbers.hasNextInt())
    scores[x] = numbers.nextInt();
    System.out.println("The total is: " + ????);
    }
    }
    }

    I'm still a little lost as to how I display the sum of the numbers. Any ideas. Thanks in advance.

  12. #12
    Junior Member
    Join Date
    Jul 2013
    Posts
    8
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Consecutive number addition

    If I were doing this on paper to add something up you plus one thing to another, so maybe you should try to add another variable that equals the number you just read into your array and add it to the next one in your array. I hope it helps.

Similar Threads

  1. Addition Table.
    By LoganC in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 4th, 2012, 12:02 PM
  2. Replies: 1
    Last Post: September 17th, 2011, 10:28 AM
  3. Addition
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 14th, 2011, 10:49 PM
  4. Replies: 1
    Last Post: December 2nd, 2010, 05:29 PM