While loop to determine if 200 is an even number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AntietamCollector
    New Member
    • Feb 2008
    • 1

    While loop to determine if 200 is an even number

    Hello all, this is my first post on the Forum. I'm trying to learn Java programming, but I'm already stuck. Can someone help me with the practice exercise below? I don't yet understand loops. Thanks very much.

    Write a program that makes an int variable num and sets it equal to some large number (like 200). Then use an if statement in a while loop that prints out "num is an even number" if num is even (hint: you can use (num % 2) == 0 to test if num is even. The % operator is the "modulo" operator, and it basically calculates the remainder when dividing two numbers).
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by AntietamCollect or
    Hello all, this is my first post on the Forum. I'm trying to learn Java programming, but I'm already stuck. Can someone help me with the practice exercise below? I don't yet understand loops. Thanks very much.

    Write a program that makes an int variable num and sets it equal to some large number (like 200). Then use an if statement in a while loop that prints out "num is an even number" if num is even (hint: you can use (num % 2) == 0 to test if num is even. The % operator is the "modulo" operator, and it basically calculates the remainder when dividing two numbers).
    Is 0 = 200 % 1? right? (if any number more than 0 % 1? always 0?)
    Is 0 = 200 % 2? right? (if any number more than 0 % 2? always 0?)
    Is 0 = 200 % 3? wrong?(if any number more than 0 % 3? always 0?)

    You can get some ideas for algo here....

    (hint: you can use (num % 2) == 0 to test if num is even. The % operator is the "modulo" operator, and it basically calculates the remainder when dividing two numbers).

    Actually you can make it without using while loop. and it is at optimized state

    correct me if im wrong,
    sukatoa, Shadow shaman
    Last edited by sukatoa; Feb 25 '08, 01:13 AM. Reason: change something

    Comment

    • hsn
      New Member
      • Sep 2007
      • 237

      #3
      just use % to figure out if the number is even or odd

      Comment

      • farhanm84
        New Member
        • Feb 2008
        • 2

        #4
        Originally posted by AntietamCollect or
        Hello all, this is my first post on the Forum. I'm trying to learn Java programming, but I'm already stuck. Can someone help me with the practice exercise below? I don't yet understand loops. Thanks very much.

        Write a program that makes an int variable num and sets it equal to some large number (like 200). Then use an if statement in a while loop that prints out "num is an even number" if num is even (hint: you can use (num % 2) == 0 to test if num is even. The % operator is the "modulo" operator, and it basically calculates the remainder when dividing two numbers).
        If you are going to check the set of integer numbers then you can go with either while loop or for loop. Since it is given that to check a single integer whether it is an Even or not you can directly assign a value to an integer varibale num and perform the if condition check and then print the output as required.

        Comment

        Working...