Mã: using System; using System.Collections.Generic; using System.Text; namespace Hinhchunhat { class Hinhchunhat { public float chieudai; public float chieurong; public Hinhchunhat() { chieudai = 0; chieurong = 0; } public void nhap() { Console.WriteLine("Moi nhap chieu dai"); chieudai = float.Parse(Console.ReadLine()); Console.WriteLine("Moi nhap chieu rong"); chieurong = float.Parse(Console.ReadLine()); } public float chuvi() { return (float)((chieudai + chieurong) * 2); } public float dientich() { return (float) (chieudai*chieurong); } } class Program { static void Main(string[] args) { Hinhchunhat hcn = new Hinhchunhat(); hcn.nhap(); Console.WriteLine("Chu vi hinh chu nhat la {0}",hcn.chuvi()); Console.WriteLine("Dien tich hinh chu nhat la {0}",hcn.dientich()); Console.ReadLine(); } } }
[C#] Tính CV và DT HCN Chương trình chính (Program.cs) Mã: using System; using System.Collections.Generic; using System.Text; namespace HINHCHUNHAT { class Program { static void Main(string[] args) { hinhchunhat hcn1 = new hinhchunhat(5, 4); Console.WriteLine("Dien tich cua hcn thu 1 la:" + hcn1.dientich()); Console.WriteLine("Chu vi cua hcn thu 1 la:" + hcn1.chuvi()); Console.ReadLine(); hinhchunhat hcn2 = new hinhchunhat(9, 7); Console.WriteLine("Dien tich cua hcn thu 2 la:" + hcn2.dientich()); Console.WriteLine("Chu vi cua hcn thu 2 la:" + hcn2.chuvi()); Console.ReadLine(); } } } Tạo 1 Class tên: HINHCHUNHAT Mã: using System; using System.Collections.Generic; using System.Text; namespace HINHCHUNHAT { class hinhchunhat { private int chieudai; private int chieurong; public hinhchunhat(int cr,int cd) { chieudai = cd; chieurong = cr; } public int dientich() { return chieudai * chieurong; } public int chuvi() { return 2* chieudai + chieurong; } } }
bổ sung thêm cái này nữa....dùng mảng Mã: using System; using System.Collections.Generic; using System.Text; namespace mangHCN { class Program { public float cd, cr, dientich; public void nhap() { Console.WriteLine("Nhap chieu dai: "); cd =Convert.ToSingle (Console.ReadLine()); Console.WriteLine("Nhap chieu rong: "); cr = Convert.ToSingle (Console.ReadLine()); } public void xuat() { Console.WriteLine("Chieu dai: "+cd+" Chieu rong: "+cr); Console.WriteLine("Dien tich: "+dt()); } public float dt() { dientich = cd * cr; return dientich; } static void Main(string[] args) { Program HCN = new Program(); Console.WriteLine("Moi nhap so HCN can tinh: "); int n = Convert .ToInt32 (Console.ReadLine()); Program[] m = new Program[n]; for (int i = 0; i <n; i++) { Console.WriteLine("HCN thu " + i); m[i] = new Program(); m[i].nhap(); } float max = 0; for (int i = 0; i < n; i++) { m[i].xuat(); if (m[i].dt () > max) { max = m[i].dt(); } } Console.WriteLine("HCN co dt LN la: " + max); Console.ReadLine(); } } }