//Java Program to Find Roots of a Quadratic Equation
import java.util.Scanner;
class Quadratic
{
public static void main(String[] args)
{
//double a = 2.3, b = 4, c = 5.6;
double a,b,c;
System.out.println("Enter a,b,c values:");
Scanner scan=new Scanner(System.in);
a=scan.nextFloat();
b=scan.nextFloat();
c=scan.nextFloat();
double root1, root2;
double determinant = b * b - 4 * a * c;
// condition for real and different roots
if(determinant > 0)
{
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.format("root1 = %.2f and root2 = %.2f", root1 , root2);
System.out.println("\nRoots are real and different");
}
// Condition for real and equal roots
else if(determinant == 0)
{
root1 = root2 = -b / (2 * a);
System.out.format("root1 = root2 = %.2f;", root1);
System.out.println("\nRoots are real and equal");
}
// If roots are not real
else
{
double realPart = -b / (2 *a);
double imaginaryPart = Math.sqrt(-determinant) / (2 * a);
System.out.format("root1 = %.2f+%.2fi and root2 = %.2f-%.2fi",realPart, imaginaryPart, realPart, imaginaryPart);
System.out.println("\nRoots are imaginary"); //complex and different
}
}
}
Output:
c) Five Bikers Compete in a race such that they drive at a constant speed which may or may not be the same as the other. To qualify the race, the speed of a racer must be more than the average speed of all 5 racers. Take as input the speed of each racer and print back the speed of qualifying racers.
Program:
import java.util.*;
class Race
{
public static void main(String a[])
{
int r1,r2,r3,r4,r5,avg;
Scanner s=new Scanner(System.in);
System.out.println("enter the speed of the r1 r2 r3 r4 and r5");
r1=s.nextInt();
r2=s.nextInt();
r3=s.nextInt();
r4=s.nextInt();
r5=s.nextInt();
avg=(r1+r2+r3+r4+r5)/5;
if(r1>avg)
{
System.out.println("first racer is qualified with a race speed "+r1+"\n");
}
else if(r2>avg)
{
System.out.println("second racer is qalified with a race speed "+r2);
}
else if(r3>avg)
{
System.out.println("third racer is qalified with a race speed "+r3);
}
else if(r4>avg)
{
System.out.println("fourth racer is qalified with a race speed "+r4);
}
else if(r5>avg)
{
System.out.println("fifth racer is qalified with a race speed "+r5);
}
else
{
System.out.println("no biker is qualified");
}
}
}
Output: