Solved

using "while" loop to output all odd numbers

Posted on 2003-10-27
4
1,014 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 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

23 Experts available now in Live!

Get 1:1 Help Now