Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# using "while" loop to output all odd numbers

Posted on 2003-10-27
Medium Priority
1,090 Views
Hi:
I'm trying to use a while loop to prompt the user to input 2 integers: firstNum & secondNum (firstNum must be less than secondNum).
well, this step is easy for me,
but, i'm having a little bit problem getting the right equation of outputting all odd numbers between these 2 integers.
Here's my codes:

import java.io.*;
import java.util.*;

public class WhileLoops
{

public static void main(String[] args) throws IOException
{
int firstNum, secondNum, n;
int rem;

n = 0;

System.out.flush();

System.out.print("please enter another one that is larger than the first integer: ");
System.out.flush();

rem = firstNum % 2;

while(firstNum < (secondNum - 2))
{
if(rem == 0)
{
firstNum = firstNum + (2*n + 1);
System.out.print(firstNum + " ");
}
else
{
firstNum = firstNum + 2;
System.out.print(firstNum + " ");

}
}
}
}

If i input 3 and 17, the output is "5 7 9 11 13 15", which is correct.
but, it doesn't work when i input 8 and 15, the output becomes "9 10 11 12 13"

I'm not sure which part i have to fix.
0
Question by:crystalfish0318
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 24

Expert Comment

ID: 9625978
Split mind?
0

LVL 86

Accepted Solution

CEHJ earned 80 total points
ID: 9625993
Your code is unnecessarily complex. This should work:

while (++firstNum < secondNum) {
if (firstNum % 2 != 0)
System.out.println(firstNum);
}
0

LVL 86

Expert Comment

ID: 9626067
:-)
0

Author Comment

ID: 9632633
import java.io.*;
import java.util.*;

public class ch5_num8_partB_OddNumbers
{

public static void main(String[] args) throws IOException
{
int firstNum, secondNum, n;
int rem;

n = 0;

System.out.print("Please enter the first integer: ");
System.out.flush();

System.out.print("Please enter the second integer that is larger than "
+ "the first integer: ");
System.out.flush();

rem = firstNum % 2;

while (++firstNum < secondNum)
{
if (firstNum % 2 != 0)
System.out.println(firstNum);
}

}
}

Sorry, how do i make the output horizontal, not vertical?
Another problem is:
if i input 3 and 17,
i want the output to be:
"The odd numbers between 3 and 17 are:
5 7 9 11 15"

how do i do this?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
###### Suggested Courses
Course of the Month9 days, 5 hours left to enroll