6 Replies - 200 Views - Last Post: 07 February 2012 - 05:59 PM Rate Topic: -----

Topic Sponsor:

#1 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Finding and adding odd numbers in a range

Posted 07 February 2012 - 05:19 PM

I'm having real trouble trying to identify the odd numbers between a requested range and adding them. I am using a while statement in order to advice this. An if statement is used to try and identify the odd numbers. Thank you for your time



import java.util.Scanner;


public class Oddy {

    public static void main(String[] args) {
        int total = 0;
        
        Scanner userinput = new Scanner(System.in);
        

        System.out.print("Please enter two integers: ");

        int num1 = userinput.nextInt();
        int num2 = userinput.nextInt();
        
        
      int largest =  Math.max(num1, num2);
        int smallest = Math.min(num1, num2);

        while (smallest <= largest)
        {
            System.out.println(smallest);
            smallest++;
            
           if(smallest % 2 == 0)
            {
            
            }
           
                else 
            {
                total+= smallest;
                
                    
                        
            }
                
            }
            
               System.out.println(total) ;
                
               

    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Finding and adding odd numbers in a range

#2 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Finding and adding odd numbers in a range

Posted 07 February 2012 - 05:21 PM

What is the problem, your code looks ok to me?
Was This Post Helpful? 0
  • +
  • -

#3 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Re: Finding and adding odd numbers in a range

Posted 07 February 2012 - 05:25 PM

Oh sorry I should of said in my post.

I request two integers for example 10 and 14. Then I intended to identify the odd numbers and add them. for example 10 and 14 would be 24 however I get 39! not 24.
Was This Post Helpful? 0
  • +
  • -

#4 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Finding and adding odd numbers in a range

Posted 07 February 2012 - 05:29 PM

change smallest <= largest to smallest < largest

because: on the last iteration when smallest is equal to 14, it passes the initial check, then smallest is incremented by 1, and 15 is added to your total.
Was This Post Helpful? 0
  • +
  • -

#5 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Re: Finding and adding odd numbers in a range

Posted 07 February 2012 - 05:46 PM

Ahh thank you very much we've gotten closer to making it fully working. I've been testing it and realised it isn't correct when you enter two odd number.

For example, if I enter 3 and 7 it will show 12 instead of 15.


import java.util.Scanner;


public class Oddy {

    public static void main(String[] args) {
        int total = 0;
        
        Scanner userinput = new Scanner(System.in);
        

        System.out.print("Please enter two integers: ");

        int num1 = userinput.nextInt();
        int num2 = userinput.nextInt();
        
        
      int largest =  Math.max(num1, num2);
        int smallest = Math.min(num1, num2);

        while ( smallest < largest)
        {
            
            smallest++;
            
           if(smallest % 2 == 0)
            {
            
            }
           
                else 
            {
                total+= smallest;
                
                    
                        
            }
                
            }
            
               System.out.println(total) ;
                
               

    }
}

This post has been edited by monster92: 07 February 2012 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Finding and adding odd numbers in a range

Posted 07 February 2012 - 05:53 PM

Ah yes, I wasnt even thinking about that. You should increment the value smallest at the very end of the while loop, that should fix your problem :)
Was This Post Helpful? 0
  • +
  • -

#7 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Re: Finding and adding odd numbers in a range

Posted 07 February 2012 - 05:59 PM

Thank you ianian112 I put back the <= and it works now <3 cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1