Odd And Even Number In Java

Find Odd And Even Number In Java Example

Find Odd And Even Number In Java: In this tutorial, you are going to learn how to Find Odd And Even Numbers In Java. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java. Finding odd and even numbers with Java programming language will help you boost your morale. This will also enhance your problem-solving and critical thinking skills. Later it can be very handy in landing you a good job. If you are not familiar with Arrays in Java then you can head out to its official document and can learn there before you try this program.

Approach 1: Find Odd And Even Number In Java Arrays

Find Odd And Even Number In Java, we will use our first approach to determine the even and odd number.

Algorithm

  1. Start the program
  2. Declare an array size of your choice.
  3. Create a prompt to initialize the array size.
  4. Then, declare an array.
  5. Ask the user to initialize the array elements.
  6. Print the even elements by checking the conditions using for loop.
  7. Stop

You can check the code sample below.

// Java Program to Print the even and odd Element of the Array 
  
import java.io.*; 
import java.util.Scanner; 
  
public class Main 
{ 
    public static void main(String[] args) 
    { 
        //Take input from the user
        Scanner sc=new Scanner(System.in);
        
        int n;    //Declare size of an array
        System.out.println("Enter the size of the array: ");
        n=sc.nextInt();    //Intialize the array size
        
        int arr[]=new int[n];   //Declare an array
        System.out.println("Enter the array elements: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();    //Initialize the array elements
        }
        //Print the even elements
        System.out.println("The Even Elements are...");
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2==0)   //Check whether even or not
            {
                System.out.print(arr[i]+" ");
            }
        }
        System.out.println(" ");
        
        //Print the odd elements
        System.out.println("The Odd Elements are...");
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2!=0)   //check whether odd or not
            {
                System.out.print(arr[i]+" ");
            }
        }
    }
}
Enter the size of the array: 10
Enter the array elements: 1 3 2 4 4 3 1 6 8 9
The Even Elements are...
2 4 4 6 8
The Odd Elements are...
1 3 3 1 9

Program 2: Find Odd And Even Number In Java Arrays

In this approach, we will see how to find the even and odd elements of an array by using separate methods for each of them.

Algorithm

  1. Start
  2. Declare an array size.
  3. Ask the user to initialize the array size.
  4. Declare an array.
  5. Ask the user to initialize the array elements.
  6. Call a method to check for the even elements.
  7. Print the even elements.
  8. Call another method to check for the odd elements.
  9. Print the odd elements.
  10. Stop

You can check the code sample below.

// Java Program to Print the even and odd Element of the Array 
  
import java.io.*; 
import java.util.Scanner; 
  
public class Main 
{ 
    //Driver Method
    public static void main(String[] args) 
    { 
        //Take input from the user
        Scanner sc=new Scanner(System.in);
        
        int n;    //Declare size of an array
        System.out.println("Enter the size of the array: ");
        n=sc.nextInt();    //Intialize the array size
        
        int arr[]=new int[n];   //Declare an array
        System.out.println("Enter the array elements: ");
        for(int i=0;i<n;i++)
        {
            arr[i]=sc.nextInt();    //Initialize the array elements
        }
        System.out.println("The Even Elements are...");
        printEven(arr,n);    //Method to print even elements
        System.out.println(" ");
        System.out.println("The Odd Elements are...");
        printOdd(arr,n);    //Method to print odd elements
           
    } 
    
    //Method to print the even elements
    static void printEven(int arr[], int n)
    {
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2==0)
            {
                System.out.print(arr[i]+" ");
            }
        }
    }
    
    //Method to print the odd elements 
    static void printOdd(int arr[], int n)
    {
        for(int i=0;i<n;i++)
        {
            if(arr[i]%2!=0)
            {
                System.out.print(arr[i]+" ");
            }
        }
    }
}
Output
Enter the size of the array: 10
Enter the array elements: 6 9 8 7 3 4 5 2 1 2
The Even Elements are...
6 8 4 2 2
The Odd Elements are...
9 7 3 5 1

Conclusion

This is it for this short tutorial guys. Hope you like these simple tricks and programs on how to find odd and even number in java. You can also try different methods like you can use while loop or even recursion.

FAQ

How to learn the program step-by-step?

Learning to code nowadays has become a trend. Especially for the younger generations. Why should not be it so? But people often hastily rush to learn to program. If you want to learn efficiently then the first thing you will do is know what you are willing to do. Does this mean that what’s your purpose of learning to program? If you just want to learn it for fun then go ahead and choose the topic of your own. But if you are serious about it and want to land a job as a software engineer then you need to follow some rules of it.
1. Pick any one language and start learning about its basics. Will say start with C programming and it helps you to build your fundamental concepts more clearly.
2. Then once you learn its fundamental things, learn some advanced concepts.
3. Make some mini projects
4. Upgrade yourself too much higher programming languages like C++, Python, Java, JavaScript, Rust
5. Start preparing to learn Data Structure and Algorithms.
And keep learning and applying your knowledge to build live projects.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.