Count all pairs of given product

Here given code implementation process.

//C Program 
//Count all pairs of given product
#include <stdio.h>

//print the array elements
void display(int arr[], int size)
{
	int i = 0;
	for (i = 0; i < size; i++)
	{
		printf("  %d", arr[i]);
	}
}
//Count all pairs of given product in array
void count_product(int arr[], int size, int product)
{
	int counter = 0;
	for (int i = 0; i < size; ++i)
	{
		for (int j = i + 1; j < size; ++j)
		{
			if (arr[i] * arr[j] == product)
			{
				//When get a new pair
				counter++;
			}
		}
	}
	printf("\n  Pair of product [%d]  is : %d ", product, counter);
}
int main()
{
	//Define array elements
	int arr[] = {
		5,
		7,
		4,
		2,
		-2,
		-5,
		10,
		1,
		20
	};
	//Count size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	int product = 10;
	printf("\n  Array : ");
	display(arr, size);
	count_product(arr, size, product);
	product = 40;
	count_product(arr, size, product);
	product = 60;
	count_product(arr, size, product);
	return 0;
}

Output

  Array :   5  7  4  2  -2  -5  10  1  20
  Pair of product [10]  is : 3
  Pair of product [40]  is : 2
  Pair of product [60]  is : 0
// Java program 
// Count all pairs of given product
class MyArray
{
	//print the array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; i++)
		{
			System.out.print(" " + arr[i]);
		}
	}
	//Count all pairs of given product in array
	public void count_product(int[] arr, int size, int product)
	{
		int counter = 0;
		for (int i = 0; i < size; ++i)
		{
			for (int j = i + 1; j < size; ++j)
			{
				if (arr[i] * arr[j] == product)
				{
					//When get a new pair
					counter++;
				}
			}
		}
		System.out.print("\n Pair of product [" + product + "] is : " + counter);
	}
	public static void main(String[] args)
	{
		MyArray obj = new MyArray();
		//Define integer elements
		int[] arr = {
			5,
			7,
			4,
			2,
			-2,
			-5,
			10,
			1,
			20
		};
		//Count size of array
		int size = arr.length;
		int product = 10;
		System.out.print("\n Array : ");
		obj.display(arr, size);
		obj.count_product(arr, size, product);
		product = 40;
		obj.count_product(arr, size, product);
		product = 60;
		obj.count_product(arr, size, product);
	}
}

Output

 Array :  5 7 4 2 -2 -5 10 1 20
 Pair of product [10] is : 3
 Pair of product [40] is : 2
 Pair of product [60] is : 0
//Include header file
#include <iostream>

using namespace std;
// C++ program 
// Count all pairs of given product
class MyArray
{
	public:
		//print the array elements
		void display(int arr[], int size)
		{
			for (int i = 0; i < size; i++)
			{
				cout << " " << arr[i];
			}
		}
	//Count all pairs of given product in array
	void count_product(int arr[], int size, int product)
	{
		int counter = 0;
		for (int i = 0; i < size; ++i)
		{
			for (int j = i + 1; j < size; ++j)
			{
				if (arr[i] * arr[j] == product)
				{
					//When get a new pair
					counter++;
				}
			}
		}
		cout << "\n Pair of product [" << product << "] is : " << counter;
	}
};
int main()
{
	MyArray obj = MyArray();
	int arr[] = {
		5 , 7 , 4 , 2 , -2 , -5 , 10 , 1 , 20
	};
	//Count size of array
	int size = sizeof(arr) / sizeof(arr[0]);
	int product = 10;
	cout << "\n Array : ";
	obj.display(arr, size);
	obj.count_product(arr, size, product);
	product = 40;
	obj.count_product(arr, size, product);
	product = 60;
	obj.count_product(arr, size, product);
	return 0;
}

Output

 Array :  5 7 4 2 -2 -5 10 1 20
 Pair of product [10] is : 3
 Pair of product [40] is : 2
 Pair of product [60] is : 0
