- import java.util.InputMismatchException;
- import java.util.Scanner;
-
- public class TroisiemeDeg {
-
-
- public static void main(String[] args) {
-
- boolean continuation=true;
- Scanner scan = new Scanner(System.in);
- double a,b,c,d,gDelta,x,y,z,im,re,u,v,p,q,m,n,theta,k;
- char verif;
-
- do{
- System.out.println("\n\nax^3+bx^2+cx+d=0");
- System.out.println("Veuillez entrer les coefficients ");
-
- try{
-
- System.out.print("a = ");
-
- a= scan.nextDouble();
- while(a==0) {
- System.out.println("a est different de 0!");
- System.out.print("a = ");
- a= scan.nextDouble();
- }
- System.out.print("b = ");
- b= scan.nextDouble();
- System.out.print("c = ");
- c= scan.nextDouble();
- System.out.print("d = ");
- d= scan.nextDouble();
- System.out.println("\nl'equation a resoudre est : "+a+"x^3+"+b+"x^2+"+c+"x+"+d+"=0\n");
-
- p=(c/a)-(Math.pow(b, 2.0)/(3*Math.pow(a, 2.0)));
- q=((2*Math.pow(b, 3.0))/(27*Math.pow(a, 3.0)))-((b*c)/(3*Math.pow(a, 2.0)))+(d/a);
- gDelta=4*Math.pow(p, 3.0)+27*Math.pow(q, 2.0);
- m=((-q)/2)+(0.5)*Math.sqrt(gDelta/27);
- n=((-q)/2)-(0.5)*Math.sqrt(gDelta/27);
- u=Math.pow(Math.abs(m), 1.0/3);
- v=Math.pow(Math.abs(n), 1.0/3);
-
- if(gDelta>0)
- {
- if (m<0) u=-u;
- if (n<0) v=-v;
- x=u+v;
- x+=(-b)/(3*a);
- re=(-x)/2;
- im=(Math.sqrt(3)/2)*(u-v);
- re+=(-b)/(3*a);
- System.out.println("1 racine reelle:\n x = "+x+"");
- System.out.println("2 racines complexes:\n" +
- " y = "+re+"-"+im+"i , z = "+re+"+"+im+"i");
-
- }
-
-
- else if(gDelta==0)
- {
-
- if(b==0&c==0&&d==0)
- System.out.println("1 racine reelle de multiplicite 3:\n x = y = z = 0 ");
- else{
- x=(3*q)/p;
- x+=(-b)/(3*a);
- y=(-3*q)/(2*p);
- y+=(-b)/(3*a);
-
- System.out.println("1 racine reelle simple:\n x = "+x+"");
- System.out.println("1 racine reelle double:\n y = z = "+y+"");
- }
-
- }
- else
- {
- k= (3*q)/((2*p)*Math.sqrt((-p)/3));
- theta=Math.acos(k);
- x= 2*Math.sqrt((-p)/3)*Math.cos(theta/3);
- y= 2*Math.sqrt((-p)/3)*Math.cos((theta+2*Math.PI)/3);
- z= 2*Math.sqrt((-p)/3)*Math.cos((theta+4*Math.PI)/3);
- if (x>-1E-6 && x< 1E-6) x=0.0;
- if (y>-1E-6 && y< 1E-6) y=0.0;
- if (z>-1E-6 && z< 1E-6) z=0.0;
- x+=(-b)/(3*a);
- y+=(-b)/(3*a);
- z+=(-b)/(3*a);
- System.out.println("3 racines reelles:\n x = "+x+" , y = "+y+" , z = "+z+"");
-
-
- }
-
-
- }
-
- catch (InputMismatchException e)
- {
- System.out.println("Vous devez introduire un nombre");
-
- }
-
- scan = new Scanner(System.in);
- System.out.println("\nVoulez-vous continuer ? [o/n/?]");
- verif=scan.next().charAt(0);
- if (verif != 'o' && verif != 'O' && verif !='0')
- continuation = false;
-
-
- }while(continuation);
-
-
-
-
- }
-
- }
import java.util.InputMismatchException;
import java.util.Scanner;
public class TroisiemeDeg {
public static void main(String[] args) {
boolean continuation=true;
Scanner scan = new Scanner(System.in);
double a,b,c,d,gDelta,x,y,z,im,re,u,v,p,q,m,n,theta,k;
char verif;
do{
System.out.println("\n\nax^3+bx^2+cx+d=0");
System.out.println("Veuillez entrer les coefficients ");
try{
System.out.print("a = ");
a= scan.nextDouble();
while(a==0) {
System.out.println("a est different de 0!");
System.out.print("a = ");
a= scan.nextDouble();
}
System.out.print("b = ");
b= scan.nextDouble();
System.out.print("c = ");
c= scan.nextDouble();
System.out.print("d = ");
d= scan.nextDouble();
System.out.println("\nl'equation a resoudre est : "+a+"x^3+"+b+"x^2+"+c+"x+"+d+"=0\n");
p=(c/a)-(Math.pow(b, 2.0)/(3*Math.pow(a, 2.0)));
q=((2*Math.pow(b, 3.0))/(27*Math.pow(a, 3.0)))-((b*c)/(3*Math.pow(a, 2.0)))+(d/a);
gDelta=4*Math.pow(p, 3.0)+27*Math.pow(q, 2.0);
m=((-q)/2)+(0.5)*Math.sqrt(gDelta/27);
n=((-q)/2)-(0.5)*Math.sqrt(gDelta/27);
u=Math.pow(Math.abs(m), 1.0/3);
v=Math.pow(Math.abs(n), 1.0/3);
if(gDelta>0)
{
if (m<0) u=-u;
if (n<0) v=-v;
x=u+v;
x+=(-b)/(3*a);
re=(-x)/2;
im=(Math.sqrt(3)/2)*(u-v);
re+=(-b)/(3*a);
System.out.println("1 racine reelle:\n x = "+x+"");
System.out.println("2 racines complexes:\n" +
" y = "+re+"-"+im+"i , z = "+re+"+"+im+"i");
}
else if(gDelta==0)
{
if(b==0&c==0&&d==0)
System.out.println("1 racine reelle de multiplicite 3:\n x = y = z = 0 ");
else{
x=(3*q)/p;
x+=(-b)/(3*a);
y=(-3*q)/(2*p);
y+=(-b)/(3*a);
System.out.println("1 racine reelle simple:\n x = "+x+"");
System.out.println("1 racine reelle double:\n y = z = "+y+"");
}
}
else
{
k= (3*q)/((2*p)*Math.sqrt((-p)/3));
theta=Math.acos(k);
x= 2*Math.sqrt((-p)/3)*Math.cos(theta/3);
y= 2*Math.sqrt((-p)/3)*Math.cos((theta+2*Math.PI)/3);
z= 2*Math.sqrt((-p)/3)*Math.cos((theta+4*Math.PI)/3);
if (x>-1E-6 && x< 1E-6) x=0.0;
if (y>-1E-6 && y< 1E-6) y=0.0;
if (z>-1E-6 && z< 1E-6) z=0.0;
x+=(-b)/(3*a);
y+=(-b)/(3*a);
z+=(-b)/(3*a);
System.out.println("3 racines reelles:\n x = "+x+" , y = "+y+" , z = "+z+"");
}
}
catch (InputMismatchException e)
{
System.out.println("Vous devez introduire un nombre");
}
scan = new Scanner(System.in);
System.out.println("\nVoulez-vous continuer ? [o/n/?]");
verif=scan.next().charAt(0);
if (verif != 'o' && verif != 'O' && verif !='0')
continuation = false;
}while(continuation);
}
}