# need help while loop sum of odd integers

Page 1 of 1

## 10 Replies - 2728 Views - Last Post: 18 September 2013 - 09:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=329202&amp;s=706317e55cf2df2ff185a9f0e7102998&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shawk23

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 15-September 13

# need help while loop sum of odd integers

Posted 15 September 2013 - 09:32 PM

need help computing the sum of odd integers between 1 and 9. (**using a while loop) what i have so far is:
```public class HW1
{
public static void main(String[] args)
{
int sum = 0, n = 1;

while (n <= 9  )
{

if(n%2 !=0);
else
if(true)
sum = sum + n;
n++;
System.out.println(sum);
}

}
}
```

This post has been edited by jon.kiparsky: 15 September 2013 - 09:38 PM
Reason for edit:: code tags

Is This A Good Question/Topic? 0

## Replies To: need help while loop sum of odd integers

### #2 jon.kiparsky

• Beginner

Reputation: 11069
• Posts: 18,906
• Joined: 19-March 11

## Re: need help while loop sum of odd integers

Posted 15 September 2013 - 09:43 PM

You start out reasonably well, but what happened here?

```            if(n%2 !=0);
else
if(true)
sum = sum + n;
n++;
System.out.println(sum);

```

1. The semicolon after the if condition is actually an instruction, it means "do nothing". So that line would be equivalent to
```if(n%2 !=0)
{
;  // empty instruction: do nothing if n is odd
}

```

2. else what?
```if (true)
```

always executes, because the condition is always true. So if n is NOT odd, you're adding n to sum.

3. Then you increment n, regardless of what else has happened, and finally you print the sum.

So I'm not sure what logic is happening here, but those are some things to look at.

### #3 shawk23

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 15-September 13

## Re: need help while loop sum of odd integers

Posted 15 September 2013 - 10:07 PM

jon.kiparsky, on 15 September 2013 - 09:43 PM, said:

You start out reasonably well, but what happened here?

```            if(n%2 !=0);
else
if(true)
sum = sum + n;
n++;
System.out.println(sum);

```

1. The semicolon after the if condition is actually an instruction, it means "do nothing". So that line would be equivalent to
```if(n%2 !=0)
{
;  // empty instruction: do nothing if n is odd
}

```

2. else what?
```if (true)
```

always executes, because the condition is always true. So if n is NOT odd, you're adding n to sum.

3. Then you increment n, regardless of what else has happened, and finally you print the sum.

So I'm not sure what logic is happening here, but those are some things to look at.

Thank you late night programming after a long day leads to silly mistakes....
now this is where i'm at not sure where my problem is here

```class HW2
{
public static void main(String[] args)
{
int n = 1, sum = 0;
while(n <= 9)
{
if(n%2 !=0)
sum = sum + n;
n++;
System.out.println(sum);
}
}
}
```

This post has been edited by jon.kiparsky: 15 September 2013 - 10:23 PM
Reason for edit:: code tags

### #4 jon.kiparsky

• Beginner

Reputation: 11069
• Posts: 18,906
• Joined: 19-March 11

## Re: need help while loop sum of odd integers

Posted 15 September 2013 - 10:24 PM

Is there a question in there somewhere?

### #5 shawk23

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 15-September 13

## Re: need help while loop sum of odd integers

Posted 15 September 2013 - 10:37 PM

jon.kiparsky, on 15 September 2013 - 10:24 PM, said:

Is there a question in there somewhere?

Sorry new to this..
```import java.util.Scanner;
public class HW1
{
public static void main(String[] args)
{
int sum = 0, n;
Scanner kb = new Scanner(System.in);
n = kb.nextInt();

while (n < 1)
{

if(n%2 !=0);

sum = sum + n;
n++;
System.out.println(sum);
}

}
}
```

I have figured out the first part of my question and in part two I am asked to compute the same as above (sum of odd integers) but ask for the conditions. When I enter the same conditions as in part 1 (n=1, x=9) I do not get the same result as in the first.

### #6 jon.kiparsky

• Beginner

Reputation: 11069
• Posts: 18,906
• Joined: 19-March 11

## Re: need help while loop sum of odd integers

Posted 15 September 2013 - 10:43 PM

Take a closer look at your while loop condition. What happens if I enter some value >1 for n?

### #7 shawk23

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 15-September 13

## Re: need help while loop sum of odd integers

Posted 18 September 2013 - 03:48 PM

jon.kiparsky, on 15 September 2013 - 10:43 PM, said:

Take a closer look at your while loop condition. What happens if I enter some value >1 for n?

