Sure, here is a C# program to find the GCD of two numbers:
C#
using System;
public class GCD {
public static void Main() {
// Get two numbers from the user
Console.WriteLine("Enter two numbers: ");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
// Find the GCD using Euclid's algorithm
int gcd = 1;
while (num2 != 0) {
int temp = num1 % num2;
num1 = num2;
num2 = temp;
gcd = num1;
}
// Print the GCD
Console.WriteLine("The GCD of {0} and {1} is {2}", num1, num2, gcd);
}
}
This program will first prompt the user to enter two numbers. It will then use Euclid's algorithm to find the GCD of the two numbers. Euclid's algorithm works by repeatedly finding the remainder of the division of one number by the other. The algorithm terminates when the remainder is 0, which means that the two numbers are relatively prime. The GCD is then equal to the last non-zero remainder.
The program will then print the GCD of the two numbers.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a C# program to find the GCD of two numbers:
C#
This program will first prompt the user to enter two numbers. It will then use Euclid's algorithm to find the GCD of the two numbers. Euclid's algorithm works by repeatedly finding the remainder of the division of one number by the other. The algorithm terminates when the remainder is 0, which means that the two numbers are relatively prime. The GCD is then equal to the last non-zero remainder.
The program will then print the GCD of the two numbers.