How To Print Odd And Even Numbers Using Different Classes In Java Program?

Here we will discuss How To Print Odd And Even Numbers Using Different Classes In Java Programs where we can use one class to find all odd the elements in one class and the rest of all will be even so print the rest of all at different places which will be called as even.

Odd

 Program to Print Odd And Even Numbers Using Different Classes In Java Program?

Here let’s see an example where we can easily see how we can use one class.

import java.util.*;

class ArrayMethod {
    private int[] Array;

    public void Calc(int[] Array) {
        this.Array = Array;
    }

    public int Sum() {
        int sum = 0;
        for (int i = 0; i < Array.length; i++) {
            sum += Array[i];
        }
        return sum;
    }

    public double Average() {
        return Sum() / Array.length;
    }

    public int PrintOdd() {
        for (int i = 0; i < Array.length; i++)
            if (Array[i] % 2 != 0) {
                int Odd = Array[i];

                System.out.println(Odd);
            }
    }

    public int PrintEven() {
        for (int i = 0; i < Array.length; i++)
            if (Array[i] % 2 == 0) {
                int Even = Array[i];
                System.out.println( Even);
            }
    }
}
public class ArrayEX {

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

        ArrayMethod B = new ArrayMethod();
        int[] A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        B.Calc(A);
        System.out.println(" Sum of the numbers inside array is " + B.Sum());
        System.out.println(" Average of the numbers inside array is " + B.Average ());
        System.out.println(" Even numbers " + B.PrintEven());
        System.out.println(" Odd Numebrs " + B.PrintOdd());


    }
}

Here the above example shows how we can segregate the elements on the basis of even and odd by getting the remainder by dividing by two. with the help of classes.

Learn more about How To Print Odd And Even Numbers Using Different Classes In Java Programs at Print odd and even from an array

Also, visit Java Tutorials for learning more about the problems related to java and get the solutions to core java problems that you could encounter during solving Java problems.

Also visit Python’s list: What is the difference Between methods Append and Extend? for learning for about python and other programming languages.

Leave a Comment

%d bloggers like this: