Check if two numbers are co-prime or not

Here given code implementation process.

/*
  Java Program 
  Check if two numbers are co-prime or not
*/
public class Coprime
{
	// Recursively find GCD of two given number
	public int gcd(int first, int second)
	{
		if (first == 0)
		{
			return second;
		}
		return gcd(second % first, first);
	}
	// Check that given two number is co prime to each other or not
	public void isCoprime(int n1, int n2)
	{
		System.out.print("\n Given Number (" + n1 + "," + n2 + ") ");
		if (gcd(n1, n2) == 1)
		{
			// When given number is co-prime
			System.out.print("\n Is co-prime ");
		}
		else
		{
			//  When given number is not co-prime
			System.out.print("\n Is Not co-prime ");
		}
	}
	public static void main(String[] args)
	{
		Coprime task = new Coprime();
		// Test Case
		task.isCoprime(7, 2);
		task.isCoprime(3, 49);
		task.isCoprime(6, 30);
	}
}

input

 Given Number (7,2)
 Is co-prime
 Given Number (3,49)
 Is co-prime
 Given Number (6,30)
 Is Not co-prime
// C Program
// Check if two numbers are co-prime or not
#include <stdio.h>

// Recursively find GCD of two given number
int find_gcd(int first, int second)
{
	if (first == 0)
	{
		return second;
	}
	return find_gcd(second % first, first);
}
// Check that given two number is co prime to each other or not
void isCoprime(int n1, int n2)
{
	printf("\n Given Number (%d %d)  ", n1, n2);
	if (find_gcd(n1, n2) == 1)
	{
		// When given number is co-prime
		printf("\n Is co-prime ");
	}
	else
	{
		//  When given number is not co-prime
		printf("\n Is Not co-prime ");
	}
}
int main()
{
	// Test Case
	isCoprime(7, 2);
	isCoprime(3, 49);
	isCoprime(6, 30);
	return 0;
}

input

 Given Number (7 2)
 Is co-prime
 Given Number (3 49)
 Is co-prime
 Given Number (6 30)
 Is Not co-prime
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program 
  Check if two numbers are co-prime or not
*/
class Coprime
{
	public:
		// Recursively find GCD of two given number
		int gcd(int first, int second)
		{
			if (first == 0)
			{
				return second;
			}
			return this->gcd(second % first, first);
		}
	// Check that given two number is co prime to each other or not
	void isCoprime(int n1, int n2)
	{
		cout << "\n Given Number (" << n1 << "," << n2 << ") ";
		if (this->gcd(n1, n2) == 1)
		{
			// When given number is co-prime
			cout << "\n Is co-prime ";
		}
		else
		{
			//  When given number is not co-prime
			cout << "\n Is Not co-prime ";
		}
	}
};
int main()
{
	Coprime *task = new Coprime();
	// Test Case
	task->isCoprime(7, 2);
	task->isCoprime(3, 49);
	task->isCoprime(6, 30);
	return 0;
}

input

 Given Number (7,2)
 Is co-prime
 Given Number (3,49)
 Is co-prime
 Given Number (6,30)
 Is Not co-prime
// Include namespace system
using System;
/*
  Csharp Program 
  Check if two numbers are co-prime or not
*/
public class Coprime
{
	// Recursively find GCD of two given number
	public int gcd(int first, int second)
	{
		if (first == 0)
		{
			return second;
		}
		return this.gcd(second % first, first);
	}
	// Check that given two number is co prime to each other or not
	public void isCoprime(int n1, int n2)
	{
		Console.Write("\n Given Number (" + n1 + "," + n2 + ") ");
		if (this.gcd(n1, n2) == 1)
		{
			// When given number is co-prime
			Console.Write("\n Is co-prime ");
		}
		else
		{
			//  When given number is not co-prime
			Console.Write("\n Is Not co-prime ");
		}
	}
	public static void Main(String[] args)
	{
		Coprime task = new Coprime();
		// Test Case
		task.isCoprime(7, 2);
		task.isCoprime(3, 49);
		task.isCoprime(6, 30);
	}
}

input

 Given Number (7,2)
 Is co-prime
 Given Number (3,49)
 Is co-prime
 Given Number (6,30)
 Is Not co-prime
<?php
/*
  Php Program 
  Check if two numbers are co-prime or not
*/
class Coprime
{
	// Recursively find GCD of two given number
	public
	function gcd($first, $second)
	{
		if ($first == 0)
		{
			return $second;
		}
		return $this->gcd($second % $first, $first);
	}
	// Check that given two number is co prime to each other or not
	public
	function isCoprime($n1, $n2)
	{
		echo "\n Given Number (". $n1 .",". $n2 .") ";
		if ($this->gcd($n1, $n2) == 1)
		{
			// When given number is co-prime
			echo "\n Is co-prime ";
		}
		else
		{
			//  When given number is not co-prime
			echo "\n Is Not co-prime ";
		}
	}
}

function main()
{
	$task = new Coprime();
	// Test Case
	$task->isCoprime(7, 2);
	$task->isCoprime(3, 49);
	$task->isCoprime(6, 30);
}
main();

input

 Given Number (7,2)
 Is co-prime
 Given Number (3,49)
 Is co-prime
 Given Number (6,30)
 Is Not co-prime
/*
  Node JS Program 
  Check if two numbers are co-prime or not
*/
class Coprime
{
	// Recursively find GCD of two given number
	gcd(first, second)
	{
		if (first == 0)
		{
			return second;
		}
		return this.gcd(second % first, first);
	}
	// Check that given two number is co prime to each other or not
	isCoprime(n1, n2)
	{
		process.stdout.write("\n Given Number (" + n1 + "," + n2 + ") ");
		if (this.gcd(n1, n2) == 1)
		{
			// When given number is co-prime
			process.stdout.write("\n Is co-prime ");
		}
		else
		{
			//  When given number is not co-prime
			process.stdout.write("\n Is Not co-prime ");
		}
	}
}

