Sempurnakan algoritma 3.3. dengan akar imajiner berbentuk :
a + bi dengan a dan b bilangan real dan bila harga : b2-4ac<0
Jawab:
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
class imajiner{
private:
float xRiil;
float xImajiner;
public:
float x;
float y;
float z;
float n;
float a;
float a1;
float a2;
void jazz(float p, float q){
xRiil=p;
xImajiner=q;}
float hasil();};
float imajiner::hasil(){
cout <<"Masukkan Koefisien 1: "; cin>>x;
cout <<"Masukkan Koefisien 2: "; cin>>y;
cout <<"Masukkan Koefisien 3: "; cin>>z;
n = y * y - 4 * x * z;
cout << "Diskriminan (" << y <<
" * " << y << " - 4 * " << x <<
" + " << z <<") = " << n << endl;
cout << "Akar persamaan " << x
<<"x^2 + " << y << "x + " << z
<<" = 0:" << endl;
if (n > 0)
{
a1 = (-y+sqrt(n))/(2*x);
a2 = (-y-sqrt(n))/(2*x);
cout << "x1 = " << a1 << endl;
cout << "x2 = " << a2 << endl;
}
else if (n == 0)
{
a = -y/(2*x);
cout << "x1 = x2 = " << a <<
endl;
}
else
{
xRiil = -y/(2*x);
xImajiner = sqrt(-n)/(2*x);
cout << "Imajiner: " << endl;
cout << "x1 = " << xRiil <<
" + " << xImajiner << " * i "<< endl;
cout << "x1 = " << xRiil <<
" - " << xImajiner << " * i "<< endl;
}
}
int main(int argc, char *argv[])
{
imajiner satu;
float p,q;
satu.jazz(p,q);
satu.hasil();
return 0;
}
Tidak ada komentar:
Posting Komentar