
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 1
This page cannot be seen from the preview
Don't miss anything!

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