Selasa, 11 Juni 2013

Contoh Kode Program C++ Mencari Rumus Persamaan Kuadrat

Contoh Kode Program C++ Mencari Rumus Persamaan Kuadrat

//Program Persamaan Kuadrat;
// Rumus:   x1,x2   = (-b +- sqrt(D)) / (2*a)
#include
#include
int main()
{
    int a,b,c;
    float x1,x2,akar;
    int D;
    char quit;


   //cout<<"a= ";cin>>a;
   //cout<<"b= ";cin>>b;
   //cout<<"c= ";cin>>c;
    printf("a= ");scanf("%d",&a);
    printf("b= ");scanf("%d",&b);
    printf("c= ");scanf("%d",&c);
    printf("a= %d b= %d c= %d\n", a,b,c);
    D= b*b - 4*a*c;
  //  akar= sqrt(float b*b - 4*a*c);
    akar=sqrt(D);
    printf("D = %d\n",D);
    printf("akar D= %f\n",akar);

   // periksa nilai a
   if (a==0)
     {
        printf("nilai x1 dan x2 tidak terdefinisi karena a=0\n");
      }
   else // a<>0
     {
           //periksa nilai determinan
          if (D>=0)
          {
             if (D==0)
                {
                      x1= -b/ float (2*a);
                      x2=x1;
                      printf("x1 sama dengan x2 = %f\n",x1);
                }
                else
                {
                  x1=  -b+ sqrt(D)/float (2*a);
                  x2= -b-sqrt(D)/float (2*a);
                  printf("x1= %f x2= %f\n",x1,x2);
                }
          } else
               {
                  printf("Nilai determinan D<0 akar="" bernilai="" br="" imajiner="" n="" sehingga="">                  printf("x1 = %d + imajiner/ %d\n",-b,2*a);
                  printf("x2 = %d - imajiner/ %d\n",-b,2*a);
               }
     }

     cin>>quit;
     return 0;
}