SOAL : sempurnakan algoritma 3.3 dengan akar imajiner berbentuk : a+bi dengan a dan b bilangan real dan bilangan harga :b2-4ac<0
sintax nya
#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 nilai(float p, float q){
xRiil=p;
xImajiner=q;}
float hasil();};
float imajiner::hasil(){
cout <<"Masukkan Koefisien a: "; cin>>x;
cout <<"Masukkan Koefisien b: "; cin>>y;
cout <<"Masukkan Koefisien c: "; 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.nilai(p,q);
satu.hasil();
return 0;
}
Se
Tidak ada komentar:
Posting Komentar