Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Program help with counting even and odd integers in input value.

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program help with counting even and odd integers in input value.

    import java.util.Scanner;
    public class Integer {
    //--------------------------------------------------------------------
    // Read a value from the user and counts the number of odd and even integers in the value
    // -------------------------------------------------------------------
    public static void main (String[ ] args) {
    int integer, oddinteger, eveninteger;
    Scanner scan = new Scanner (System.in);
    System.out.print (“Enter an integer: ”)

    What I need the program to do is to read the value input by the user and identify the number of even and odd integers in the value. For example, if the user inputs 403 I need the program to output : Odd Integers: 2 Even Integers: 1.

    Does that make sense?
    Please help me! Thank you in advance. I'm fairly new at this.
    Last edited by helloworld922; July 3rd, 2010 at 11:28 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,928
    Thanks
    55
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Program help with counting even and odd integers in input value.

    Do you have the algorithm yet for extracting a digit from a number? You need to figure out how to get 3 digits out of 403.
    This will involve modulus and division.
    Then look at the digits one by one to determine if they are odd or even.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: Program help with counting even and odd integers in input value.

    To be exact, the code to do this would look like this:

    int num1 = 6;
     
    if(num1%2==0)
       System.out.print("Even Number");
     
    if(num1%2==1)
       System.out.print("Odd Number");

    Doing "int % 2" will give you either the number 0 or 1. If the result is 0, it means it is even. If the result is 1, it means it is odd.

    Basically, mod ( % ) will grab the two number, divide them, and return the remainder. If you divide 4 by 2, you get 2 with a remainder of 0, so it returns 0. If you divide 3 by 2, you get 1 with a remainder of 1, so it returns 1.

    You can do the same thing if you needed to find out if a number was divisible by something. For instance, if you needed to know if 40 was divisible by 10, you would simply code "40%10" and it would return 0, which would indicate that it is divisible by 0. If you did "45%10" it would return 5, because 5 is the remainder of 45/10.

    I dont think I can explain it any more detailed then that. Once you get used to using mod, you will find out how extremely useful it is.

Similar Threads

  1. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 04:40 PM
  2. Counting Vowels and Prepositions in a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 27th, 2010, 08:36 PM
  3. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 05:44 AM
  4. Simple Input/Output program Acting weird
    By drexasaurus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 02:15 PM
  5. Replies: 9
    Last Post: June 27th, 2009, 05:05 PM