Cấu trúc có điều kiện C Bài Tập vầ giải Giải1.Viết chương trình giải phương trình bậc 2 : ax2 + bx + c = 02.Viết chương trình giả 1 máy tính tay với các phép toán đơn giản như +,,, và lấy căn bậc 2.Gợi ý : Nhập phép toán (toán tử) và 2 số (toán hạng). Lưu ý nếu lấy căn bậc 2 thì chỉ nhập 1 toán hạng
CHƯƠNG 4: CẤU TRÚC ĐIỀU KIỆN Viết chương trình giải phương trình bậc : ax2 + bx + c = Console.WriteLine("cho hệ số a,b,c: "); int a = int.Parse (Console.ReadLine()); int b = int.Parse (Console.ReadLine()); int c = int.Parse (Console.ReadLine()); int denta; if ( a == ) { if ( b ==0) { if ( c == 0) Console.WriteLine( "PT có vô số nghiệm" ); else Console.WriteLine( "PT vô nghiệm" ); } else Console.WriteLine( " PT có nghiệm x = {0}",-c/b ); } else {denta = b*b - 4*a*c; if ( denta == 0) Console.WriteLine("PT có nghiệm x1 = x2 ={0}", -b/(2*a)); if ( denta < 0) Console.WriteLine( "PT vô nghiệm" ); else { Console.WriteLine( " PT có hai nghiệm là:"); Console.WriteLine( " x1 = {0}",(-b+Math.Sqrt(denta))/(2*a)); Console.WriteLine( " x2 = {0}",(-b-Math.Sqrt(denta))/(2*a)); } } Console.ReadLine() ; Viết chương trình giả máy tính tay với phép toán đơn giản +,-,*,/ lấy bậc Gợi ý : Nhập phép toán (toán tử) số (toán hạng) Lưu ý lấy bậc nhập toán hạng Console.WriteLine(" nhập giá trị a:"); float a = float.Parse(Console.ReadLine()); Console.WriteLine("nhập giá trị b:"); float b = float.Parse(Console.ReadLine()); Console.WriteLine(" nhập số từ dến tuong ung +,-,*,/ lay can bac hai"); int n = int.Parse(Console.ReadLine()); switch (n) { case 1: Console.WriteLine("{0}+{1}={2}",a,b,a+b); break; case 2: Console.WriteLine("{0}-0{1}={2}",a,b,a-b); break; case 3: Console.WriteLine("{0}*{1}={2}",a,b,a*b); break; case 4: if ( b==0) Console.WriteLine(" khong thuc hien duoc phep chia"); else Console.WriteLine("{0}/{1}={2}",a,b,a/b); break; case 5: Console.WriteLine("can bac hai cua {0} la {1}",a,Math.Sqrt(a)); break; } Console.ReadLine(); Viết chương trình giải hệ phương trình sau a1x + b1y = c1 a2x + b2y = c2 với tất trường hợp ( có nghiệm, vô nghiệm, vô số nghiệm) Console.WriteLine(" nhap a1,b1,c1,a2,b2,c2"); double a1,b1,c1,a2,b2,c2; a1=double.Parse(Console.ReadLine()); b1=double.Parse(Console.ReadLine()); c1=double.Parse(Console.ReadLine()); a2=double.Parse(Console.ReadLine()); b2=double.Parse(Console.ReadLine()); c2=double.Parse(Console.ReadLine()); if ((a1*b2-a2*b1)==0) { if ((c1*b2-c2*b1)==0) { if ((a1*c2-a2*c1)==0) Console.WriteLine("PT co vo so nghiem"); else Console.WriteLine("PT vo nghiem"); } else Console.WriteLine("Pt vo nghiem"); } else Console.WriteLine(" PT co nghiem la x ={0}, y={1}",((c1*b2-c2*b1)/(a1*b2-a2*b1)), ((a1*c2-a2*c1)/(a1*b2-a2*b1))); Console.ReadLine(); Nhập số a,b,c d In số lớn nhỏ số Console.WriteLine(" nhap so a,b,c,d"); float a = float.Parse(Console.ReadLine()); float b = float.Parse(Console.ReadLine()); float c = float.Parse(Console.ReadLine()); float d = float.Parse(Console.ReadLine()); float max, min; max = Math.Max(Math.Max(Math.Max(a,b),c),d); = Math.Min(Math.Min(Math.Min(a,b),c),d); Console.WriteLine(" gia tri lon nhat la: " + max); Console.WriteLine(" gia tri nho nhat la: " + min); Console.ReadLine(); Nhập số a,b,c d In lại số theo thứ tự giảm dần An phím từ A G > Phát âm tần số tương ứng với phím ấn A: La B : Si C : Do D : Re E : Mi F:Fa G: Sol Nhập năm dương lịch sau năm 1900, in tên năm âm lịch tương ứng (năm 1900 năm Canh Tý) Thập Can : Giáp, At, Bính , Đinh, Mậu, Kỷ , Canh , Tân, Nhâm, Quý Thập nhị Chi : Tý , Sửu, Dần, Mão, Thìn, Tỵ, Ngọ, Mùi, Thân, Dậu, Tuát, Hợi Console.WriteLine(" nhap nam sinh duong lich cua ban:"); int namsinh = int.Parse(Console.ReadLine()); int a,b; string s1 = " "; string s2 = " "; a = ( namsinh - 1900 + )%10; b = ( namsinh - 1900 + )%12; switch (a) { case : s1 = " giap";break; case : s1 = " At";break; case : s1 = " Binh";break; case : s1 = " Dinh";break; case : s1 = " Mau";break; case : s1 = " ky";break; case : s1 = " Canh";break; case : s1 = " Tan";break; case : s1 = " Nham";break; case : s1 = " Quy";break; } switch ( b) { case : s2 = " Ty";break; case : s2 = " Suu";break; case : s2 = " Dan";break; case : s2 = " Mao";break; case : s2 = " Thin";break; case : s2 = " Ty";break; case : s2 = " Ngo";break; case : s2 = " Mui";break; case : s2 = " Than";break; case 10 : s2 = " Dau";break; case 11 : s2 = " Tuat";break; case : s2 = " Hoi";break; } Console.WriteLine(" nam am lich tuong ung:" + s1+" " + s2); Console.ReadLine(); Nhập năm sinh người nam, năm sinh người nữ Cho biết người có kỵ tuổi hay không (Tứ hành xung gồm {Dần-Thân-Tỵ-Hợi},{Thìn-Tuất-Sửu-Mùi} {Tỵ-Ngọ-MẹoDậu} ) Console.WriteLine(" nhap nam sinh cua nam:"); int nam = int.Parse(Console.ReadLine()); Console.WriteLine(" nhap nam sinh cua nu:"); int nu = int.Parse(Console.ReadLine()); if ( nam == nu) Console.WriteLine(" cung tuoi nam duoi ma an"); else { if ( Math.Abs(nam - nu )%3 == 0) Console.WriteLine(" xung khac"); else {if(Math.Abs(nam-nu)%4 ==0) Console.WriteLine(" hop tuoi"); else Console.WriteLine(" binh thuong"); } } Console.ReadLine(); CHƯƠNG 5: CẤU TRÚC LẶP Nhập số N Nếu N>0 kiểm tra xem N có phải số nguyên tố hay không? Console.WriteLine(" nhap n"); int n = int.Parse(Console.ReadLine()); int i=0; if ( n ==1) Console.WriteLine(" n ko phai so NT"); else { if ( n< 4) Console.WriteLine(" n la so NT"); else { int canbachai=(int)Math.Sqrt(n); for (i=2;i canbachai) Console.WriteLine(" n la so NT"); } Console.ReadLine(); Nhập số N nguyên dương, sau in giá trị nhị phân N Console.WriteLine(" nhap so nguyen n:"); int n = int.Parse(Console.ReadLine()); string nhiphan = " " ; while ( n != ) { nhiphan = n%2 + nhiphan; n = n/2; } Console.WriteLine(" gia tri nhi phan:" + nhiphan); Console.ReadLine(); Viết chương trình nhập vào chiều dài cạnh hình chữ nhật, sau vẽ hình chữ nhật hình tạo dấu * Vd: 10 ********** ********** ********** Console.WriteLine(" nhap chieu dai canh hinh chu nhat"); int a=int.Parse(Console.ReadLine()); int b=int.Parse(Console.ReadLine()); int i,j; for (i=1;iN>0, sau in kết sau:tổ hợp M chọn N , chỉnh hợp M chọn N Console.WriteLine(" nhap so nguyen duong m,n(m>n>0)"); int m = int.Parse(Console.ReadLine()); int n = int.Parse(Console.ReadLine()); if ( m