Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
4.2 kB
2
Indexable
Never
import java.util.Scanner;

public class A{
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);

        System.out.println("Enter size of the array:");
        int size = cin.nextInt();

        System.out.println("Enter the contents of the array");

        int[] arr = new int[size];

        int[] odd = new int[size];
        int[] even = new int [size];
        int oddCounter=0;
        int evenCounter=0;

        for(int i=0; i<size; i++){
            System.out.print(i+1+" :");
            arr[i]=cin.nextInt();
        }

        System.out.println("The contents of the array are");
        for(int i=0; i<size; i++){
            System.out.print(arr[i]+" ");
        }
        
        System.out.println("\n------------------------------------");

        System.out.println("These are all of the odd numbers: ");

        for(int i =0; i<size; i++){
            if(arr[i]%2!=0){
                System.out.print(arr[i]+" ");
                oddCounter++;
            }
        }

        for(int i = 0; i<size; i++){
            if((arr[i])%2!=0){
                odd[i]=arr[i];
            }
        }

        System.out.println("\nThese are all of the even numbers:");

        for(int i =0; i<size; i++){
            if(arr[i]%2==0){
                System.out.print(arr[i]+" ");
                even[i]=arr[i];
                evenCounter++;
            }
        }

        int[] finalOdd = new int[oddCounter];
        int[] finalEven = new int [evenCounter];

        System.out.println("Final odd inside finalOdd[]: ");
        for(int i = 0; i<oddCounter; i++){
            finalOdd[i]=odd[i];
            System.out.println(finalOdd[i]);
        }

        System.out.println("Final even inside finalEven[]: ");
        for(int i = 0; i<evenCounter; i++){
            finalEven[i]=even[i];
            System.out.println(finalEven[i]);
        }

        System.out.println("\n------------------------------------");

        int sum=0;
        for(int i =0 ; i<size; i++){
            sum+=arr[i];
        }

        int average = sum/size;
        int largestNum = findMaxNum(arr);
        int largestOdd = findMaxNum(odd);
        int largestEven = findMaxNum(even);
        int smallestEven = findMinNum(even);
        int oddAverage = findAverage(finalOdd);
        int evenAverage = findAverage(finalEven);




        System.out.println("The average of all numbers in the array: "+ average);
        System.out.println("The difference between the largest number in the array and the average of all numbers is: "+(largestNum-average));

        System.out.println("------------------------------------");

        System.out.println("The average of all odd numbers is: "+oddAverage);
        System.out.println("The average of all even numbers is: "+evenAverage);

        System.out.println("The difference of the average of odd and average of even is: "+(oddAverage-evenAverage));

        System.out.println("------------------------------------");

        System.out.println("The largest odd number is: "+largestOdd);
        System.out.println("The largest even number is: "+largestEven);
        System.out.println("The smallest even number is: "+smallestEven);

        System.out.println("The difference between the largest odd number and the smallest even number is: "+(largestOdd - smallestEven));
    
    }

    private static int findMaxNum(int[] num) {
        int maxNum = num[0];
        for(int i=1;i < num.length;i++){
          if(num[i] > maxNum){
            maxNum = num[i];
          }
        }
        return maxNum;
    }

    private static int findMinNum(int[] num) {
        int minNum = num[0];
        for(int i=1;i < num.length;i++){
          if(num[i] < minNum){
            minNum = num[i];
          }
        }
        return minNum;
    }


    private static int findAverage(int[] num) {
        int length = num.length;
        int sum = 0;

        for (int i=0; i< length; i++)
           sum += num[i];
     
        return sum/length;
    
    }


}