Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By JosAH

Thread: Odd and Even Numbers

  1. #1
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Question Odd and Even Numbers

    Ive faced a problem here ,
    user enters some numbers , programs shows how many of them are odd and how many are even.
    here is what i have so far :

    Java Code:
    import java.util.Scanner;
    
    public class OddEven {
        public static void main (String [] args){
            int evenCount=0;int oddCount=0;
            Scanner sc=new Scanner(System.in);
            System.out.print("enter 10 numbers : ");
            
           int input=sc.nextInt();
           for(int i=1;i<input;i++){
               
                    if (i%input==0){
                    evenCount++;
                
                }
                else {
                oddCount++;
                }
              
                      }  
           
            
                System.out.print("u have entered "+evenCount+" even numbers");
                
            }
                  
                }
    each time i run the program (testing only even numbers) , i get 0. is there something wrong with my counter?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Odd and Even Numbers

    Where do you change the value of the the variable: input inside of the loop?

    Rethink your logic to get a new input to test.

    How many numbers were you able to enter for the program to read when you executed the program?
    This should have told you there was a logic problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Odd and Even Numbers

    Quote Originally Posted by Norm View Post
    Where do you change the value of the the variable: input inside of the loop?

    Rethink your logic to get a new input to test.

    How many numbers were you able to enter for the program to read when you executed the program?
    This should have told you there was a logic problem.
    lets say im trying to enter following numbers : 10 20 25 23 44 8 3 7 39 56 . without having to ask user to enter each number separately ,ive modified the code into this :

    Java Code:
     public class OddEven {
        public static void main (String [] args){
            int evenCount=0;int oddCount=0;
            Scanner sc=new Scanner(System.in);
            System.out.print("enter 10 numbers : ");
            
           int input=sc.nextInt();
           for(int i=1;i<50;i++){
                
                    if (i%input==0){
                    evenCount++;
                 
                }
                else {
                oddCount++;
                }
               
                      }  
            
             
                System.out.print("u have entered "+evenCount+" even numbers");
                 
            }
                   
                }
    but the output is not the exact count of even numbers.
    Last edited by pinkdreammsss; 04-13-2012 at 01:40 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Odd and Even Numbers

    Where do you change the value of the variable: input inside of the loop?

    Add a println statement inside of the loop immediately after the for statement (line 9) that prints out the value of the input variable so you can see what the computer sees when it executes the code.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Odd and Even Numbers

    A question for you: why do you loop fifty times? And why do you only read a number once? I don't understand your program logic and I think it is completely wrong.

    kind regards,

    Jos
    Everyman likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Odd and Even Numbers

    Quote Originally Posted by JosAH View Post
    A question for you: why do you loop fifty times? And why do you only read a number once? I don't understand your program logic and I think it is completely wrong.

    kind regards,

    Jos
    for the loop, i know the problem is the logic , my loop should generate a range of numbers, say from 1 to 100, so :if x%rangeOfnumbers=0 then count++; but the loop im using is not doing so.

    also, user enters all the numbers at once so program wouldnt have to prompt for user input multiple times. e.g: 2 5 7 9 10 38. notice the space between each number.
    Last edited by pinkdreammsss; 04-13-2012 at 03:16 PM.

  7. #7
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Odd and Even Numbers

    That won't work... you need to split your input first for having separate numbers... try e.g. the split() function of the String class and loop through the resulting array of single numbers the user entered. ;)
    Like that you try this: if ( 1 % "1 7 9 8" == 0)... 1 dividefd by a generic String won't work very well...

  8. #8
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Odd and Even Numbers

    Quote Originally Posted by Sierra View Post
    That won't work... you need to split your input first for having separate numbers... try e.g. the split() function of the String class and loop through the resulting array of single numbers the user entered. ;)
    Like that you try this: if ( 1 % "1 7 9 8" == 0)... 1 dividefd by a generic String won't work very well...
    thanks for the tip , tho im trying to do this without using arrays.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Odd and Even Numbers

    The call to nextInt() should work with all the numbers entered on one line. However if you do not call nextInt inside the loop you will not get the next number.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Odd and Even Numbers

    Sorry, my fault, Norm, you are right... you just need it inside the loop. :)

  11. #11
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Odd and Even Numbers

    Quote Originally Posted by Norm View Post
    The call to nextInt() should work with all the numbers entered on one line. However if you do not call nextInt inside the loop you will not get the next number.
    ive found a sample for calling nextInt() in the loop, but it's array , and i dont want an array.


    Java Code:
    for (int i = 0; i < input ; i++ )
    
        {
    
            numbers[i] = input.nextInt();
    any idea how can i do this without using array?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Odd and Even Numbers

    You don't need an array. Just move the assignment to input inside the loop. Use the hasNext() method to detect the end of input data.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Odd and Even Numbers

    Quote Originally Posted by Norm View Post
    You don't need an array. Just move the assignment to input inside the loop. Use the hasNext() method to detect the end of input data.
    is this what u meant? gives me the error incompatible types.

    Java Code:
    for(int i=0;i<100;i++){
             int input=sc.hasNext();   
                    if (i%input==0){
                    evenCount++;
                 
                }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Odd and Even Numbers

    You need to read the API doc for the Scanner class for the nextInt() and for the hasNext() methods.
    They return different values and are used for different purposes.


    I said: Use the hasNext() method to detect the end of input data.
    Not to read values from the user.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Odd and Even Numbers

    Quote Originally Posted by Norm View Post
    I said: Use the hasNext() method to detect the end of input data.
    Not to read values from the user.
    how am i suppose to detect the end of input data? any example?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Odd and Even Numbers

    Did you look at the API doc for the hasNext() or hasNextInt() methods? The return a boolean that can be tested. Might be a problem with blocking to wait for input.

    pseudo code
    begin loop
    is there more input - if not exit the loop
    read the input
    process the input
    end loop


    You might want to use a "sentinel" value the user types in to tell you that there is no more input.

    Or read in the whole line of numbers with nextLine() and work through them either using another instance of Scanner.
    Here's a sample:
    Java Code:
          Scanner scnr = new Scanner("1 2  3 4 5\n 6 8 9 0");
          while(scnr.hasNextInt()) {
             System.out.print(scnr.nextInt() + " ");  // 1 2 3 4 5 6 8 9 0 
          }
    Last edited by Norm; 04-14-2012 at 01:34 PM.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    pinkdreammsss is offline Member
    Join Date
    Apr 2009
    Posts
    84
    Rep Power
    0

    Default Re: Odd and Even Numbers

    i figured out the problem with my program,
    i had to move
    Java Code:
    int input=sc.nextInt();
    in a for loop so it'll keep count of numbers entered by user ,
    e.g:
    Java Code:
    for (int i=0;i<10;i++){ // the loop keeps count of entered numbers,
    int input=sc.nextInt();// here after each time user enters a numbers , loop count 1 , until 10 numbers are entered.
     }
    the rest is up to if statement that calculates how many odd n even numbers are there. here's the final program:
    Java Code:
    import java.util.Scanner;
    
    public class oddeven {
        public static void main (String [] args){
            int evenCount=0;int oddCount=0;
            Scanner sc=new Scanner(System.in);
            System.out.print("enter 10 numbers : ");
            
        
           for(int i=0;i<10;i++){
            int input=sc.nextInt();  
                
                    if (input%2==0){
                    evenCount++;
                
                }
                else 
                oddCount++;
                
              
                      
           }
            
                System.out.print("u have entered "+evenCount+" even numbers and "+oddCount+" odd numbers");
                
            }
                  
                }

Similar Threads

  1. how to change numbers into word numbers?
    By akeni in forum New To Java
    Replies: 13
    Last Post: 11-18-2011, 08:46 AM
  2. Help with incrementing numbers in a GUI
    By jdg951 in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 03:23 AM
  3. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  4. Sum of 4 numbers...!?
    By xmenus in forum New To Java
    Replies: 7
    Last Post: 02-14-2010, 03:36 PM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM

Tags for this Thread

Posting Permissions

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