Find the even product pairs in an array

Here given code implementation process.

/*
  Java program
  Find the even product pairs in an array 
*/
import java.util.ArrayList;
public class Products
{
	//This function are displaying given array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + arr[i]);
		}
		System.out.print("\n");
	}
	// Find pairs of even product in an array
	public void evenProduct(int[] arr)
	{
		// Get the length
		int n = arr.length;
		boolean status = false;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		int k = 0;
		int l = 0;
		// Use to collect element
		ArrayList < Integer > oddValue = new ArrayList < Integer > ();
		ArrayList < Integer > evenValue = new ArrayList < Integer > ();
		// Collecting even and odd value in given array
		for (i = 0; i < n; i++)
		{
			if (arr[i] % 2 == 0)
			{
				evenValue.add(arr[i]);
			}
			else
			{
				oddValue.add(arr[i]);
			}
		}
		// Get the length of even and odd value
		k = evenValue.size();
		l = oddValue.size();
		// reset i index value
		i = 0;
		System.out.print("\n Array : \n");
		display(arr, n);
		System.out.print(" Even Product : \n");
		// Find product of even and odd elements
		while (i < k && j < l)
		{
			// Display pair
			System.out.print(" (" + evenValue.get(i) + ", " + oddValue.get(j) + ") \n");
			status = true;
			// Increase the index value
			i++;
			j++;
		}
		// Display remaining even element
		while (i + 1 < k)
		{
			status = true;
			// Display pair
			System.out.print(" ( " + evenValue.get(i) + ", " + evenValue.get(i + 1) + " ) ");
			i += 2;
		}
		if (status == false)
		{
			System.out.print("\n None \n");
		}
	}
	public static void main(String[] args)
	{
		Products task = new Products();
		// Define array of integer elements
		int[] arr = {
			5 , 2 , 7 , 3 , 4 , 2 , 9 , 8 , 1 , 1 , 10
		};
		// Test case
		task.evenProduct(arr);
	}
}

Output

 Array :
 5 2 7 3 4 2 9 8 1 1 10
 Even Product :
 (2, 5)
 (4, 7)
 (2, 3)
 (8, 9)
 (10, 1)
// Include header file
#include <iostream>
#include <vector>
using namespace std;
/*
  C++ program
  Find the even product pairs in an array 
*/
class Products
{
	public:
		//This function are displaying given array elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " " << arr[i];
			}
			cout << "\n";
		}
	// Find pairs of even product in an array
	void evenProduct(int arr[], int n)
	{
		
		bool status = false;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		int k = 0;
		int l = 0;
		// Use to collect element
		vector <int> oddValue;
		vector <int> evenValue;
		// Collecting even and odd value in given array
		for (i = 0; i < n; i++)
		{
			if (arr[i] % 2 == 0)
			{
				evenValue.push_back(arr[i]);
			}
			else
			{
				oddValue.push_back(arr[i]);
			}
		}
		// Get the length of even and odd value
		k = evenValue.size();
		l = oddValue.size();
		// reset i index value
		i = 0;
		cout << "\n Array : \n";
		this->display(arr, n);
		cout << " Even Product : \n";
		// Find product of even and odd elements
		while (i < k && j < l)
		{
			// Display pair
			cout << " (" << evenValue.at(i) << ", " << oddValue.at(j) << ") \n";
			status = true;
			// Increase the index value
			i++;
			j++;
		}
		// Display remaining even element pair
		while (i + 1 < k)
		{
			status = true;
			// Display pair
			cout << " ( " << evenValue.at(i) << ", " << evenValue.at(i + 1) << " ) ";
			i += 2;
		}
		if (status == false)
		{
			cout << "\n None \n";
		}
	}
};
int main()
{
	Products task = Products();
	// Define array of integer elements
	int arr[] = {
		5 , 2 , 7 , 3 , 4 , 2 , 9 , 8 , 1 , 1 , 10
	};
  	// Get the length
	int n = sizeof(arr) / sizeof(arr[0]);
	// Test case
	task.evenProduct(arr,n);
	return 0;
}

Output

 Array :
 5 2 7 3 4 2 9 8 1 1 10
 Even Product :
 (2, 5)
 (4, 7)
 (2, 3)
 (8, 9)
 (10, 1)
