0% found this document useful (0 votes)
7 views5 pages

Tran Vu Hoang

The document contains a series of programming exercises and code snippets related to various tasks such as calculating employee salaries, utility bills, and solving equations. It includes user input prompts and validation checks for date and numeric inputs. Additionally, it features functions for calculating costs based on different criteria and outputs formatted results.

Uploaded by

nhat48350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Tran Vu Hoang

The document contains a series of programming exercises and code snippets related to various tasks such as calculating employee salaries, utility bills, and solving equations. It includes user input prompts and validation checks for date and numeric inputs. Additionally, it features functions for calculating costs based on different criteria and outputs formatted results.

Uploaded by

nhat48350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Tên: Trần Vũ Hoàng Nguyễn Hữu Đại Mang Huỳnh Tấn Dũng
BTL1 7,8,9 4,5,6 1,2,3

BTL2 7,8,9 4,5,6 1,2,3

BTL3 7,8,9 4,5,6 1,2,3

BTL4 4,5 2,3 1

BTL5 6,7,8 3,4,5 1,2

BTL6 Full
2
Console.WriteLine("-----------------------------");
Console.Write("Nhap ngay: ");
int day = int.Parse(Console.ReadLine());
Câu 1.7: static void Bai7()
{ Console.Write("Nhap thang: ");
Console.WriteLine("\t\t\t\t\t\t\tBai7"); int month = int.Parse(Console.ReadLine());
int x;
bool isValidInput; Console.Write("Nhap nam: ");
do int year = int.Parse(Console.ReadLine());
{ bool isValid = true;
try
{ switch (month)
Console.Write("Nhap Vao Mot So Nguyen: "); {
x = int.Parse(Console.ReadLine()); case 1:
case 3:
if (x % 2 == 0) case 5:
{ case 7:
Console.WriteLine("So chan"); case 8:
} case 10:
else case 12:
{ if (day < 1 || day > 31) isValid = false;
Console.WriteLine("So le"); break;
}
isValidInput = true; case 4:
} case 6:
catch (FormatException) case 9:
{ case 11:
Console.WriteLine("Loi: Vui Long Nhap Mot So Nguyen Hop Le"); if (day < 1 || day > 30) isValid = false;
isValidInput = false; break;
}
} while (!isValidInput); case 2:
bool isLeapYear = (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
} if (day < 1 || day > (isLeapYear ? 29 : 28)) isValid = false;
Câu 1.8: static void Bai8() break;
{
double a; default:
Console.WriteLine("\t\t\t\t\t\t\tBai8"); isValid = false;
Console.WriteLine("Chuong Trinh Tinh Luong Cua Nhan Vien TNCT"); break;
Console.Write("Nhap So Thang TNCT: "); }
double canban = 1350000;
double hesoluong; if (!isValid || year < 1)
a = double.Parse(Console.ReadLine()); {
if (a < 12) Console.WriteLine("Ngay thang nam khong hop le!");
{ return;
Console.WriteLine($"So thang TNCT: {a} < 12"); }
hesoluong = 2.34; DateTime currentDate = new DateTime(year, month, day);
} DateTime yesterday = currentDate.AddDays(-1);
else if (a >= 12 && a < 36) DateTime tomorrow = currentDate.AddDays(1);
{ Console.WriteLine();
Console.WriteLine($"So thang TNCT: 12<={a}<36"); Console.WriteLine($"Ngay hom qua: {yesterday:dd/MM/yyyy}");
hesoluong = 2.67; Console.WriteLine($"Ngay hom nay: {currentDate:dd/MM/yyyy}");
} Console.WriteLine($"Ngay mai: {tomorrow:dd/MM/yyyy}");
else if (a >= 36 && a < 60) }
{ Câu 2.8: static void Bai8()
Console.WriteLine($"So thang TNCT: 36<={a}<60"); {
hesoluong = 3.0; while (true)
} {
else Console.WriteLine("CHUONG TRINH GIAI PHUONG TRINH");
{ Console.WriteLine("1. Phuong trinh bac 1 (ax + b = 0)");
Console.WriteLine($"So thang TNCT: {a}>=60"); Console.WriteLine("2. Phuong trinh bac 2 (ax^2 + bx + c = 0)");
hesoluong = 3.33; Console.WriteLine("3. Phuong trinh bac 3 (ax^3 + bx^2 + cx + d = 0)");
} Console.WriteLine("4. Thoat");
double luong = hesoluong * canban; Console.Write("Chon chuc nang (1-4): ");
Console.WriteLine($"He so luong: {hesoluong}");
Console.WriteLine($"Luong cua nhan vien: {luong.ToString("N0")} VND"); int choice;
Console.ReadKey(); if (!int.TryParse(Console.ReadLine(), out choice))
} {
Câu 1.9: static void Bai9() Console.WriteLine("Vui long nhap so!");
{ continue;
Console.WriteLine("\t\t\t\t\t\t\tBai9"); }
Console.WriteLine("\t\t\t\t\t\tChuong Trinh Tinh Tien Dien");
Console.Write($"Nhap so KW tieu thu: "); switch (choice)
double a = double.Parse(Console.ReadLine()); {
case 1:
double dongia; GiaiPTBac1();
if (a <= 100) break;
{ case 2:
Console.WriteLine($"donggia: {dongia = 2000}Dong"); GiaiPTBac2();
} break;
else if (a >= 101 && a <= 200) case 3:
{ GiaiPTBac3();
Console.WriteLine($"donggia: {dongia = 2500}Dong"); break;
} case 4:
else if (a >= 201 && a <= 300) Console.WriteLine("Ket thuc chuong trinh!");
{ return;
Console.WriteLine($"donggia: {dongia = 3000}Dong"); default:
} Console.WriteLine("Lua chon khong hop le!");
else break;
{ }
Console.WriteLine($"donggia: {dongia = 5000}Dong"); Console.WriteLine();
} }
double z = a * dongia; }
Console.WriteLine($"Tien dien: {z.ToString("N0")} VND");
Console.ReadKey(); static void GiaiPTBac1()
} {
Câu 1.10: static void Bai10() Console.Write("Nhap a: ");
{ double a = double.Parse(Console.ReadLine());
Console.WriteLine("\t\t\t\t\t\t\tBai10"); Console.Write("Nhap b: ");
Console.WriteLine("\t\t\t\t\t\tChuong Trinh Tinh Tien Karaoke"); double b = double.Parse(Console.ReadLine());

int bd, kt; if (a == 0)


while (true) {
{ Console.WriteLine(b == 0 ? "Vo so nghiem" : "Vo nghiem");
Console.Write("Nhap gio bat dau (8-24): "); }
if (int.TryParse(Console.ReadLine(), out bd) && bd >= 8 && bd <= 24) else
{ {
break; Console.WriteLine($"Nghiem x = {-b / a:F2}");
} }
Console.WriteLine("Loi: Vui long nhap gio bat dau hop le (8-24)!"); }
}
while (true) static void GiaiPTBac2()
{ {
Console.Write("Nhap gio ket thuc (8-24): "); Console.Write("Nhap a: ");
if (int.TryParse(Console.ReadLine(), out kt) && kt >= 8 && kt <= 24 && kt >= bd) double a = double.Parse(Console.ReadLine());
{ Console.Write("Nhap b: ");
break; double b = double.Parse(Console.ReadLine());
} Console.Write("Nhap c: ");
Console.WriteLine("Loi: Vui long nhap gio ket thuc hop le (8-24 và >= gio bat dau)!"); double c = double.Parse(Console.ReadLine());
}
double tgt = kt - bd; if (a == 0)
double tienThuePhong; {
if (tgt <= 3) GiaiPTBac1();
{ return;
tienThuePhong = tgt * 30000; }
}
else double delta = b * b - 4 * a * c;
{
tienThuePhong = 3 * 30000 + (tgt - 3) * 21000; if (delta < 0)
} {
if (bd >= 8 && kt <= 17) Console.WriteLine("Vo nghiem thuc");
{ }
tienThuePhong *= 0.9; else if (delta == 0)
} {
Console.WriteLine($"Thoi gian thue phong: {tgt} gio"); Console.WriteLine($"Nghiem kep x = {-b / (2 * a):F2}");
Console.WriteLine($"Tien thue phong: {tienThuePhong.ToString("N0")} VND"); }
else
} {
Câu 2.7: static void Bai7() double x1 = (-b + Math.Sqrt(delta)) / (2 * a);
{ double x2 = (-b - Math.Sqrt(delta)) / (2 * a);
Console.WriteLine("TINH NGAY HOM QUA VA NGAY MAI"); Console.WriteLine($"2 nghiem: x1 = {x1:F2}, x2 = {x2:F2}");
3
} donGia = 3000;
} break;
case "Canh":
static void GiaiPTBac3() donGia = 2500;
{ break;
Console.Write("Nhap a: "); case "Thịt":
double a = double.Parse(Console.ReadLine()); donGia = 8000;
Console.Write("Nhap b: "); break;
double b = double.Parse(Console.ReadLine()); case "Rau luộc":
Console.Write("Nhap c: "); donGia = 2000;
double c = double.Parse(Console.ReadLine()); break;
Console.Write("Nhap d: "); case "Chuối":
double d = double.Parse(Console.ReadLine()); donGia = 2000;
break;
if (a == 0) case "Trà đá":
{ donGia = 1000;
GiaiPTBac2(); break;
return; }
} int thanhTien = donGia * quantities[i];
tongTien += thanhTien;
Console.WriteLine($"| {i + 1,3} | {selectedItems[i],-10} | {donGia,6} | {quantities[i],8} | {thanhTien,10} |");
double delta = 18 * a * b * c * d - 4 * b * b * b * d + b * b * c * c - 4 * a * c * c * c - 27 * a * a * d * d; }
double k = b / (3 * a); Console.WriteLine("------------------------------------------------------------");
double p = (3 * a * c - b * b) / (3 * a * a); Console.WriteLine($"| Tổng | | | | {tongTien,10} |");
double q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a); Console.WriteLine("------------------------------------------------------------");
Console.WriteLine("\nBằng chữ: " + SoTienBangChu(tongTien));
if (delta > 0) }
{ static string SoTienBangChu(int soTien)
{
double u = Math.Sqrt(-q / 2 + Math.Sqrt(q * q / 4 + p * p * p / 27)); if (soTien < 0)
double v = Math.Sqrt(-q / 2 - Math.Sqrt(q * q / 4 + p * p * p / 27)); return "Không hợp lệ";
double x = u + v - k; if (soTien == 0)
Console.WriteLine($"Phuong trinh co 1 nghiem thuc: x = {x:F2}"); return "Không đồng";
} string[] donVi = { "", "nghìn", "triệu", "tỷ" };
else if (delta == 0) string[] so = { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín" };
{ string[] muoi = { "mười", "mười một", "mười hai", "mười ba", "mười bốn", "mười lăm", "mười sáu", "mười bảy", "mười tám", "mười chín" };
string result = "";
double x1 = (9 * a * d - b * c) / (2 * (b * b - 3 * a * c)) - k; int i = 0;
double x2 = (4 * a * b * c - 9 * a * a * d - b * b * b) / (a * (b * b - 3 * a * c)) - k; while (soTien > 0)
Console.WriteLine($"Phuong trinh co nghiem kep: x1 = x2 = {x1:F2}"); {
Console.WriteLine($"Va nghiem don: x3 = {x2:F2}"); int chunk = soTien % 1000;
} soTien /= 1000;
else
{ if (chunk != 0)
{
double theta = Math.Acos(3 * q * Math.Sqrt(-3 / p) / (2 * p)); string chunkStr = "";
double x1 = 2 * Math.Sqrt(-p / 3) * Math.Cos(theta / 3) - k; if (chunk >= 100)
double x2 = 2 * Math.Sqrt(-p / 3) * Math.Cos((theta + 2 * Math.PI) / 3) - k; {
double x3 = 2 * Math.Sqrt(-p / 3) * Math.Cos((theta + 4 * Math.PI) / 3) - k; chunkStr += so[chunk / 100] + " trăm ";
Console.WriteLine("Phuong trinh co 3 nghiem thuc:"); chunk %= 100;
Console.WriteLine($"x1 = {x1:F2}"); }
Console.WriteLine($"x2 = {x2:F2}"); if (chunk >= 20)
Console.WriteLine($"x3 = {x3:F2}"); {
} chunkStr += so[chunk / 10] + " mươi ";
} chunk %= 10;
Câu 2.9: static void Bai9() if (chunk > 0)
{ {
Console.OutputEncoding = System.Text.Encoding.UTF8; if (chunk == 1)
Console.WriteLine("MENU QUÁN CƠM SINH VIÊN"); chunkStr += "mốt ";
Console.WriteLine("1. Cơm - 3,000đ"); else if (chunk == 4)
Console.WriteLine("2. Canh - 2,500đ"); chunkStr += "tư ";
Console.WriteLine("3. Thịt - 8,000đ"); else if (chunk == 5)
Console.WriteLine("4. Rau luộc - 2,000đ"); chunkStr += "lăm ";
Console.WriteLine("5. Chuối - 2,000đ"); else
Console.WriteLine("6. Trà đá - 1,000đ"); chunkStr += so[chunk] + " ";
Console.WriteLine("0. Kết thúc và in hóa đơn"); }
Console.WriteLine(); }
else if (chunk >= 10)
List<string> selectedItems = new List<string>(); {
List<int> quantities = new List<int>(); chunkStr += muoi[chunk - 10] + " ";
chunk = 0;
while (true) }
{ else if (chunk > 0)
Console.Write("Nhập số tương ứng với món (0 để kết thúc): "); {
int choice; chunkStr += so[chunk] + " ";
if (!int.TryParse(Console.ReadLine(), out choice)) }
{
Console.WriteLine("Vui lòng nhập số!"); chunkStr += donVi[i] + " ";
continue; result = chunkStr + result;
} }
if (choice == 0) break;
i++;
if (choice < 1 || choice > 6) }
{
Console.WriteLine("Lựa chọn không hợp lệ. Vui lòng chọn từ 1-6!"); return result.Trim() + " đồng";
continue; }
} }
Câu 3.7: static void bai7()
Console.Write("Nhập số lượng: "); {
int quantity; Console.Write("Nhập số nguyên dương n: ");
if (!int.TryParse(Console.ReadLine(), out quantity) || quantity <= 0) int n = int.Parse(Console.ReadLine());
{ double sum = 0.0;
Console.WriteLine("Số lượng không hợp lệ!");
continue; for (int i = 1; i <= n; i++)
} {
string itemName = ""; sum += 1.0 / i;
switch (choice) }
{
case 1: Console.WriteLine($"Tổng nghịch đảo của {n} số nguyên đầu tiên là: {sum:F4}");
itemName = "Cơm"; Console.ReadLine();
break; }
case 2: Câu 3.8: static void bai8()
itemName = "Canh"; {
break; int n;
case 3: do
itemName = "Thịt"; {
break; Console.Write("Nhập số nguyên dương n: ");
case 4: n = int.Parse(Console.ReadLine());
itemName = "Rau luộc"; if (n <= 0)
break; {
case 5: Console.WriteLine("Số nhập vào phải là nguyên dương. Vui lòng nhập lại!");
itemName = "Chuối"; }
break; } while (n <= 0);
case 6:
itemName = "Trà đá";
break; int S1 = 0;
} for (int i = 1; i <= n; i++)
selectedItems.Add(itemName); {
quantities.Add(quantity); S1 += i;
Console.WriteLine($"Đã thêm {quantity} {itemName} vào hóa đơn.\n"); }
}
if (selectedItems.Count == 0)
{ int S2 = 0;
Console.WriteLine("Bạn chưa chọn món nào!"); for (int i = 1; i <= 2 * n - 1; i += 2)
return; {
} S2 += i;
Console.WriteLine("\nHÓA ĐƠN: CƠM SINH VIÊN"); }
Console.WriteLine("(Ghi chú: Không ký nợ dưới mọi hình thức)");
Console.WriteLine();
Console.WriteLine("------------------------------------------------------------"); int S3 = 0;
Console.WriteLine("| STT | NỘI DUNG | ĐƠN GIÁ | SỐ LƯỢNG | THÀNH TIỀN |"); for (int i = 2; i <= 2 * n; i += 2)
Console.WriteLine("------------------------------------------------------------"); {
S3 += i;
int tongTien = 0; }
for (int i = 0; i < selectedItems.Count; i++)
{ Console.WriteLine($"Kết quả:");
int donGia = 0; Console.WriteLine($"S1 = 1 + 2 + 3 + ... + {n} = {S1}");
switch (selectedItems[i]) Console.WriteLine($"S2 = 1 + 3 + 5 + ... + {2 * n - 1} = {S2}");
{ Console.WriteLine($"S3 = 2 + 4 + 6 + ... + {2 * n} = {S3}");
case "Cơm": Console.ReadLine();
4
} int choice;
Câu 4.4: static void Main(string[] args) while (!int.TryParse(Console.ReadLine(), out choice) || choice < 0 || choice > 2)
{ {
Console.WriteLine("nhap mot so "); Console.Write("Vui lòng nhập lại (0-2): ");
int so = int.Parse(Console.ReadLine()); }
while (so < 0)
{ switch (choice)
Console.WriteLine("vui long nhap lai "); {
so = int.Parse(Console.ReadLine()); case 1:
} GiaiPhuongTrinhBacHai();
int sobandau = so; break;
case 2:
int t = 0; GiaiHePhuongTrinhHaiAn();
while (so > 0) break;
{ case 0:
int i = so % 10; Console.WriteLine("Đã thoát chương trình!");
t += i; return;
so /= 10; }
} }
Console.WriteLine($"tong cac chu so {sobandau} = {t}"); }
Console.ReadKey(); static void GiaiPhuongTrinhBacHai()
} {
Câu 4.5: static void Main(string[] args) Console.WriteLine("----- GIẢI PHƯƠNG TRÌNH BẬC 2: ax² + bx + c = 0 -----");
{ Console.Write("Nhập hệ số a: ");
Console.OutputEncoding = Encoding.UTF8; double a = double.Parse(Console.ReadLine());
Console.WriteLine("MENU ĐỒ UỐNG"); Console.Write("Nhập hệ số b: ");
Console.WriteLine("1. Cafe - 25,000 VND"); double b = double.Parse(Console.ReadLine());
Console.WriteLine("2. Cam vắt - 30,000 VND"); Console.Write("Nhập hệ số c: ");
Console.WriteLine("3. Nước ép cà rốt - 35,000 VND"); double c = double.Parse(Console.ReadLine());
Console.WriteLine("4. Nước lọc - 10,000 VND");
Console.WriteLine("5. Nước dừa - 20,000 VND"); if (a == 0)
Console.WriteLine("0. Thoát chương trình"); {

bool continueOrdering = true; if (b == 0)


{
while (continueOrdering) Console.WriteLine(c == 0 ? "PT có vô số nghiệm" : "PT vô nghiệm");
{ }
Console.Write("\nNhập số đồ uống bạn muốn (1-5) hoặc 0 để thoát: "); else
int choice; {
while (!int.TryParse(Console.ReadLine(), out choice) || (choice < 0 || choice > 5)) Console.WriteLine($"Nghiệm x = {-c / b}");
{ }
Console.Write("Lựa chọn không hợp lệ. Vui lòng nhập lại (0-5): "); }
} else
{
if (choice == 0) double delta = b * b - 4 * a * c;
{ if (delta < 0)
continueOrdering = false; {
Console.WriteLine("Cảm ơn bạn đã sử dụng dịch vụ!"); Console.WriteLine("PT vô nghiệm thực");
continue; }
} else if (delta == 0)
{
Console.WriteLine($"Nghiệm kép x = {-b / (2 * a)}");
string drinkName = ""; }
int price = 0; else
{
switch (choice) double x1 = (-b + Math.Sqrt(delta)) / (2 * a);
{ double x2 = (-b - Math.Sqrt(delta)) / (2 * a);
case 1: Console.WriteLine($"2 nghiệm phân biệt: x1 = {x1}, x2 = {x2}");
drinkName = "Cafe"; }
price = 25000; }
break; }
case 2:
drinkName = "Cam vắt";
price = 30000; static void GiaiHePhuongTrinhHaiAn()
break; {
case 3: Console.WriteLine("----- GIẢI HỆ PHƯƠNG TRÌNH 2 ẨN -----");
drinkName = "Nước ép cà rốt"; Console.Write("Nhập a1: ");
price = 35000; double a1 = double.Parse(Console.ReadLine());
break; Console.Write("Nhập b1: ");
case 4: double b1 = double.Parse(Console.ReadLine());
drinkName = "Nước lọc"; Console.Write("Nhập c1: ");
price = 10000; double c1 = double.Parse(Console.ReadLine());
break; Console.Write("Nhập a2: ");
case 5: double a2 = double.Parse(Console.ReadLine());
drinkName = "Nước dừa"; Console.Write("Nhập b2: ");
price = 20000; double b2 = double.Parse(Console.ReadLine());
break; Console.Write("Nhập c2: ");
} double c2 = double.Parse(Console.ReadLine());

Console.Write($"Bạn đã chọn {drinkName}. Nhập số lượng: "); double D = a1 * b2 - a2 * b1;


int quantity; double Dx = c1 * b2 - c2 * b1;
double Dy = a1 * c2 - a2 * c1;

while (!int.TryParse(Console.ReadLine(), out quantity) || quantity <= 0) if (D != 0)


{ {
Console.Write("Số lượng không hợp lệ. Vui lòng nhập lại: "); double x = Dx / D;
} double y = Dy / D;
Console.WriteLine($"Nghiệm: x = {x}, y = {y}");
}
int total = price * quantity; else
Console.WriteLine($"\nĐơn giá: {price.ToString("N0")} VND"); {
Console.WriteLine($"Số lượng: {quantity}"); Console.WriteLine(Dx == 0 && Dy == 0 ? "Hệ có vô số nghiệm" : "Hệ vô nghiệm");
Console.WriteLine($"Thành tiền: {total.ToString("N0")} VND"); }
}
} Câu 6: static int N = 0;
Câu 5.6: static void Main() static float[] arr;
{
Console.OutputEncoding = Encoding.UTF8; static void Main(string[] args)
Console.Write("Nhập n: "); {
int n = int.Parse(Console.ReadLine()); Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine($"Tổng chữ số = {TongChuSo(n)}"); int choice;
} do
static int TongChuSo(int n) {
{ Console.WriteLine("\n----- MENU -----");
int tong = 0; Console.WriteLine("1. Nhập một số nguyên N (0 < N < 50)");
while (n != 0) Console.WriteLine("2. Nhập một mảng gồm N số thực");
{ Console.WriteLine("3. Tìm số lớn nhất trong mảng");
tong += n % 10; Console.WriteLine("4. Tìm số dương lẻ nhỏ nhất trong mảng");
n /= 10; Console.WriteLine("5. Tìm số dương chẵn lớn nhất trong mảng");
} Console.WriteLine("6. Tính trung bình cộng các phần tử mảng");
return tong; Console.WriteLine("7. Tìm những phần tử lớn hơn trung bình cộng");
} Console.WriteLine("8. Sắp xếp mảng theo trật tự tăng dần");
} Console.WriteLine("9. Sắp xếp mảng theo trật tự giảm dần");
} Console.WriteLine("0. Thoát chương trình");
Câu 5.7: static void Main() Console.Write("Nhập lựa chọn của bạn: ");
{
Console.OutputEncoding = Encoding.UTF8; while (!int.TryParse(Console.ReadLine(), out choice))
Console.Write("Nhập x và y: "); {
string[] input = Console.ReadLine().Split(); Console.Write("Vui lòng nhập số nguyên: ");
double x = double.Parse(input[0]); }
int y = int.Parse(input[1]);
Console.WriteLine($"{x}^{y} = {TinhLuyThua(x, y)}"); switch (choice)
} {
static double TinhLuyThua(double x, int y) case 1:
{ cau1();
return Math.Pow(x, y); break;
} case 2:
} cau2();
} break;
Câu 5.8: static void Main() case 3:
{ cau3();
Console.OutputEncoding = Encoding.UTF8; break;
while (true) case 4:
{ cau4();
Console.WriteLine("\n==== MENU CHỌN CHỨC NĂNG ===="); break;
Console.WriteLine("1. Giải phương trình bậc 2"); case 5:
Console.WriteLine("2. Giải hệ phương trình 2 ẩn"); cau5();
Console.WriteLine("0. Thoát chương trình"); break;
Console.Write("Lựa chọn của bạn: "); case 6:
cau6();
5
break; }
case 7: else
cau7(); {
break; Console.WriteLine("Không tìm thấy số dương chẵn trong mảng.");
case 8: }
cau8(); }
break;
case 9: static void cau6()
cau9(); {
break; if (arr == null || arr.Length == 0)
case 0: {
Console.WriteLine("Thoát chương trình."); Console.WriteLine("Mảng chưa được khởi tạo. Vui lòng nhập mảng trước (chọn menu 2).");
break; return;
default: }
Console.WriteLine("Lựa chọn không hợp lệ. Vui lòng chọn lại.");
break; float sum = 0;
} foreach (float num in arr)
{
} while (choice != 0); sum += num;
} }

