thieumao

Giải phương trình bậc 2

Jul 13th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. // MonoDevelop
  2. using System;
  3.  
  4. namespace GiaiPTBac2
  5. {
  6.     class MainClass
  7.     {
  8.         public static void Main (string[] args)
  9.         {
  10.             float a, b, c, d, x1, x2;
  11.             Console.WriteLine ("Giai Phuong Trinh Bac 2 ");
  12.             Console.WriteLine ("Nhap a: ");
  13.             a = float.Parse (Console.ReadLine ());
  14.             Console.WriteLine ("Nhap b: ");
  15.             b = float.Parse (Console.ReadLine ());
  16.             Console.WriteLine ("Nhap c: ");
  17.             c = float.Parse (Console.ReadLine ());
  18.             d = b * b - 4 * a * c;
  19.             if (a != 0) {
  20.                 if (d < 0) {
  21.                     Console.WriteLine ("PT vo nghiem");
  22.                 }
  23.                 if (d == 0) {
  24.                     x1 = -b / (2 * a);
  25.                     Console.WriteLine ("PT co nghiem kep x1 = x2 = {0}", x1);
  26.                 }
  27.                 if (d > 0) {
  28.                     Console.WriteLine ("PT co 2 nghiem phan biet");
  29.                     x1 = (-b + (float)Math.Sqrt (d)) / (2 * a);
  30.                     x2 = (-b - (float)Math.Sqrt (d)) / (2 * a);
  31.                     Console.WriteLine ("    x1 = {0}", x1);
  32.                     Console.WriteLine ("    x2 = {0}", x2);
  33.                 }
  34.             } else {
  35.                 if (b != 0) {
  36.                     x1 = -c / b;
  37.                     Console.WriteLine ("PT co nghiem x = {0}", x1);
  38.                 } else {
  39.                     if (c != 0) {
  40.                         Console.WriteLine ("PT vo nghiem");
  41.                     } else {
  42.                         Console.WriteLine ("PT vo so nghiem"); 
  43.                     }
  44.                 }
  45.             }
  46.             Console.ReadLine ();
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment