Friday, April 17, 2015

Bài 64

Bài 64: Giải phương trình bậc 2: ax^2 + bx + c = 0 (a # 0)

//========================================
// Cach 1
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
 float a,b,c;
 printf("a = ");
 scanf("%f", &a);
 printf("b = ");
 scanf("%f", &b);
 printf("c = ");
 scanf("%f", &c);
 float D = b*b - 4*a*c;
 if (D <=0)
  if (D == 0)
  {
   float x0 = -b/(2*a); // Luu y la phai co dau dong mo ngoac don (2*a)
   printf("Phuong trinh co nghiem duy nhat x0 = %f", x0);
  }
  else
  {
   printf("Phuong trinh vo nghiem");
  }
 else
 {
  float x1 = (-b-sqrt(D))/(2*a);
  float x2 = (-b+sqrt(D))/(2*a);
  printf("x1 = %f", x1);
  printf("x2 = %f", x2);
 }
}

//========================================
//Cach 2
#include <stdio.h>
#include <conio.h>
#include <math.h>

void GiaiPT(float,float,float);
void main()
{
 float a,b,c;
 printf("a = ");
 scanf("%f", &a);
 printf("b = ");
 scanf("%f", &b);
 printf("c = ");
 scanf("%f", &c);
 GiaiPT(a,b,c);
}

void GiaiPT(float d, float e, float f)
{
 float D = e*e - 4*d*f;
 if (D <=0)
  if (D == 0)
  {
   float x0 = -e/(2*d); // Luu y la phai co dau dong mo ngoac don (2*a)
   printf("Phuong trinh co nghiem duy nhat x0 = %f", x0);
  }
  else
  {
   printf("Phuong trinh vo nghiem");
  }
 else
 {
  float x1 = (-e-sqrt(D))/(2*d);
  float x2 = (-e+sqrt(D))/(2*d);
  printf("x1 = %f", x1);
  printf("x2 = %f", x2);
 }
}

No comments:

Post a Comment