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

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. ## 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. 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. ## 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. ## 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. ## Re: Consecutive number addition

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

7. ## Re: Consecutive number addition

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.

8. ## 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. ## 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. ## Re: Consecutive number addition

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

11. ## 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;
{
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. ## 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.