//Include namespace system
using System;
// C# program 
// Count all pairs of given product
class MyArray
{
	//print the array elements
	public void display(int[] arr, int size)
	{
		for (int i = 0; i < size; i++)
		{
			Console.Write(" " + arr[i]);
		}
	}
	//Count all pairs of given product in array
	public void count_product(int[] arr, int size, int product)
	{
		int counter = 0;
		for (int i = 0; i < size; ++i)
		{
			for (int j = i + 1; j < size; ++j)
			{
				if (arr[i] * arr[j] == product)
				{
					//When get a new pair
					counter++;
				}
			}
		}
		Console.Write("\n Pair of product [" + product + "] is : " + counter);
	}
	public static void Main(String[] args)
	{
		MyArray obj = new MyArray();
		int[] arr = {
			5 , 7 , 4 , 2 , -2 , -5 , 10 , 1 , 20
		};
		//Count size of array
		int size = arr.Length;
		int product = 10;
		Console.Write("\n Array : ");
		obj.display(arr, size);
		obj.count_product(arr, size, product);
		product = 40;
		obj.count_product(arr, size, product);
		product = 60;
		obj.count_product(arr, size, product);
	}
}

Output

 Array :  5 7 4 2 -2 -5 10 1 20
 Pair of product [10] is : 3
 Pair of product [40] is : 2
 Pair of product [60] is : 0
<?php
// Php program 
// Count all pairs of given product
class MyArray
{
	//print the array elements
	public	function display( & $arr, $size)
	{
		for ($i = 0; $i < $size; $i++)
		{
			echo " ". $arr[$i];
		}
	}
	//Count all pairs of given product in array
	public	function count_product( & $arr, $size, $product)
	{
		$counter = 0;
		for ($i = 0; $i < $size; ++$i)
		{
			for ($j = $i + 1; $j < $size; ++$j)
			{
				if ($arr[$i] * $arr[$j] == $product)
				{
					//When get a new pair
					$counter++;
				}
			}
		}
		echo "\n Pair of product [". $product ."] is : ". $counter;
	}
}

function main()
{
	$obj = new MyArray();
	//Define integer elements
	$arr = array(5, 7, 4, 2, -2, -5, 10, 1, 20);
	//Count size of array
	$size = count($arr);
	$product = 10;
	echo "\n Array : ";
	$obj->display($arr, $size);
	$obj->count_product($arr, $size, $product);
	$product = 40;
	$obj->count_product($arr, $size, $product);
	$product = 60;
	$obj->count_product($arr, $size, $product);
}
main();

Output

 Array :  5 7 4 2 -2 -5 10 1 20
 Pair of product [10] is : 3
 Pair of product [40] is : 2
 Pair of product [60] is : 0
// Node Js program 
// Count all pairs of given product
class MyArray
{
	//print the array elements
	display(arr, size)
	{
		for (var i = 0; i < size; i++)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	//Count all pairs of given product in array
	count_product(arr, size, product)
	{
		var counter = 0;
		for (var i = 0; i < size; ++i)
		{
			for (var j = i + 1; j < size; ++j)
			{
				if (arr[i] * arr[j] == product)
				{
					//When get a new pair
					counter++;
				}
			}
		}
		process.stdout.write("\n Pair of product [" + product + "] is : " + counter);
	}
}

function main()
{
	var obj = new MyArray();
	//Define integer elements
	var arr = [5, 7, 4, 2, -2, -5, 10, 1, 20];
	//Count size of array
	var size = arr.length;
	var product = 10;
	process.stdout.write("\n Array : ");
	obj.display(arr, size);
	obj.count_product(arr, size, product);
	product = 40;
	obj.count_product(arr, size, product);
	product = 60;
	obj.count_product(arr, size, product);
}
main();

Output

