CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    Reading odd and even integers

    I'm trying to write a program in for school that will read whether a number is odd or even, and I just can't get anything to work.
    I'm guessing I need if statements but I can't figure out exactly what, this is what I have so far...

    import javax.swing.JOptionPane;

    public class Even {
    public static void main (String args[])
    {
    String num1;
    int n1,odd,even;

    num1 = JOptionPane.showInputDialog("Please type in an integer:");

    n1 = Integer.parseInt(num1);
    odd = ? ;
    even = ? ;


    if( ? )JOptionPane.showMessageDialog(
    null,"The number is "+odd,"Results",JOptionPane.INFORMATION_MESSAGE);
    else JOptionPane.showMessageDialog(
    null,"The number is "+even,"Results",JOptionPane.INFORMATION_MESSAGE);

    System.exit (0);
    }
    }

    The ?'s are where I', completely lost, someone Please help!!!


  2. #2
    Join Date
    Sep 1999
    Location
    Upstate NY
    Posts
    6

    Re: Reading odd and even integers

    OK, get rid of even and odd completly. in your if statement, divide n1 by 2. if the remainder is 0 n1 is even. if the remainder is anything other than 0, it is odd. use the remainder operator (%) to do the division.

    like this

    if((n1%2)==0)
    n1 is even
    else
    n1 is odd




    hope this helped, and disregard my other reply



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured