• 02-12-2012, 08:50 PM
kmlam585
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!
• 02-12-2012, 09:08 PM
JosAH
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:

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
• 02-12-2012, 09:16 PM
kmlam585
yeah, I understand that this is just squaring the N number, but here's the question I'm trying to answer:

Quote:

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.
• 02-12-2012, 09:19 PM
JosAH
Quote:

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
• 02-12-2012, 09:44 PM
kmlam585
Thanks for the help! I was thinking about it completely wrong.

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);     } }```
• 02-12-2012, 10:24 PM
JosAH
Almost perfect; if i is an odd number, then i+2 also is an odd number. You can change your loop to this:

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
• 02-12-2012, 11:25 PM
kmlam585