 Array :  5 7 4 2 -2 -5 10 1 20
 Pair of product [10] is : 3
 Pair of product [40] is : 2
 Pair of product [60] is : 0
#  Python 3 program 
#  Count all pairs of given product
class MyArray :
	# print the array elements
	def display(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	# Count all pairs of given product in array
	def count_product(self, arr, size, product) :
		counter = 0
		i = 0
		while (i < size) :
			j = i + 1
			while (j < size) :
				if (arr[i] * arr[j] == product) :
					# When get a new pair
					counter += 1
				
				j += 1
			
			i += 1
		
		print("\n Pair of product [", product ,"] is : ", counter, end = "")
	

def main() :
	obj = MyArray()
	# Define integer elements
	arr = [5, 7, 4, 2, -2, -5, 10, 1, 20]
	# Count size of array
	size = len(arr)
	product = 10
	print("\n Array : ", end = "")
	obj.display(arr, size)
	obj.count_product(arr, size, product)
	product = 40
	obj.count_product(arr, size, product)
	product = 60
	obj.count_product(arr, size, product)

if __name__ == "__main__": main()

Output

 Array :   5  7  4  2  -2  -5  10  1  20
 Pair of product [ 10 ] is :  3
 Pair of product [ 40 ] is :  2
 Pair of product [ 60 ] is :  0
#  Ruby program 
#  Count all pairs of given product
class MyArray

	# print the array elements
	def display(arr, size)
	
		i = 0
		while (i < size)
		
			print(" ", arr[i])
			i += 1
		end
	end
	# Count all pairs of given product in array
	def count_product(arr, size, product)
	
		counter = 0
		i = 0
		while (i < size)
		
			j = i + 1
			while (j < size)
			
				if (arr[i] * arr[j] == product)
				
					# When get a new pair
					counter += 1
				end
				j += 1
			end
			i += 1
		end
		print("\n Pair of product [", product ,"] is : ", counter)
	end
end
def main()

	obj = MyArray.new()
	# Define integer elements
	arr = [5, 7, 4, 2, -2, -5, 10, 1, 20]
	# Count size of array
	size = arr.length
	product = 10
	print("\n Array : ")
	obj.display(arr, size)
	obj.count_product(arr, size, product)
	product = 40
	obj.count_product(arr, size, product)
	product = 60
	obj.count_product(arr, size, product)
end
main()

Output

 Array :  5 7 4 2 -2 -5 10 1 20
 Pair of product [10] is : 3
 Pair of product [40] is : 2
 Pair of product [60] is : 0
// Scala program 
// Count all pairs of given product
class MyArray
{
	//print the array elements
	def display(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	//Count all pairs of given product in array
	def count_product(arr: Array[Int], size: Int, product: Int): Unit = {
		var counter: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			var j: Int = i + 1;
			while (j < size)
			{
				if (arr(i) * arr(j) == product)
				{
					//When get a new pair
					counter += 1;
				}
				j += 1;
			}
			i += 1;
		}
		print("\n Pair of product [" + product + "] is : " + counter);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyArray = new MyArray();
		//Define integer elements
		var arr: Array[Int] = Array(5, 7, 4, 2, -2, -5, 10, 1, 20);
		//Count size of array
		var size: Int = arr.length;
		var product: Int = 10;
		print("\n Array : ");
		obj.display(arr, size);
		obj.count_product(arr, size, product);
		product = 40;
		obj.count_product(arr, size, product);
		product = 60;
		obj.count_product(arr, size, product);
	}
}

Output

 Array :  5 7 4 2 -2 -5 10 1 20
 Pair of product [10] is : 3
 Pair of product [40] is : 2
 Pair of product [60] is : 0
// Swift program 
// Count all pairs of given product
class MyArray
{
	//print the array elements
	func display(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	//Count all pairs of given product in array
	func count_product(_ arr: [Int], _ size: Int, _ product: Int)
	{
		var counter: Int = 0;
		var i: Int = 0;
		while (i < size)
		{
			var j: Int = i + 1;
			while (j < size)
			{
				if (arr[i] * arr[j] == product)
				{
					//When get a new pair
					counter += 1;
				}
				j += 1;
			}
			i += 1;
		}
		print("\n Pair of product [", product ,"] is : ", counter, terminator: "");
	}
}
func main()
{
	let obj: MyArray = MyArray();
	//Define integer elements
	let arr: [Int] = [5, 7, 4, 2, -2, -5, 10, 1, 20];
	//Count size of array
	let size: Int = arr.count;
	var product: Int = 10;
	print("\n Array : ", terminator: "");
	obj.display(arr, size);
	obj.count_product(arr, size, product);
	product = 40;
	obj.count_product(arr, size, product);
	product = 60;
	obj.count_product(arr, size, product);
}
main();

Output

 Array :   5  7  4  2  -2  -5  10  1  20
 Pair of product [ 10 ] is :  3
 Pair of product [ 40 ] is :  2
 Pair of product [ 60 ] is :  0

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