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

A c# program that calculates the greatest common divisor (gcd) and least common multiple (lcm) of two numbers using an online compiler. The program takes user input for two numbers and displays their gcd and lcm. The algorithm for finding gcd uses the euclidean method, while lcm is calculated using the formula lcm(a, b) = |a*b| / gcd(a, b). This code snippet can be useful for computer science students, particularly those studying programming and algorithms.

Typology: Exercises

2021/2022

Uploaded on 06/03/2022

jj-toraja
jj-toraja 🇵🇭

5 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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