LCM of two numbers

Here given code implementation process.

//C Program
//Calculate LCM of two numbers
#include <stdio.h>

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

void lcm(int n1,int n2)
{
  //Get LCM of two number
  int result = (n1*n2)/find_gcd(n1,n2);

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

Output

Numbers (24 75) : LCM : 600
Numbers (45 30) : LCM : 90
Numbers (21 49) : LCM : 147
Numbers (15 20) : LCM : 60
Numbers (16 40) : LCM : 80
/*
 C++ Program
 Calculate LCM of two numbers
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

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

Output

Numbers (24 75) : LCM : 600
Numbers (45 30) : LCM : 90
Numbers (21 49) : LCM : 147
Numbers (15 20) : LCM : 60
Numbers (16 40) : LCM : 80
/*
  Java Program
  Calculate LCM of two numbers
*/
public class MyNumber {
 

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

  public void lcm(int n1,int n2)
  {
    //Get LCM of two number
    int result = (n1*n2)/find_gcd(n1,n2);

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

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

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

Output

Numbers (24 75) : LCM : 600
Numbers (45 30) : LCM : 90
Numbers (21 49) : LCM : 147
Numbers (15 20) : LCM : 60
Numbers (16 40) : LCM : 80
/*
  C# Program
  Calculate LCM of two numbers
*/
using System;
public class MyNumber {


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

	public void lcm(int n1, int n2) {
		//Get LCM of two number
		int result = (n1 * n2) / find_gcd(n1, n2);

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

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

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

Output

Numbers (24 75) : LCM : 600
Numbers (45 30) : LCM : 90
Numbers (21 49) : LCM : 147
Numbers (15 20) : LCM : 60
Numbers (16 40) : LCM : 80
# Python 3 Program
# Calculate LCM of two numbers
class MyNumber :
	# Recursively find GCD of two given number
	def find_gcd(self, n1, n2) :
		if (n1 == 0) :
			return n2
		
		return self.find_gcd(n2 % n1, n1)
	
	def lcm(self, n1, n2) :
		# Get LCM of two number
		result = int((n1 * n2) / self.find_gcd(n1, n2))
		print("Numbers (", n1 ," ", n2 ,") : LCM : ", result)
	

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


if __name__ == "__main__":
	main()

Output

Numbers (24 75) : LCM : 600
Numbers (45 30) : LCM : 90
Numbers (21 49) : LCM : 147
Numbers (15 20) : LCM : 60
Numbers (16 40) : LCM : 80
# Ruby Program 
# Calculate LCM of two numbers
class MyNumber 
	# Recursively find GCD of two given number
	def find_gcd(n1, n2) 
		if (n1 == 0) 
			return n2
		end
		return self.find_gcd(n2 % n1, n1)
	end
	def lcm(n1, n2) 
		# Get LCM of two number
		result = (n1 * n2) / self.find_gcd(n1, n2)
		print("\nNumbers (", n1 ," ", n2 ,")  :LCM  :", result)
	end
end
def main() 
	obj = MyNumber.new()
	# Test Case
	obj.lcm(24, 75)
	obj.lcm(45, 30)
	obj.lcm(21, 49)
	obj.lcm(15, 20)
	obj.lcm(16, 40)
end
main()

Output

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

Output

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

Output

Numbers ( 24   75 ) : LCM :  600
Numbers ( 45   30 ) : LCM :  90
Numbers ( 21   49 ) : LCM :  147
Numbers ( 15   20 ) : LCM :  60
Numbers ( 16   40 ) : LCM :  80
<?php
/*
  Php Program
  Calculate LCM of two numbers
*/
class MyNumber {
	//Recursively find GCD of two given number

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

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

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

Output

Numbers (24 75) : LCM : 600
Numbers (45 30) : LCM : 90
Numbers (21 49) : LCM : 147
Numbers (15 20) : LCM : 60
Numbers (16 40) : LCM : 80
/*
 Node Js Program
 Calculate LCM of two numbers
*/
class MyNumber {
	//Recursively find GCD of two given number
	find_gcd(n1, n2) {
		if (n1 == 0) {
			return n2;
		}
		return this.find_gcd(n2 % n1, n1);
	}
	lcm(n1, n2) {
		//Get LCM of two number
		var result = parseInt((n1 *n2) / this.find_gcd(n1, n2));
		process.stdout.write("\nNumbers (" + n1 + " " + n2 + ") : LCM : " + result);
	}
}

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

Output

Numbers (24 75) : LCM : 600
Numbers (45 30) : LCM : 90
Numbers (21 49) : LCM : 147
Numbers (15 20) : LCM : 60
Numbers (16 40) : LCM : 80


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