Docsity

C# Program to Find Greatest Common Divisor and Least Common Multiple, Exercises of Javascript programming

Toggle sidebar
bg1
Learn Python
C# Online Compiler
Main.cs Output
// Online C# Editor for free
// Write, Edit and Run your C# code using C#
Online Compiler
//Rivas, Joy B.
//BSIT III-2
//Write a program that given two numbers finds
their greatest common divisor (GCD) and
their least common multiple (LCM). You may
use the formula LCM(a, b) = |a*b| / GCD(a,
b).
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.Write("Please enter the first
number: ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Please enter the second number:
");
int num2 = int.Parse(Console.ReadLine());
int gcd = GetGCD(num1, num2);
int lcm = GetLCM(num1, num2);
Console.WriteLine("Greatest Common Divisor ({0
,4} and {1,4}) = {2,6}", num1, num2, gcd);
Console.WriteLine("Least Common Multiple ({0,4}
and {1,4}) = {2,6}", num1, num2, lcm);
int GetGCD(int first, int second)
{
while (first != second)
{
if (first > second)
first = first - second;
if (second > first)
second = second - first;
}
return first;
}
int GetLCM(int first, int second)
{
return (first * second) / GetGCD(first,
second);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Run

Partial preview of the text

Download C# Program to Find Greatest Common Divisor and Least Common Multiple and more Exercises Javascript programming in PDF only on Docsity!

C# Online Compiler Learn Python

Main.cs Output

// Online C# Editor for free // Write, Edit and Run your C# code using C# Online Compiler

//Rivas, Joy B. //BSIT III- //Write a program that given two numbers finds their greatest common divisor (GCD) and their least common multiple (LCM). You may use the formula LCM(a, b) = |a*b| / GCD(a, b).

using System;

public class HelloWorld { public static void Main(string[] args) { Console.Write("Please enter the first number: "); int num1 = int.Parse(Console.ReadLine()); Console.Write("Please enter the second number: "); int num2 = int.Parse(Console.ReadLine());

int gcd = GetGCD(num1, num2); int lcm = GetLCM(num1, num2);

Console.WriteLine("Greatest Common Divisor ({ ,4} and {1,4}) = {2,6}", num1, num2, gcd); Console.WriteLine("Least Common Multiple ({0,4} and {1,4}) = {2,6}", num1, num2, lcm);

int GetGCD(int first, int second) { while (first != second) { if (first > second) first = first - second;

if (second > first) second = second - first; } return first; }

int GetLCM(int first, int second) { return (first * second) / GetGCD(first, second); } } }

Run