Untitled
unknown
csharp
7 days ago
1.2 kB
3
Indexable
using System; public class BaiTap67 { // Hàm tìm USCLN bằng đệ quy (Thuật toán Euclid) static int USCLN(int a, int b) { if (b == 0) { return a; // Điều kiện dừng } else { return USCLN(b, a % b); // Bước đệ quy } } public static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.UTF8; Console.InputEncoding = System.Text.Encoding.UTF8; Console.WriteLine("Tìm Ước số chung lớn nhất (USCLN) bằng đệ quy"); int soM, soN; Console.Write("Nhập số nguyên thứ nhất (>= 0): "); while (!int.TryParse(Console.ReadLine(), out soM) || soM < 0) { Console.Write("Số không hợp lệ. Nhập lại: "); } Console.Write("Nhập số nguyên thứ hai (>= 0): "); while (!int.TryParse(Console.ReadLine(), out soN) || soN < 0) { Console.Write("Số không hợp lệ. Nhập lại: "); } int ketQua = USCLN(Math.Abs(soM), Math.Abs(soN)); // Đảm bảo số không âm Console.WriteLine($"USCLN của {soM} và {soN} là: {ketQua}"); } }
Editor is loading...
Leave a Comment