1. Member Join Date
Feb 2012
Posts
6
Rep Power
0

## Adding odd numbers

This is probably something very basic, but I can't for the life of me think of how to get this to work.

What I need to do is prompt the user for a positive integer(n), then add n odd numbers together (eg: n=4; add 1+3+5+7 = 16)

I know I need to loop through 4 times: for(int i = 0; i < n; i++)
I can add up sequential numbers, just can't seem to get it right when I only need odd numbers.

I'm stuck on this problem, can someone help me with this?

Thanks! 2. ## Re: Adding odd numbers

If you have to loop n times, the largest number in your loop is 2*n-1 (check this); you only want to do odd numbers so you have to start at one (1) with an increment of 2; so:

Java Code:
```for (int i= 1; i <= 2*n-1; i+= 2)
// do your dirty deed here ...```
But, if you apply a bit of math, you don't have to loop at all; adding the numbers 1+3+5+ ... +2*n-1 is n*n

kind regards,

Jos 3. Member Join Date
Feb 2012
Posts
6
Rep Power
0

## Re: Adding odd numbers

yeah, I understand that this is just squaring the N number, but here's the question I'm trying to answer:

Write a program that reads in a positive integer N and then calculates and displays
the sum of the first N odd integers. For example, if N is 4, your program should
display the value 16, which is 1 + 3 + 5 + 7.
I'm trying to answer this by printing out the loop (ex: i = 0 and total = 1, i = 1 and total = 3, i = 2 and total = 5, i = 3 and total = 7) Then I'll have a separate variable to add total for find the final answer of 16.

I want to break it down into the it's smallest parts so I can get a better understanding of why and how it's doing what it's doing. 4. ## Re: Adding odd numbers Originally Posted by kmlam585 yeah, I understand that this is just squaring the N number, but here's the question I'm trying to answer:

I'm trying to answer this by printing out the loop (ex: i = 0 and total = 1, i = 1 and total = 3, i = 2 and total = 5, i = 3 and total = 7) Then I'll have a separate variable to add total for find the final answer of 16.

I want to break it down into the it's smallest parts so I can get a better understanding of why and how it's doing what it's doing.
See the loop in my previous reply.

kind regards,

Jos 5. Member Join Date
Feb 2012
Posts
6
Rep Power
0

## Re: Adding odd numbers

Thanks for the help! I was thinking about it completely wrong.

Java Code:
```import acm.program.*;

public class AddOddInts extends ConsoleProgram {

public void run() {
int n = readInt("Enter a positive integer: ");
int total = 0;
for (int i = 1; i <= 2 * n - 1; i++) {
if (i % 2 != 0) {
total += i;
//println(i + " Odd " + total);
}
}
println(total);
}
}``` 6. ## Re: Adding odd numbers

Almost perfect; if i is an odd number, then i+2 also is an odd number. You can change your loop to this:

Java Code:
```for (i= 1; i <= 2*n-1; i+= 2)
total+= i;```
Variable i only has odd values so you can skip the test in the body of your for loop.

kind regards,

Jos 7. Member Join Date
Feb 2012
Posts
6
Rep Power
0

## Re: Adding odd numbers

ahhh, ok that makes sense to me. I was doing a lot of this, but not putting it together correctly. I see how it works now though. 