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.

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?"

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.

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.

Re: Consecutive number addition

do they put in each number at a time like

1

3

4

or all at once like 5235324?

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.

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

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.

Re: Consecutive number addition

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

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.

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.