I am now stuck again, I am new to this... brand new in fact I just can't seem to figure out where I am going wrong.
```    public static void main(String[] args)
{
int sum = 0, n, count = 1;
Scanner kb = new Scanner(System.in);
System.out.println("Enter n");
n = kb.nextInt();
while (count <= n)
{

if(sum%2 !=0)
{
sum = sum + count;
count++;
}
else ;
}
System.out.println(n + sum);
}
}
```

### #8 MentalFloss

• .

Reputation: 577
• Posts: 1,500
• Joined: 02-September 09

## Re: need help while loop sum of odd integers

Posted 18 September 2013 - 09:05 PM

shawk23, on 18 September 2013 - 10:48 PM, said:

I am now stuck again, I am new to this... brand new in fact I just can't seem to figure out where I am going wrong.

It's cool. We've all been there. Here's how you have to play the game for a little bit though:

```import java.util.Scanner;

public class Client {
public static void main(String[] args) {
int sum = 0, n, count = 1;
Scanner kb = new Scanner(System.in);
System.out.println("Enter n");
n = kb.nextInt();
System.out.println("dbg: n = " + n);
while (count <= n) {
System.out.println("dbg: count = " + count + ", n = " + n);
if (sum % 2 != 0) {
System.out.println("dbg: sum = " + sum + " & sum%2 != 0");
sum = sum + count;
System.out.println("dbg: sum(changed) = " + sum);
count++;
System.out.println("dbg: count = " + count);
} /*else
; you don't need that...*/
}
System.out.println(n + sum);
}
}

```

Stick with it and we'll show you some new tricks along the way but everyone starts like that.

### #9 jon.kiparsky

• Beginner

Reputation: 11069
• Posts: 18,906
• Joined: 19-March 11

## Re: need help while loop sum of odd integers

Posted 18 September 2013 - 09:30 PM

shawk23, on 18 September 2013 - 05:48 PM, said:

jon.kiparsky, on 15 September 2013 - 10:43 PM, said:

Take a closer look at your while loop condition. What happens if I enter some value >1 for n?

I am now stuck again, I am new to this... brand new in fact I just can't seem to figure out where I am going wrong.

You can use debug statements as MentalFloss suggests, or you can step through the code in your head. Specifically, look at what happens in the code if you enter a value like 7 at the prompt.

```int n = 7;
while (count <= n)
{
System.out.println("Entered the loop!");
}

```

Will that println ever execute? Try to answer it by examination of the code, it's not that difficult.

(Sorry, didn't mean to hit the downrep button... I'll make it up elsewhere)

This post has been edited by jon.kiparsky: 18 September 2013 - 09:58 PM

### #10 shawk23

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 15-September 13

## Re: need help while loop sum of odd integers

Posted 18 September 2013 - 09:44 PM

MentalFloss, on 18 September 2013 - 09:05 PM, said:

shawk23, on 18 September 2013 - 10:48 PM, said:

I am now stuck again, I am new to this... brand new in fact I just can't seem to figure out where I am going wrong.

It's cool. We've all been there. Here's how you have to play the game for a little bit though:

```import java.util.Scanner;

public class Client {
public static void main(String[] args) {
int sum = 0, n, count = 1;
Scanner kb = new Scanner(System.in);
System.out.println("Enter n");
n = kb.nextInt();
System.out.println("dbg: n = " + n);
while (count <= n) {
System.out.println("dbg: count = " + count + ", n = " + n);
if (sum % 2 != 0) {
System.out.println("dbg: sum = " + sum + " & sum%2 != 0");
sum = sum + count;
System.out.println("dbg: sum(changed) = " + sum);
count++;
System.out.println("dbg: count = " + count);
} /*else
; you don't need that...*/
}
System.out.println(n + sum);
}
}

```

Stick with it and we'll show you some new tricks along the way but everyone starts like that.

Thank you , but I am still not printing the results I want. I am trying to obtain the sum of odd integers up to n that is entered. For example if i enter n = 5 I should yield the result 25.
I am getting an infinite loop still.

### #11 MentalFloss

• .

Reputation: 577
• Posts: 1,500
• Joined: 02-September 09

## Re: need help while loop sum of odd integers

Posted 18 September 2013 - 09:45 PM

OK well, just know that the only spot you are incrementing count which is a condition of your while is in a location that does not get hit on first pass so it will never increment and thus it is an infinite loop.

It jumps out at you if you have a hojillion print statements blazing by but it's hard to see if you've never programmed before.

This one's just not ready for "mentally stepping through code".