// Include namespace system
using System;
using System.Collections.Generic;
/*
  C# program
  Find the even product pairs in an array 
*/
public class Products
{
	//This function are displaying given array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	// Find pairs of even product in an array
	public void evenProduct(int[] arr)
	{
		// Get the length
		int n = arr.Length;
		Boolean status = false;
		// Loop controlling variables
		int i = 0;
		int j = 0;
		int k = 0;
		int l = 0;
		// Use to collect element
		List < int > oddValue = new List < int > ();
		List < int > evenValue = new List < int > ();
		// Collecting even and odd value in given array
		for (i = 0; i < n; i++)
		{
			if (arr[i] % 2 == 0)
			{
				evenValue.Add(arr[i]);
			}
			else
			{
				oddValue.Add(arr[i]);
			}
		}
		// Get the length of even and odd value
		k = evenValue.Count;
		l = oddValue.Count;
		// reset i index value
		i = 0;
		Console.Write("\n Array : \n");
		display(arr, n);
		Console.Write(" Even Product : \n");
		// Find product of even and odd elements
		while (i < k && j < l)
		{
			// Display pair
			Console.Write(" (" + evenValue[i] + ", " + oddValue[j] + ") \n");
			status = true;
			// Increase the index value
			i++;
			j++;
		}
		// Display remaining even element pair
		while (i + 1 < k)
		{
			status = true;
			// Display pair
			Console.Write(" ( " + evenValue[i] + ", " + evenValue[i + 1] + " ) ");
			i += 2;
		}
		if (status == false)
		{
			Console.Write("\n None \n");
		}
	}
	public static void Main(String[] args)
	{
		Products task = new Products();
		// Define array of integer elements
		int[] arr = {
			5 , 2 , 7 , 3 , 4 , 2 , 9 , 8 , 1 , 1 , 10
		};
		// Test case
		task.evenProduct(arr);
	}
}

Output

 Array :
 5 2 7 3 4 2 9 8 1 1 10
 Even Product :
 (2, 5)
 (4, 7)
 (2, 3)
 (8, 9)
 (10, 1)
<?php
/*
  Php program
  Find the even product pairs in an array 
*/
class Products
{
	//This function are displaying given array elements
	public	function display( $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Find pairs of even product in an array
	public	function evenProduct( $arr)
	{
		// Get the length
		$n = count($arr);
		$status = false;
		// Loop controlling variables
		$i = 0;
		$j = 0;
		$k = 0;
		$l = 0;
		// Use to collect element
		$oddValue = array();
      	$evenValue = array();
		// Collecting even and odd value in given array
		for ($i = 0; $i < $n; $i++)
		{
			if ($arr[$i] % 2 == 0)
			{
				$evenValue[] = $arr[$i];
			}
			else
			{
				$oddValue[] = $arr[$i];
			}
		}
		// Get the length of even and odd value
		$k = count($evenValue);
		$l = count($oddValue);
		// reset i index value
		$i = 0;
		echo "\n Array : \n";
		$this->display($arr, $n);
		echo " Even Product : \n";
		// Find product of even and odd elements
		while ($i < $k && $j < $l)
		{
			// Display pair
			echo " (". $evenValue[$i] .", ". $oddValue[$j] .") \n";
			$status = true;
			// Increase the index value
			$i++;
			$j++;
		}
		// Display remaining even element pair
		while ($i + 1 < $k)
		{
			$status = true;
			// Display pair
			echo " ( ". $evenValue[$i] .", ". $evenValue[$i + 1] ." ) ";
			$i += 2;
		}
		if ($status == false)
		{
			echo "\n None \n";
		}
	}
}

function main()
{
	$task = new Products();
	// Define array of integer elements
	$arr = array(5, 2, 7, 3, 4, 2, 9, 8, 1, 1, 10);
	// Test case
	$task->evenProduct($arr);
}
main();

Output