static void cau1() float average = sum / arr.Length;


{ Console.WriteLine($"Trung bình cộng các phần tử mảng là: {average}");
do }
{
Console.Write("Nhập số nguyên N (0 < N < 50): "); static void cau7()
while (!int.TryParse(Console.ReadLine(), out N)) {
{ if (arr == null || arr.Length == 0)
Console.Write("Vui lòng nhập số nguyên: "); {
} Console.WriteLine("Mảng chưa được khởi tạo. Vui lòng nhập mảng trước (chọn menu 2).");
return;
if (N <= 0 || N >= 50) }
{
Console.WriteLine("N phải thỏa mãn 0 < N < 50. Vui lòng nhập lại."); float sum = 0;
} foreach (float num in arr)
} while (N <= 0 || N >= 50); {
} sum += num;
}
static void cau2() float average = sum / arr.Length;
{
if (N == 0) Console.WriteLine($"Các phần tử lớn hơn trung bình cộng ({average}):");
{ bool found = false;
Console.WriteLine("Vui lòng nhập N trước (chọn menu 1)."); for (int i = 0; i < arr.Length; i++)
return; {
} if (arr[i] > average)
{
arr = new float[N]; Console.WriteLine($"Phần tử thứ {i + 1}: {arr[i]}");
Console.WriteLine($"Nhập {N} số thực:"); found = true;
}
for (int i = 0; i < N; i++) }
{
Console.Write($"Nhập phần tử thứ {i + 1}: "); if (!found)
while (!float.TryParse(Console.ReadLine(), out arr[i])) {
{ Console.WriteLine("Không có phần tử nào lớn hơn trung bình cộng.");
Console.Write("Vui lòng nhập số thực: "); }
} }
}
static void cau8()
Console.WriteLine("Đã nhập mảng thành công."); {
} if (arr == null || arr.Length == 0)
{
static void cau3() Console.WriteLine("Mảng chưa được khởi tạo. Vui lòng nhập mảng trước (chọn menu 2).");
{ return;
if (arr == null || arr.Length == 0) }
{
Console.WriteLine("Mảng chưa được khởi tạo. Vui lòng nhập mảng trước (chọn menu 2)."); Array.Sort(arr);
return; Console.WriteLine("Mảng sau khi sắp xếp tăng dần:");
} PrintArray();
}
float max = arr[0];
for (int i = 1; i < arr.Length; i++) static void cau9()
{ {
if (arr[i] > max) if (arr == null || arr.Length == 0)
{ {
max = arr[i]; Console.WriteLine("Mảng chưa được khởi tạo. Vui lòng nhập mảng trước (chọn menu 2).");
} return;
} }

Console.WriteLine($"Số lớn nhất trong mảng là: {max}"); Array.Sort(arr);


} Array.Reverse(arr);
Console.WriteLine("Mảng sau khi sắp xếp giảm dần:");
static void cau4() PrintArray();
{ }
if (arr == null || arr.Length == 0)
{ static void PrintArray()
Console.WriteLine("Mảng chưa được khởi tạo. Vui lòng nhập mảng trước (chọn menu 2)."); {
return; for (int i = 0; i < arr.Length; i++)
} {
Console.Write(arr[i] + " ");
float min = float.MaxValue; }
bool found = false; Console.WriteLine();
}
foreach (float num in arr) }
{ }
if (num > 0 && num % 2 != 0)
{
if (num < min)
{
min = num;
found = true;
}
}
}

if (found)
{
Console.WriteLine($"Số dương lẻ nhỏ nhất trong mảng là: {min}");
}
else
{
Console.WriteLine("Không tìm thấy số dương lẻ trong mảng.");
}
}

static void cau5()


{
if (arr == null || arr.Length == 0)
{
Console.WriteLine("Mảng chưa được khởi tạo. Vui lòng nhập mảng trước (chọn menu 2).");
return;
}

float max = float.MinValue;


bool found = false;

foreach (float num in arr)


{
if (num > 0 && num % 2 == 0)
{
if (num > max)
{
max = num;
found = true;
}
}
}

if (found)
{
Console.WriteLine($"Số dương chẵn lớn nhất trong mảng là: {max}");

You might also like