Home > Enterprise >  How do I display the numbers that belong to the positive,negative,odd and even groups?
How do I display the numbers that belong to the positive,negative,odd and even groups?

Time:10-15

This program asks for 10 integer numbers and stores these integer numbers in an array. The program then counts how many odd, how many even, how many negative, how many positive, and how many zero numbers. The program should also display what are the numbers that belong in these groups.

this is my program:

import java.util.Scanner;
 public class count { 
public static void main (String[] args)
        {           
            int i,x,a[] = new int[10];          
            int n=0,p=0,e=0,o=0,z=0;            
            Scanner input = new Scanner(System.in);
                        System.out.println("Enter 10 Numbers");         
            for(i=0;i<10;i  )
            {
             System.out.println("Enter Number :");
             a[i] = input.nextInt();
            }           
            for(i=0;i<10;i  )
            {
                if(a[i]<0)
                    n  ;
                else
                    p  ;

                x=a[i]%2;
                if(x==0)
                    e  ;
                else
                    o  ;
                                if(a[i]==0)
                                    z  ;
            }

            //print
            System.out.println("Total Even Numbers = " e);
                        System.out.println("Even Numbers are = ");
            System.out.println("Total Odd Numbers = " o);
                         System.out.println("Odd Numbers are = ");
            System.out.println("Total Negative Numbers = " n);
                         System.out.println("Negative Numbers are = ");
            System.out.println("Total Positive Numbers = " p);
                         System.out.println("Positive Numbers are = ");
                        System.out.println("Total Zero Numbers = " z);
        }
}  

CodePudding user response:

This is the output of it.

Enter 10 Numbers Enter Number : 0 Enter Number : 0 Enter Number : 2 Enter Number :

Enter Number :

Enter Number : 6 Enter Number : 9 Enter Number : 5 Enter Number : 0 Enter Number : 9 Total Even Numbers = 6 Even Numbers are = Total Odd Numbers = 4 Odd Numbers are = Total Negative Numbers = 2 Negative Numbers are = Total Positive Numbers = 8 Positive Numbers are = Total Zero Numbers = 3

CodePudding user response:

As I said in the comments a simple solution will be to save every number to the corresponding List

List<Integer> evenNumbers = new ArrayList<>();

Then every time you check each number with the if statements instead of having a counter just add the number to the list

if (numbers[i] % 2 == 0) evenNumbers.add(numbers[i]);

And last print them as you already do but check the size of each List

System.out.println("Total Even Numbers = "   evenNumbers.size());
System.out.println("Even Numbers are = "   evenNumbers);
  • Related