GCD of two numbers

Here given code implementation process.

//C Program
//Calculate GCD of two numbers
#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);
}

void gcd(int n1,int n2)
{
  //Get GCD of two number
  int result = find_gcd(n1,n2);

  printf("\nNumbers (%d %d) : GCD : %d",n1,n2,result);
}
int main()
{
  //Test Case
  gcd(15,20);
  gcd(24,75);
  gcd(45,30);
  gcd(21,49);
  gcd(16,40);
  return 0;
}

Output

Numbers (15 20) : GCD : 5
Numbers (24 75) : GCD : 3
Numbers (45 30) : GCD : 15
Numbers (21 49) : GCD : 7
Numbers (16 40) : GCD : 8
/*
 C++ Program
 Calculate GCD of two numbers
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

		//Recursively find GCD of two given number
		int find_gcd(int first, int second) {
			if (first == 0) {
				return second;
			}
			return this->find_gcd(second % first, first);
		}
	void gcd(int n1, int n2) {
		//Get GCD of two number
		int result = this->find_gcd(n1, n2);
		cout << "\nNumbers (" << n1 << " " << n2 << ") : GCD : " << result;
	}
};
int main() {
	MyNumber obj;
	//Test Case
	obj.gcd(15, 20);
	obj.gcd(24, 75);
	obj.gcd(45, 30);
	obj.gcd(21, 49);
	obj.gcd(16, 40);
	return 0;
}

Output

Numbers (15 20) : GCD : 5
Numbers (24 75) : GCD : 3
Numbers (45 30) : GCD : 15
Numbers (21 49) : GCD : 7
Numbers (16 40) : GCD : 8
/*
  Java Program
  Calculate GCD of two numbers
*/
public class MyNumber {
 
  //Recursively find GCD of two given number
  public int find_gcd(int first,int second)
  {
    if(first==0)
    {
      return second;
    }
    return find_gcd(second%first,first);
  }

  public void gcd(int n1,int n2)
  {
    //Get GCD of two number
    int result = find_gcd(n1,n2);

    System.out.print("\nNumbers ("+n1+" "+n2+") : GCD : "+result);
  }

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    //Test Case
    obj.gcd(15,20);
    obj.gcd(24,75);
    obj.gcd(45,30);
    obj.gcd(21,49);
    obj.gcd(16,40);
  }
}

Output

Numbers (15 20) : GCD : 5
Numbers (24 75) : GCD : 3
Numbers (45 30) : GCD : 15
Numbers (21 49) : GCD : 7
Numbers (16 40) : GCD : 8
/*
  C# Program
  Calculate GCD of two numbers
*/
using System;
public class MyNumber {

	//Recursively find GCD of two given number
	public int find_gcd(int first, int second) {
		if (first == 0) {
			return second;
		}
		return find_gcd(second % first, first);
	}

	public void gcd(int n1, int n2) {
		//Get GCD of two number
		int result = find_gcd(n1, n2);

		Console.Write("\nNumbers (" + n1 + " " + n2 + ") : GCD : " + result);
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		//Test Case
		obj.gcd(15, 20);
		obj.gcd(24, 75);
		obj.gcd(45, 30);
		obj.gcd(21, 49);
		obj.gcd(16, 40);
	}
}

Output

Numbers (15 20) : GCD : 5
Numbers (24 75) : GCD : 3
Numbers (45 30) : GCD : 15
Numbers (21 49) : GCD : 7
Numbers (16 40) : GCD : 8
# Python 3 Program
# Calculate GCD of two numbers
class MyNumber :
	# Recursively find GCD of two given number
	def find_gcd(self, first, second) :
		if (first == 0) :
			return second
		
		return self.find_gcd(second % first, first)
	
	def gcd(self, n1, n2) :
		# Get GCD of two number
		result = self.find_gcd(n1, n2)
		print("Numbers (", n1 ," ", n2 ,") : GCD : ", result)
	

def main() :
	obj = MyNumber()
	# Test Case
	obj.gcd(15, 20)
	obj.gcd(24, 75)
	obj.gcd(45, 30)
	obj.gcd(21, 49)
	obj.gcd(16, 40)


if __name__ == "__main__":
	main()

Output

Numbers (15 20) : GCD : 5
Numbers (24 75) : GCD : 3
Numbers (45 30) : GCD : 15
Numbers (21 49) : GCD : 7
Numbers (16 40) : GCD : 8
# Ruby Program 
# Calculate GCD of two numbers
class MyNumber 
	# Recursively find GCD of two given number
	def find_gcd(first, second) 
		if (first == 0) 
			return second
		end
		return self.find_gcd(second % first, first)
	end
	def gcd(n1, n2) 
		# Get GCD of two number
		result = self.find_gcd(n1, n2)
		print("\nNumbers (", n1 ," ", n2 ,")  :GCD  :", result)
	end
