Solved

using "while" loop to output all odd numbers

Posted on 2003-10-27
4
1,000 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
  • 2
4 Comments
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Split mind?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
Comment Utility
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
Comment Utility
:-)
0
 

Author Comment

by:crystalfish0318
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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 …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now