1. Member
Join Date
Feb 2011
Posts
71
Rep Power
0

## how to go about doing this?

Write a Java program that will ask the user for a starting number, and ending number, and an increment number. The program will then count upward, from the starting number up to and including the ending number, by the increment number, displaying each number on a separate line. Also, for each displayed number, the program will say whether the number is even or odd. This program will require the use of the “while” loop as well as the “if/else” statement. Examine the sample outputs carefully in order to structure your code in a logical manner.

I can do all the coding, I just have no idea how to use the mix of loop and if/else to get it done. Can anyone help me out with that.

2. Sure Can you show that you wrote?

3. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
here is the psudo code for what I'm thinking it should look like

[code]
while (number < end)
if even
{
print number even
print number
number + increment
}
else
{
print number odd
print number
number + increment
}

4. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
not sure how to tell if the number is even or odd and where to put the print out statement that the number is even or odd with out it being in the loop as well

5. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
okay, each number it prints out, it also has to say if the number is even or odd.

6. It is really easy.
see
Java Code:
```package action;

public class EvenOrOdd {
public static void main(String[] arg) {
for (int i=0;i!=10;++i) {
boolean isEven = i % 2 == 0;
if (isEven) {
System.out.println("Number " + i + " is even");
} else {
System.out.println("Number " + i + " is odd");
}
}
}
}```

7. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
here is the example we are given

Please enter a starting number: 3
Please enter an ending number: 19
Please enter an increment number: 3
The number 3 is odd
The number 6 is even
The number 9 is odd
The number 12 is even
The number 15 is odd
The number 18 is even
Thanks for playing!

8. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
oh crap, your right, I completly forgot to think of boolean

9. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
I gave the wrong pseudo code, I need to format it to look like the example

10. So Have you solved this problem?

11. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
this is what I have so far.

Java Code:
```import java.ulti.Scanner

public class Lab7
{
public static void main(String [] args)
{
int start;
int end;
int increment;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a starting number");
start = keyboard.nextInt();
System.out.println("Enter a ending number");
end = keyboard.nextInt();
System.out.println("Enter an increment number");
increment = keyboard.nextInt();
while (int i=0;i!=10;++i)
{
boolean even = i % 2 == 0;
if (Even)
{
System.out.println("Number " + start + " is even");
}
else
{
System.out.println("Number " + sstart + " is odd");
}

++i
}
system.out.println("Thank you for playing!")
}
}```
mind letting me know if I'm on the right track?

12. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
sry double post
Last edited by jjth39347; 03-16-2011 at 03:50 PM. Reason: double post

13. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
okay I think I almost got it. but it only shows the start number, anyone care to tell me why?

Java Code:
```import java.util.Scanner;

public class Lab7
{
public static void main(String [] args)
{
int start;
int end;
int increment;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a starting number");
start = keyboard.nextInt();
System.out.println("Enter a ending number");
end = keyboard.nextInt();
System.out.println("Enter an increment number");
increment = keyboard.nextInt();
while (start==0);
{
boolean even = start % 2 == 0;
if (even)
{
while (start==0);
System.out.println("Number " + start + " is even");
start = start + increment;
}
else
{
while (start==0);
System.out.println("Number " + start + " is odd");
start = start + increment;
}

}
System.out.println("Thank you for playing!");
}
}```

14. see
Java Code:
```import java.util.Scanner;

public class EvenOrOdd {
public static void main(String[] arg) {
int start;
int end;
int increment;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a starting number");
start = keyboard.nextInt();
System.out.println("Enter a ending number");
end = keyboard.nextInt();
System.out.println("Enter an increment number");
increment = keyboard.nextInt();
if (start > end || increment <= 0) {
throw new IllegalArgumentException("start < end || increment <= 0");
}
while (start < end) {
boolean even = start % 2 == 0;
if (even) {
System.out.println("Number " + start + " is even");
} else {
System.out.println("Number " + start + " is odd");
}
start += increment;
}
System.out.println("Thank you for playing!");
}
}```

15. Member
Join Date
Feb 2011
Posts
71
Rep Power
0
Thank you, I see where I messed up. Works perfectly now.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•