1 Ocak 2015 Perşembe

Algebra(solve quadratic equations)

import java.util.*;

public class Algebra{
public static void main(String args[]){

Scanner input=new Scanner(System.in);
System.out.println("Please write 3 numbers to show ax^2+bx+c equation");

System.out.print("a=");
double a=input.nextDouble();
System.out.print("b=");
double b=input.nextDouble();
System.out.print("c=");
double c=input.nextDouble();

double discriminant=(b*b)-(4*a*c);

double r1=(-b+Math.pow(discriminant,0.5))/(2*a);

double r2=(-b-Math.pow(discriminant,0.5))/(2*a);

double r3=(-b/(2*a));

if(discriminant>0){
System.out.println("Equation has two roots,"+"\nr1="+r1+"\nr2="+r2);
}
if(discriminant==0){
System.out.println("Equation has one root,"+"\nr="+r3);
}
if(discriminant<0){
System.out.println("There is no root");
}
}
}

Hiç yorum yok:

Yorum Gönder