 Array :
  5  2  7  3  4  2  9  8  1  1  10
 Even Product :
 (2, 5)
 (4, 7)
 (2, 3)
 (8, 9)
 (10, 1)
/*
  Node Js program
  Find the even product pairs in an array 
*/
class Products
{
	//This function are displaying given array elements
	display(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Find pairs of even product in an array
	evenProduct(arr)
	{
		// Get the length
		var n = arr.length;
		var status = false;
		// Loop controlling variables
		var i = 0;
		var j = 0;
		var k = 0;
		var l = 0;
		// Use to collect element
		var oddValue = []; 
		var evenValue = [];
		// Collecting even and odd value in given array
		for (i = 0; i < n; i++)
		{
			if (arr[i] % 2 == 0)
			{
				evenValue.push(arr[i]);
			}
			else
			{
				oddValue.push(arr[i]);
			}
		}
		// Get the length of even and odd value
		k = evenValue.length;
		l = oddValue.length;
		// reset i index value
		i = 0;
		process.stdout.write("\n Array : \n");
		this.display(arr, n);
		process.stdout.write(" Even Product : \n");
		// Find product of even and odd elements
		while (i < k && j < l)
		{
			// Display pair
			process.stdout.write(" (" + evenValue[i] + ", " + oddValue[j] + ") \n");
			status = true;
			// Increase the index value
			i++;
			j++;
		}
		// Display remaining even element pair
		while (i + 1 < k)
		{
			status = true;
			// Display pair
			process.stdout.write(" ( " + evenValue[i] + ", " + evenValue[i + 1] + " ) ");
			i += 2;
		}
		if (status == false)
		{
			process.stdout.write("\n None \n");
		}
	}
}

function main()
{
	var task = new Products();
	// Define array of integer elements
	var arr = [5, 2, 7, 3, 4, 2, 9, 8, 1, 1, 10];
	// Test case
	task.evenProduct(arr);
}
main();

Output

 Array :
  5  2  7  3  4  2  9  8  1  1  10
 Even Product :
 (2, 5)
 (4, 7)
 (2, 3)
 (8, 9)
 (10, 1)
#   Python 3 program
#   Find the even product pairs in an array 

class Products :
    # This function are displaying given list elements
    def display(self, arr, size) :
        i = 0
        while (i < size) :
            print("  ", arr[i], end = "")
            i += 1
        
        print(end = "\n")
    
    #  Find pairs of even product in an list
    def evenProduct(self, arr) :
        #  Get the length
        n = len(arr)
        status = False
        #  Loop controlling variables
        i = 0
        j = 0
        k = 0
        l = 0
        #  Use to collect element
        oddValue=[]
        evenValue=[]
        #  Collecting even and odd value in given list
        i = 0
        while (i < n) :
            if (arr[i] % 2 == 0) :
                evenValue.append(arr[i])
            else :
                oddValue.append(arr[i])
            
            i += 1
        
        #  Get the length of even and odd value
        k = len(evenValue)
        l = len(oddValue)
        #  reset i index value
        i = 0
        print("\n Array : ")
        self.display(arr, n)
        print(" Even Product : ")
        #  Find product of even and odd elements
        while (i < k and j < l) :
            #  Display pair
            print(" (", evenValue[i] ,",", oddValue[j] ,") ")
            status = True
            #  Increase the index value
            i += 1
            j += 1
        
        #  Display remaining even element pair
        while (i + 1 < k) :
            status = True
            #  Display pair
            print(" ( ", evenValue[i] ,", ", evenValue[i + 1] ," ) ", end = "")
            i += 2
        
        if (status == False) :
            print("\n None ")
        
    

def main() :
    task = Products()
    #  Define list of integer elements
    arr = [5, 2, 7, 3, 4, 2, 9, 8, 1, 1, 10]
    #  Test case
    task.evenProduct(arr)

if __name__ == "__main__": main()

Output

