• We just launched and are currently in beta. Join us as we build and grow the community.

Classify Numbers in Java

yopol1234567

SQL Injector
Y Rep
0
0
0
Rep
0
Y Vouches
0
0
0
Vouches
0
Posts
50
Likes
42
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
The following Java program application counts the number of odd and even numbers. The program also counts the number of zeros. I will be using the JCreator IDE in developing the program.

To start in this tutorial, first open the JCreator IDE, click new and paste the following code.

  1. import

    java.util.*

    ;
  2. public

    class

    ClassifyNumbers
  3. {
  4. static

    Scanner console =

    new

    Scanner (

    System

    .in

    )

    ;

  5. static

    final

    int

    N =

    20

    ;

  6. public

    static

    void

    main (

    String

    [

    ]

    args)
  7. {
  8. //declare variables

  9. int

    counter;

    //loop control variable
  10. int

    number;

    //variable to store the new number

  11. int

    zeros =

    0

    ;
  12. int

    odds =

    0

    ;
  13. int

    evens =

    0

    ;

  14. System

    .out

    .println

    (

    "Please enter "

    +

    N
  15. +

    " integers, positive, negative, or zeros: "

    )

    ;

  16. for

    (

    counter =

    1

    ;

    counter <=

    N;

    counter++

    )
  17. {
  18. number =

    console.nextInt

    (

    )

    ;
  19. System

    .out

    .print

    (

    number +

    " "

    )

    ;

  20. switch

    (

    number %

    2

    )
  21. {
  22. case

    0

    :

    evens++;
  23. if

    (

    number ==

    0

    )
  24. zeros++;
  25. break

    ;
  26. case

    1

    :
  27. case

    -

    1

    :

    odds++;
  28. }
  29. }
  30. System

    .out

    .println

    (

    )

    ;
  31. System

    .out

    .println

    (

    "There are "

    +

    evens +

    "evens "
  32. +

    "which also includes "
  33. +

    zeros +

    " zeros"

    )

    ;
  34. System

    .out

    .println

    (

    "Total number of odds is: "

    +

    odds)

    ;
  35. }
  36. }

Sample run:

Please enter 20 integers, positive, negative, or zeros:
1 2 3 4 5 6 7 8 9 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 0
1 2 3 4 5 6 7 8 9 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 0
There are 10evens which also includes 2 zeros
Total number of odds is: 10

The program work as follows:

The statement
  1. System

    .out

    .println

    (

    "Please enter "

    +

    N
  2. +

    " integers, positive, negative, or zeros: "

    )

    ;

    prompts the user to enter integers, positive, negative or zero numbers.

  3. The

    statement <

    java number =

    console.nextInt

    (

    )

    ;
  4. System

    .out

    .print

    (

    number +

    " "

    )

    ;

gets the next number and outputs the number.

The statement for

(

counter =

1

;

counter <=

N;

counter++

)

process and analyze the 20 numbers.

The statement
  1. System

    .out

    .println

    (

    "There are "

    +

    evens +

    "evens "
  2. +

    "which also includes "
  3. +

    zeros +

    " zeros"

    )

    ;
  4. System

    .out

    .println

    (

    "Total number of odds is: "

    +

    odds)

    ;

print the result and output the values of the variables zeros, evens and odds.

 

452,292

323,526

323,535

Top