Java Odd or Even

  1. Check Given number is Odd or Even in Java
  2. Find Odd numbers between two numbers in Java
  3. Find Even numbers between two numbers in Java
  4. Calculate total number of Odd and Even numbers in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Check Given number is Odd or Even in Java


OddEven_Ex1.java

import java.util.Scanner;
 
public class OddEven_Ex1 {
	
	Scanner scan;
	int num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Check Odd or Even");
		
		System.out.println("\nEnter the number : ");
		num = Integer.parseInt(scan.nextLine());
	}
	
	void check() {
		
		if((num % 2) == 0) 
			System.out.println(num + " is Even number");
		
		else
			System.out.println(num + " is Odd number");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		OddEven_Ex1 obj = new OddEven_Ex1();
		
		obj.getVal();
		obj.check();
	}
}

Sample Output

Check Odd or Even

Enter the number : 
111
111 is Odd number
Check Odd or Even

Enter the number : 
120
120 is Even number




B. Find Odd numbers between two numbers in Java


OddEven_Ex2.java

import java.util.Scanner;
 
public class OddEven_Ex2 {
	
	Scanner scan;
	int start, end;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Find Odd numbers between two numbers");
		
		System.out.println("\nEnter the Starting value : ");
		start = Integer.parseInt(scan.nextLine());
 
		System.out.println("Enter the End value : ");
		end = Integer.parseInt(scan.nextLine());
	}
	
	void check() {
		
		System.out.println();
 
		for(int i=start; i<=end; i++) {
			
			if((i % 2) != 0) 
				System.out.println(i + " is Odd number");
		}		
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		OddEven_Ex2 obj = new OddEven_Ex2();
		
		obj.getVal();
		obj.check();
	}
}

Sample Output

Find Odd numbers between two numbers

Enter the Starting value : 
25
Enter the End value : 
38

25 is Odd number
27 is Odd number
29 is Odd number
31 is Odd number
33 is Odd number
35 is Odd number
37 is Odd number




C. Find Even numbers between two numbers in Java


OddEven_Ex3.java

import java.util.Scanner;
 
public class OddEven_Ex3 {
	
	Scanner scan;
	int start, end;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Find Even numbers between two numbers");
		
		System.out.println("\nEnter the Starting value : ");
		start = Integer.parseInt(scan.nextLine());
 
		System.out.println("Enter the End value : ");
		end = Integer.parseInt(scan.nextLine());
	}
	
	void check() {
		
		System.out.println();
		
		for(int i=start; i<=end; i++) {
			
			if((i % 2) == 0) 
				System.out.println(i + " is Even number");
		}		
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		OddEven_Ex3 obj = new OddEven_Ex3();
		
		obj.getVal();
		obj.check();
	}
}

Sample Output

Find Even numbers between two numbers

Enter the Starting value : 
25
Enter the End value : 
38

26 is Even number
28 is Even number
30 is Even number
32 is Even number
34 is Even number
36 is Even number
38 is Even number




D. Calculate total number of Odd and Even numbers in Java


OddEven_Ex4.java

import java.util.Scanner;
 
public class OddEven_Ex4 {
	
	Scanner scan;
	int start, end;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Calculate total number of " +
				"Odd & Even numbers between two numbers");
		
		System.out.println("\nEnter the Starting value : ");
		start = Integer.parseInt(scan.nextLine());
 
		System.out.println("Enter the End value : ");
		end = Integer.parseInt(scan.nextLine());
	}
	
	void check() {
		
		int odd = 0;
		int even = 0;
		
		for(int i=start; i<=end; i++) {
			
			if((i % 2) == 0) 
				even ++;
			else
				odd ++;
		}	
		
		System.out.println("\nTotal number of Odd number is " + odd);
		System.out.println("Total number of Even number is " + even);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		OddEven_Ex4 obj = new OddEven_Ex4();
		
		obj.getVal();
		obj.check();
	}
}

Sample Output

Calculate total number of Odd & Even numbers between two numbers

Enter the Starting value : 
25
Enter the End value : 
38

Total number of Odd number is 7
Total number of Even number is 7






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com