 Array :
   5   2   7   3   4   2   9   8   1   1   10
 Even Product :
 ( 2 , 5 )
 ( 4 , 7 )
 ( 2 , 3 )
 ( 8 , 9 )
 ( 10 , 1 )
#   Ruby program
#   Find the even product pairs in an array 

class Products 
	# This function are displaying given array elements
	def display(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Find pairs of even product in an array
	def evenProduct(arr) 
		#  Get the length
		n = arr.length
		status = false
		#  Loop controlling variables
		i = 0
		j = 0
		k = 0
		l = 0
		#  Use to collect element
		oddValue = []
        evenValue = []
		#  Collecting even and odd value in given array
		i = 0
		while (i < n) 
			if (arr[i] % 2 == 0) 
				evenValue.push(arr[i])
			else 
				oddValue.push(arr[i])
			end

			i += 1
		end

		#  Get the length of even and odd value
		k = evenValue.length
		l = oddValue.length
		#  reset i index value
		i = 0
		print("\n Array : \n")
		self.display(arr, n)
		print(" Even Product : \n")
		#  Find product of even and odd elements
		while (i < k && j < l) 
			#  Display pair
			print(" (", evenValue[i] ,", ", oddValue[j] ,") \n")
			status = true
			#  Increase the index value
			i += 1
			j += 1
		end

		#  Display remaining even element pair
		while (i + 1 < k) 
			status = true
			#  Display pair
			print(" ( ", evenValue[i] ,", ", evenValue[i + 1] ," ) ")
			i += 2
		end

		if (status == false) 
			print("\n None \n")
		end

	end

end

def main() 
	task = Products.new()
	#  Define array of integer elements
	arr = [5, 2, 7, 3, 4, 2, 9, 8, 1, 1, 10]
	#  Test case
	task.evenProduct(arr)
end

main()

Output

 Array : 
  5  2  7  3  4  2  9  8  1  1  10
 Even Product : 
 (2, 5) 
 (4, 7) 
 (2, 3) 
 (8, 9) 
 (10, 1) 
import scala.collection.mutable._;
/*
  Scala program
  Find the even product pairs in an array 
*/
class Products
{
	//This function are displaying given array elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Find pairs of even product in an array
	def evenProduct(arr: Array[Int]): Unit = {
		// Get the length
		var n: Int = arr.length;
		var status: Boolean = false;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		var l: Int = 0;
		// Use to collect element
		var oddValue =  ArrayBuffer[Int]();
		var evenValue = ArrayBuffer[Int]();
		// Collecting even and odd value in given array
		i = 0;
		while (i < n)
		{
			if (arr(i) % 2 == 0)
			{
				evenValue += arr(i);
			}
			else
			{
				oddValue += arr(i);
			}
			i += 1;
		}
		// Get the length of even and odd value
		k = evenValue.size;
		l = oddValue.size;
		// reset i index value
		i = 0;
		print("\n Array : \n");
		this.display(arr, n);
		print(" Even Product : \n");
		// Find product of even and odd elements
		while (i < k && j < l)
		{
			// Display pair
			print(" (" + evenValue(i) + ", " + oddValue(j) + ") \n");
			status = true;
			// Increase the index value
			i += 1;
			j += 1;
		}
		// Display remaining even element pair
		while (i + 1 < k)
		{
			status = true;
			// Display pair
			print(" ( " + evenValue(i) + ", " + evenValue(i + 1) + " ) ");
			i += 2;
		}
		if (status == false)
		{
			print("\n None \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Products = new Products();
		// Define array of integer elements
		var arr: Array[Int] = Array(5, 2, 7, 3, 4, 2, 9, 8, 1, 1, 10);
		// Test case
		task.evenProduct(arr);
	}
}

Output

 Array :
  5  2  7  3  4  2  9  8  1  1  10
 Even Product :
 (2, 5)
 (4, 7)
 (2, 3)
 (8, 9)
 (10, 1)
/*
  Swift 4 program
  Find the even product pairs in an array 
*/
class Products
{
	//This function are displaying given array elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Find pairs of even product in an array
	func evenProduct(_ arr: [Int])
	{
		// Get the length
		let n: Int = arr.count;
		var status: Bool = false;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		var l: Int = 0;
		// Use to collect element
		
		var oddValue = [Int]();
		var evenValue = [Int]();
		// Collecting even and odd value in given array
		i = 0;
		while (i < n)
		{
			if (arr[i] % 2 == 0)
			{
				evenValue.append(arr[i]);
			}
			else
			{
				oddValue.append(arr[i]);
			}
			i += 1;
		}
		// Get the length of even and odd value
		k = evenValue.count;
		l = oddValue.count;
		// reset i index value
		i = 0;
		print("\n Array : ");
		self.display(arr, n);
		print(" Even Product : ");
		// Find product of even and odd elements
		while (i < k && j < l)
		{
			// Display pair
			print(" (", evenValue[i] ,", ", oddValue[j] ,") ");
			status = true;
			// Increase the index value
			i += 1;
			j += 1;
		}
		// Display remaining even element pair
		while (i + 1 < k)
		{
			status = true;
			// Display pair
			print(" ( ", evenValue[i] ,", ", evenValue[i + 1] ," ) ", terminator: "");
			i += 2;
		}
		if (status == false)
		{
			print("\n None ");
		}
	}
}
func main()
{
	let task: Products = Products();
	// Define array of integer elements
	let arr: [Int] = [5, 2, 7, 3, 4, 2, 9, 8, 1, 1, 10];
	// Test case
	task.evenProduct(arr);
}
main();

Output

 Array :
   5   2   7   3   4   2   9   8   1   1   10
 Even Product :
 ( 2 ,  5 )
 ( 4 ,  7 )
 ( 2 ,  3 )
 ( 8 ,  9 )
 ( 10 ,  1 )
/*
  Swift 4 program
  Find the even product pairs in an array 
*/
class Products
{
	//This function are displaying given array elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Find pairs of even product in an array
	func evenProduct(_ arr: [Int])
	{
		// Get the length
		let n: Int = arr.count;
		var status: Bool = false;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		var k: Int = 0;
		var l: Int = 0;
		// Use to collect element
		
		var oddValue = [Int]();
		var evenValue = [Int]();
		// Collecting even and odd value in given array
		i = 0;
		while (i < n)
		{
			if (arr[i] % 2 == 0)
			{
				evenValue.append(arr[i]);
			}
			else
			{
				oddValue.append(arr[i]);
			}
			i += 1;
		}
		// Get the length of even and odd value
		k = evenValue.count;
		l = oddValue.count;
		// reset i index value
		i = 0;
		print("\n Array : ");
		self.display(arr, n);
		print(" Even Product : ");
		// Find product of even and odd elements
		while (i < k && j < l)
		{
			// Display pair
			print(" (", evenValue[i] ,", ", oddValue[j] ,") ");
			status = true;
			// Increase the index value
			i += 1;
			j += 1;
		}
		// Display remaining even element pair
		while (i + 1 < k)
		{
			status = true;
			// Display pair
			print(" ( ", evenValue[i] ,", ", evenValue[i + 1] ," ) ", terminator: "");
			i += 2;
		}
		if (status == false)
		{
			print("\n None ");
		}
	}
}
func main()
{
	let task: Products = Products();
	// Define array of integer elements
	let arr: [Int] = [5, 2, 7, 3, 4, 2, 9, 8, 1, 1, 10];
	// Test case
	task.evenProduct(arr);
}
main();

Output

 Array :
   5   2   7   3   4   2   9   8   1   1   10
 Even Product :
 ( 2 ,  5 )
 ( 4 ,  7 )
 ( 2 ,  3 )
 ( 8 ,  9 )
 ( 10 ,  1 )
/*
  Kotlin program
  Find the even product pairs in an array 
*/
class Products
{
	//This function are displaying given array elements
	fun display(arr: Array < Int > , size: Int): Unit
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Find pairs of even product in an array
	fun evenProduct(arr: Array <Int> ): Unit
	{
		// Get the length
		var n: Int = arr.count();
		var status: Boolean = false;
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 0;
		var k: Int ;
		var l: Int ;
		// Use to collect element
		var oddValue  = mutableListOf<Int>();  
		var evenValue = mutableListOf<Int>();
		// Collecting even and odd value in given array
		while (i < n)
		{
			if (arr[i] % 2 == 0)
			{
				evenValue.add(arr[i]);
			}
			else
			{
				oddValue.add(arr[i]);
			}
			i += 1;
		}
		// Get the length of even and odd value
		k = evenValue.size;
		l = oddValue.size;
		// reset i index value
		i = 0;
		print("\n Array : \n");
		this.display(arr, n);
		print(" Even Product : \n");
		// Find product of even and odd elements
		while (i < k && j < l)
		{
			// Display pair
			print(" (" + evenValue[i] + ", " + oddValue[j] + ") \n");
			status = true;
			// Increase the index value
			i += 1;
			j += 1;
		}
		// Display remaining even element pair
		while (i + 1 < k)
		{
			status = true;
			// Display pair
			print(" ( " + evenValue[i] + ", " + evenValue[i + 1] + " ) ");
			i += 2;
		}
		if (status == false)
		{
			print("\n None \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Products = Products();
	// Define array of integer elements
	var arr: Array < Int > = arrayOf(5, 2, 7, 3, 4, 2, 9, 8, 1, 1, 10);
	// Test case
	task.evenProduct(arr);
}

Output

 Array :
  5  2  7  3  4  2  9  8  1  1  10
 Even Product :
 (2, 5)
 (4, 7)
 (2, 3)
 (8, 9)
 (10, 1)


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved