Solved

using "while" loop to output all odd numbers

Posted on 2003-10-27
4
1,082 Views
Last Modified: 2007-12-19
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
{
      static BufferedReader keyboard =
                        new BufferedReader(new InputStreamReader(System.in));
      
      public static void main(String[] args) throws IOException
      {
            int firstNum, secondNum, n;
            int rem;
            
            n = 0;
            
            System.out.print("please enter an integer: ");
            System.out.flush();
            firstNum = Integer.parseInt(keyboard.readLine());
            
            System.out.print("please enter another one that is larger than the first integer: ");
            System.out.flush();
            secondNum = Integer.parseInt(keyboard.readLine());
            
            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
Comment
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
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 9625978
Split mind?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 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

by:CEHJ
ID: 9626067
:-)
0
 

Author Comment

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

public class ch5_num8_partB_OddNumbers
{
      static BufferedReader keyboard =
                        new BufferedReader(new InputStreamReader(System.in));
      
      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();
            firstNum = Integer.parseInt(keyboard.readLine());
            
            System.out.print("Please enter the second integer that is larger than "
                                 + "the first integer: ");
            System.out.flush();
            secondNum = Integer.parseInt(keyboard.readLine());
            
            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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question