Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1114
  • Last Modified:

using "while" loop to output all odd numbers

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
crystalfish0318
Asked:
crystalfish0318
  • 2
1 Solution
 
sciuriwareCommented:
Split mind?
0
 
CEHJCommented:
Your code is unnecessarily complex. This should work:

          while (++firstNum < secondNum) {
                if (firstNum % 2 != 0)
                      System.out.println(firstNum);
          }
0
 
CEHJCommented:
:-)
0
 
crystalfish0318Author Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now