end
def main() 
	obj = MyNumber.new()
	# Test Case
	obj.gcd(15, 20)
	obj.gcd(24, 75)
	obj.gcd(45, 30)
	obj.gcd(21, 49)
	obj.gcd(16, 40)
end
main()

Output

Numbers (15 20)  :GCD  :5
Numbers (24 75)  :GCD  :3
Numbers (45 30)  :GCD  :15
Numbers (21 49)  :GCD  :7
Numbers (16 40)  :GCD  :8
/*
 Scala Program
 Calculate GCD of two numbers
*/
class MyNumber {
	//Recursively find GCD of two given number
	def find_gcd(first: Int, second: Int): Int = {
		if (first == 0) {
			return second;
		}
		return this.find_gcd(second % first, first);
	}
	def gcd(n1: Int, n2: Int): Unit = {
		//Get GCD of two number
		var result: Int = this.find_gcd(n1, n2);
		print("\nNumbers (" + n1 + " " + n2 + ") : GCD : " + result);
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.gcd(15, 20);
        obj.gcd(24, 75);
        obj.gcd(45, 30);
        obj.gcd(21, 49);
        obj.gcd(16, 40);
	}
}

Output

Numbers (15 20) : GCD : 5
Numbers (24 75) : GCD : 3
Numbers (45 30) : GCD : 15
Numbers (21 49) : GCD : 7
Numbers (16 40) : GCD : 8
/*
  Swift 4 Program
  Calculate GCD of two numbers
*/
class MyNumber {
	//Recursively find GCD of two given number
	func find_gcd(_ first: Int, _ second: Int) -> Int {
		if (first == 0) {
			return second;
		}
		return self.find_gcd(second % first, first);
	}
	func gcd(_ n1: Int, _ n2: Int) {
		//Get GCD of two number
		let result: Int = self.find_gcd(n1, n2);
		print("Numbers (", n1 ," ", n2 ,") : GCD : ", result);
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.gcd(15, 20);
	obj.gcd(24, 75);
	obj.gcd(45, 30);
	obj.gcd(21, 49);
	obj.gcd(16, 40);
}
main();

Output

Numbers ( 15   20 ) : GCD :  5
Numbers ( 24   75 ) : GCD :  3
Numbers ( 45   30 ) : GCD :  15
Numbers ( 21   49 ) : GCD :  7
Numbers ( 16   40 ) : GCD :  8
<?php
/*
  Php Program
  Calculate GCD of two numbers
*/
class MyNumber {
	//Recursively find GCD of two given number

	public 	function find_gcd($first, $second) {
		if ($first == 0) {
			return $second;
		}
		return $this->find_gcd($second % $first, $first);
	}
	public 	function gcd($n1, $n2) {
		//Get GCD of two number
		$result = $this->find_gcd($n1, $n2);
		echo("\nNumbers (". $n1 ." ". $n2 .") : GCD : ". $result);
	}
};

function main() {
	$obj = new MyNumber();
	//Test Case

	$obj->gcd(15, 20);
	$obj->gcd(24, 75);
	$obj->gcd(45, 30);
	$obj->gcd(21, 49);
	$obj->gcd(16, 40);
}
main();

Output

Numbers (15 20) : GCD : 5
Numbers (24 75) : GCD : 3
Numbers (45 30) : GCD : 15
Numbers (21 49) : GCD : 7
Numbers (16 40) : GCD : 8
/*
 Node Js Program
 Calculate GCD of two numbers
*/
class MyNumber {
	//Recursively find GCD of two given number
	find_gcd(first, second) {
		if (first == 0) {
			return second;
		}
		return this.find_gcd(second % first, first);
	}
	gcd(n1, n2) {
		//Get GCD of two number
		var result = this.find_gcd(n1, n2);
		process.stdout.write("\nNumbers (" + n1 + " " + n2 + ") : GCD : " + result);
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test Case
	obj.gcd(15, 20);
	obj.gcd(24, 75);
	obj.gcd(45, 30);
	obj.gcd(21, 49);
	obj.gcd(16, 40);
}
main();

Output

Numbers (15 20) : GCD : 5
Numbers (24 75) : GCD : 3
Numbers (45 30) : GCD : 15
Numbers (21 49) : GCD : 7
Numbers (16 40) : GCD : 8

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