function main()
{
	var task = new Coprime();
	// Test Case
	task.isCoprime(7, 2);
	task.isCoprime(3, 49);
	task.isCoprime(6, 30);
}
main();

input

 Given Number (7,2)
 Is co-prime
 Given Number (3,49)
 Is co-prime
 Given Number (6,30)
 Is Not co-prime
# Python 3 Program
# Check if two numbers are co - prime or not
class Coprime :
	# Recursively find GCD of two given number
	def gcd(self, first, second) :
		if (first == 0) :
			return second
		
		return self.gcd(second % first, first)
	
	# Check that given two number is co prime to each other or not
	def isCoprime(self, n1, n2) :
		print("\n Given Number (", n1 ,",", n2 ,") ", end = "")
		if (self.gcd(n1, n2) == 1) :
			# When given number is co - prime
			print("\n Is co-prime ", end = "")
		else :
			# When given number is not co - prime
			print("\n Is Not co-prime ", end = "")
		
	

def main() :
	task = Coprime()
	# Test Case
	task.isCoprime(7, 2)
	task.isCoprime(3, 49)
	task.isCoprime(6, 30)

if __name__ == "__main__": main()

input

 Given Number ( 7 , 2 )
 Is co-prime
 Given Number ( 3 , 49 )
 Is co-prime
 Given Number ( 6 , 30 )
 Is Not co-prime
# Ruby Program
# Check if two numbers are co - prime or not
class Coprime 
	# Recursively find GCD of two given number
	def gcd(first, second) 
		if (first == 0) 
			return second
		end

		return self.gcd(second % first, first)
	end

	# Check that given two number is co prime to each other or not
	def isCoprime(n1, n2) 
		print("\n Given Number (", n1 ,",", n2 ,") ")
		if (self.gcd(n1, n2) == 1) 
			# When given number is co - prime
			print("\n Is co-prime ")
		else 
			# When given number is not co - prime
			print("\n Is Not co-prime ")
		end

	end

end

def main() 
	task = Coprime.new()
	# Test Case
	task.isCoprime(7, 2)
	task.isCoprime(3, 49)
	task.isCoprime(6, 30)
end

main()

input

 Given Number (7,2) 
 Is co-prime 
 Given Number (3,49) 
 Is co-prime 
 Given Number (6,30) 
 Is Not co-prime 
/*
  Scala Program 
  Check if two numbers are co-prime or not
*/
class Coprime
{
	// Recursively find GCD of two given number
	def gcd(first: Int, second: Int): Int = {
		if (first == 0)
		{
			return second;
		}
		return gcd(second % first, first);
	}
	// Check that given two number is co prime to each other or not
	def isCoprime(n1: Int, n2: Int): Unit = {
		print("\n Given Number (" + n1 + "," + n2 + ") ");
		if (gcd(n1, n2) == 1)
		{
			// When given number is co-prime
			print("\n Is co-prime ");
		}
		else
		{
			//  When given number is not co-prime
			print("\n Is Not co-prime ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Coprime = new Coprime();
		// Test Case
		task.isCoprime(7, 2);
		task.isCoprime(3, 49);
		task.isCoprime(6, 30);
	}
}

input

 Given Number (7,2)
 Is co-prime
 Given Number (3,49)
 Is co-prime
 Given Number (6,30)
 Is Not co-prime
/*
  Swift 4 Program 
  Check if two numbers are co-prime or not
*/
class Coprime
{
	// Recursively find GCD of two given number
	func gcd(_ first: Int, _ second: Int)->Int
	{
		if (first == 0)
		{
			return second;
		}
		return self.gcd(second % first, first);
	}
	// Check that given two number is co prime to each other or not
	func isCoprime(_ n1: Int, _ n2: Int)
	{
		print("\n Given Number (", n1 ,",", n2 ,") ", terminator: "");
		if (self.gcd(n1, n2) == 1)
		{
			// When given number is co-prime
			print("\n Is co-prime ", terminator: "");
		}
		else
		{
			//  When given number is not co-prime
			print("\n Is Not co-prime ", terminator: "");
		}
	}
}
func main()
{
	let task: Coprime = Coprime();
	// Test Case
	task.isCoprime(7, 2);
	task.isCoprime(3, 49);
	task.isCoprime(6, 30);
}
main();

input

 Given Number ( 7 , 2 )
 Is co-prime
 Given Number ( 3 , 49 )
 Is co-prime
 Given Number ( 6 , 30 )
 Is Not co-prime
/*
  Kotlin Program 
  Check if two numbers are co-prime or not
*/
class Coprime
{
	// Recursively find GCD of two given number
	fun gcd(first: Int, second: Int): Int
	{
		if (first == 0)
		{
			return second;
		}
		return this.gcd(second % first, first);
	}
	// Check that given two number is co prime to each other or not
	fun isCoprime(n1: Int, n2: Int): Unit
	{
		print("\n Given Number (" + n1 + "," + n2 + ") ");
		if (this.gcd(n1, n2) == 1)
		{
			// When given number is co-prime
			print("\n Is co-prime ");
		}
		else
		{
			//  When given number is not co-prime
			print("\n Is Not co-prime ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Coprime = Coprime();
	// Test Case
	task.isCoprime(7, 2);
	task.isCoprime(3, 49);
	task.isCoprime(6, 30);
}

input

 Given Number (7,2)
 Is co-prime
 Given Number (3,49)
 Is co-prime
 Given Number (6,30)
 Is Not co-prime

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