Even Odd

Java program to find number is odd or even using array, while, do-while loop, using bit-wise operator

There are different ways to identify number odd or even in Java.

  1. Write a program to find Number is a Odd number or Even number in simple way.
  2. Identify Odd or Even Number in a way is using bit-wise Operator.
  3. Write a program to find Number is a Odd number or Even number using Array.
  4. Write a program to find Number is a Odd number or Even number from 1 to N.
  5. Write a program to find Number is a Odd number or Even number From Given Range.
  6. Write a program to find Number is a Odd or Even from 1 to N using while loop.
  7. Write a program to find Number is a Odd or Even from 1 to N using do-while loop.
import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the number: ");
		int number=scan.nextInt();
		//condition used to identify number is odd or even
		if(number%2==0)
		{
			System.out.println(number + " is Even number");
		}
		else
		{
			System.out.println(number + " is odd number");
		}
	}
}

Output:
Enter the number: 
64
64 is Even number  


  

import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the number: ");
		int number=scan.nextInt();
		//condition used to identify number is odd or even
		if((number/2)*2==number)
		{
			System.out.println(number + " is Even number");
		}
		else
		{
			System.out.println(number + " is odd number");
		}
	}
}
Output:
Enter the number: 
64
64 is Even number


 

  import java.util.Scanner;
public class EvenOdd {
	public static void main(String[] args) {
		int even_count=0,odd_count=0;
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the Array size: ");
		int size=scan.nextInt();
		int [] number=new int[size];
		System.out.println("Enter the elements of an Array: ");
			for(int i=0;i<size;i++)
            {
				number[i]=scan.nextInt();
			}
			//condition used to identify number is odd or even
			for(int i=0; i<size;i++)	
			{
				if(number[i]%2==0)
				{
				even_count++;
				System.out.println(number[i] + " is Even number");
				}
				else
				{
				odd_count++;
				System.out.println(number[i] + " is odd number");
				}
			}
		System.out.println("Even Number Count of an array:- " + even_count);
		System.out.println("Odd Number Count of an array:- " + odd_count);
	}
}

Output:
Enter the Array size: 
6
Enter the elements of an Array: 
15
96
78
35
62
45
15 is odd number
96 is Even number
78 is Even number
35 is odd number
62 is Even number
45 is odd number
Even Number Count of an array:- 3
Odd Number Count of an array:- 3


  

  import java.util.Scanner;
public class EvenOdd {
	public static void main(String[] args) {
		int even_count=0;
		int odd_count=0;
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int n=scan.nextInt();
		//condition used to identify number is odd or even
		for(int i=1; i<=n;i++)	{
			if(i%2==0)
			{
				even_count++;
				System.out.println(i + " is Even number");
			}
			else
			{
				odd_count++;
				System.out.println(i + " is odd number");
			}
		}
		System.out.println("Even count:  "+even_count);
		System.out.println("Odd count:  "+odd_count);
		}
}

Output:
Enter the N: 
10
1 is odd number
2 is Even number
3 is odd number
4 is Even number
5 is odd number
6 is Even number
7 is odd number
8 is Even number
9 is odd number
10 is Even number
Even count:  5
Odd count:  5


  

import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
	{
		int even_count=0,odd_count=0;
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the First Number: ");
		int n1=scan.nextInt();
		System.out.println("Enter the Second Number: ");
		int n2=scan.nextInt();
		//condition used to identify number is odd or even
		System.out.println("Even no List");
		for(int i=n1; i<=n2;i++)	
		{
			if(i%2==0)
			{
				even_count++;
				System.out.println(i);
			}
		}
		System.out.println("Odd no List");
		for(int i=n1; i<=n2;i++)	
		{
			if(i%2!=0)
			{
				odd_count++;
				System.out.println(i);
			}
		}
		System.out.println("Even count:  "+even_count);
		System.out.println("Odd count:  "+odd_count);
	}
}

Output:
Enter the First Number: 
10
Enter the Second Number: 
20
Even no List
10
12
14
16
18
20
Odd no List
11
13
15
17
19
Even count:  6
Odd count:  5


  

  import java.util.Scanner;
public class EvenOdd 
{
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int n=scan.nextInt();
		int i=1;
		//condition used to identify number is odd or even
		System.out.println("Even Numbers:");
		while(i<=n)	
		{
			if(i%2==0)
			{
				System.out.println(i);
			}
			i++;
		}
		System.out.println("Odd Numbers:");
		i=1;
		while(i<=n)	
		{
			if(i%2!=0)
			{
				System.out.println(i);
			}
			i++;
		}
    }
}


Output:
Enter the N: 
7
Even Numbers:
2
4
6
Odd Numbers:
1
3
5
7


  

import java.util.Scanner;
public class EvenOdd {
	public static void main(String[] args) 
	{
		Scanner scan = new java.util.Scanner(System.in);
		System.out.println("Enter the N: ");
		int n=scan.nextInt();
		int i=1;
		//condition used to identify number is odd or even
		System.out.println("Even Numbers:");
		do	
		{
			if(i%2==0)
			{
				System.out.println(i);
			}
			i++;
		}while(i<=n);
		System.out.println("Odd Numbers:");
		i=1;
		do
		{
			if(i%2!=0)
			{
				System.out.println(i);
			}
			i++;
		}	while(i<=n);
    }
}


Output:
Enter the N: 
6
Even Numbers:
2
4
6
Odd Numbers:
1
3
5
 
  

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32