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) ;
}
}
Finding and adding odd numbers in a range
Page 1 of 16 Replies - 200 Views - Last Post: 07 February 2012 - 05:59 PM
Topic Sponsor:
#1
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
Replies To: Finding and adding odd numbers in a range
#2
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?
#3
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.
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.
#4
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.
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.
#5
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.
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
#6
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
#7
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!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|