# Using loops to add odd numbers

Page 1 of 1

## 11 Replies - 4777 Views - Last Post: 15 December 2011 - 11:16 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=259974&amp;s=f241e7fdaa5c666d9f3e4353262a9f8e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 98
• Joined: 12-December 11

# Using loops to add odd numbers

Posted 15 December 2011 - 06:37 AM

Java - I am struggling on a certain question in my assignment where it asks you to use a loop method to add all the odd numbers within a specific range. Any help please?
Is This A Good Question/Topic? 0

## Replies To: Using loops to add odd numbers

### #2 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 07:05 AM

Show us what you've done and point out what's causing you to struggle.

If the beginning of the range is 'beginning', it's an odd number, and the end of the range is 'ending', your for loop statement should look something like:

```for ( int i = beginning ; i < ending ; i += 2 )
```

Reputation: 0
• Posts: 98
• Joined: 12-December 11

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 07:09 AM

Thanks Gregg. This is my code so far, can you point out what I have done wrong and what I need to add?
```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package assignment2;

/**
*
* @author currys
*/
import java.util.Scanner;
public class Oddy {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner (System.in);
int num1, num2, sum, total, i;
System.out.println("Please enter two integers:" ); num1 = scan.nextInt(); num2 = scan.nextInt();

}
}

```

This post has been edited by macosxnerd101: 15 December 2011 - 06:13 PM
Reason for edit:: Please use code tags

### #4 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 07:18 AM

I assume the 2 integers (num1 and num2) you're collecting are the lower and upper range, so after you have those you need to determine if num1 is odd, if not add 1 to make it odd. Then set up a for loop similar to what I posted before from num1 or num + 1 to num2, whose body adds each odd number in the range to another variable, sum. I assume you'll then want to print out the result. Program over.

Reputation: 0
• Posts: 98
• Joined: 12-December 11

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 07:37 AM

Thankyou Gregg, I have tried what you said but it sais that the beginning and the ending variables are not initialized and there is an empty statement after for.

Would it be possible if you could show me an example to help me understand.

### #6 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 07:39 AM

Show us what you tried, posted in CODE TAGS, with the error message you received, copied and pasted.

### #7 RobW1985

Reputation: 14
• Posts: 38
• Joined: 22-June 11

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 07:46 AM

Also you may want to tell your user's when you ask them to provide two integers, to provide them in order from least to greatest. This would prevent the loop from starting with the higher number and returning with an error/infinite loop/or incorrect result

Reputation: 0
• Posts: 98
• Joined: 12-December 11

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 07:49 AM

```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package assignment2;

/**
*
* @author currys
*/
import java.util.Scanner;
public class Oddy {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner (System.in);
int num1, num2, sum, total, i, beginning, ending;
System.out.println("Please enter two integers:" ); num1 = scan.nextInt(); num2 = scan.nextInt();
for (i = beginning ; i < ending ; i += 2 );//*Variable beginning might not have been initialized. Variable ending might not have been initialized. Empty statement after for.
System.out.println("Result:" + i);
{
}
}

```

This post has been edited by macosxnerd101: 15 December 2011 - 06:14 PM
Reason for edit:: Please use code tags

### #9 RobW1985

Reputation: 14
• Posts: 38
• Joined: 22-June 11

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 08:03 AM

your loop is missing the declaration part, the for loop should look something along the lines of this
the first part of the loop declares a variable to be used as a counter, you must include the int part, the second part is the evaluation, if index < someNumber continue, if it's not then the loop stops, the third part is the incrementer, it increments index by 1 each time the loop executes. Dont use the exact names I have used but fill in with your own variables,as for the other errors, you are never declaring what beginning and end equal, you must declare what they are(hint: there integers) and what they are equal to(hint: num1 and num2) before they can be used

Reputation: 0
• Posts: 98
• Joined: 12-December 11

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 08:24 AM

I am still having difficulties

### #11 RobW1985

Reputation: 14
• Posts: 38
• Joined: 22-June 11

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 09:25 AM

your close but not quite there, this part here:
for (i = beginning ; i < ending ; i += 2 );//*Variable beginning might not have been initialized. Variable ending might not have been initialized. Empty statement after for.
System.out.println("Result:" + i);

needs to be fixed
1. Declare your variables beginning being the smaller number and ending being the bigger
int beginning = num1;//or whichever value is smaller
int ending = num2;
for (int i = beginning; i < ending; i+=2) {
sum += i; // Sum is equal to itself plus the value of I, this keeps a running total
}
3. Make sure whenever you use a variable it has been declared, attempting to use an undeclared variable will result in error every time

after this, you can print out the total of sum or do whatever you want with it

This post has been edited by RobW1985: 15 December 2011 - 09:26 AM

Reputation: 0
• Posts: 98
• Joined: 12-December 11

## Re: Using loops to add odd numbers

Posted 15 December 2011 - 11:16 AM

I have done what you said buts adding the two numbers together instead of adding all the odd numbers within the range.