# Consecutive number addition

• July 8th, 2013, 11:27 PM
Tedstriker
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.
• July 9th, 2013, 01:46 AM
jps
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?"
• July 9th, 2013, 03:38 AM
Stayawake
If given a input which contains an upperbound, you could something along the lines like this:

Code java:

```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.
• July 9th, 2013, 03:47 AM
PhHein
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.
• July 9th, 2013, 09:54 AM
Tedstriker
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.
• July 9th, 2013, 11:25 AM
derekxec
Re: Consecutive number addition
do they put in each number at a time like
1
3
4
or all at once like 5235324?
• July 9th, 2013, 02:55 PM
Tedstriker
Re: Consecutive number addition
Quote:

Originally Posted by derekxec
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.
• July 9th, 2013, 09:49 PM
jps
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
• July 10th, 2013, 05:05 PM
Stayawake
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.
• July 14th, 2013, 06:15 AM
Re: Consecutive number addition
try the pseudocode given above and make sure you get the loops right....best of luck :)
• July 14th, 2013, 11:52 PM
Tedstriker
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:

Code java:

```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.
• July 15th, 2013, 12:14 PM
Seraphim
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.