11 Replies - 3421 Views - Last Post: 15 December 2011 - 11:16 AM Rate Topic: -----

#1 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • 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 )

Was This Post Helpful? 0
  • +
  • -

#3 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 RobW1985  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 RobW1985  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • 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
for(int index=0;index<someNumber;index++)
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
Was This Post Helpful? 0
  • +
  • -

#10 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#11 RobW1985  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • 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;
2. your for loop
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

Was This Post Helpful? 0
  • +
  • -

#12 c9-adams  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1