0% found this document useful (0 votes)
24 views

Java Program 1

The document provides a Java program that takes user input for values a, b, and c and uses the quadratic formula to calculate the real solutions to the quadratic equation ax^2 + bx + c = 0. It prints the solutions, indicating whether they are real/not real or real and equal/distinct based on the discriminant. It includes example outputs for different inputs testing all the cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Java Program 1

The document provides a Java program that takes user input for values a, b, and c and uses the quadratic formula to calculate the real solutions to the quadratic equation ax^2 + bx + c = 0. It prints the solutions, indicating whether they are real/not real or real and equal/distinct based on the discriminant. It includes example outputs for different inputs testing all the cases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Write a java program that prints all real solutions to the quadratic
equation ax2+bx+c=0. Read in a, b, c and use the quadratic formula.

import java.util.Scanner;

class Quadratic
{
public static void main(String[] Strings)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of a: ");
double a = input.nextDouble();
System.out.print("Enter the value of b: ");
double b = input.nextDouble();
System.out.print("Enter the value of c: ");
double c = input.nextDouble();
double d= b * b – 4.0 * a * c;
if (d> 0)
{
double r1 = (-b + Math.sqrt(d)) / (2.0 * a);
double r2 = (-b - Math.sqrt(d)) / (2.0 * a);
System.out.println("The roots are real and distinct" + r1 + " and " + r2);
}
else if (d == 0)
{
double r1 = -b / (2.0 * a);
double r2 = -b / (2.0 * a);

System.out.println("The root are real and equal" + r1 + " and " + r2);
}
else
{
System.out.println("Roots are not real.");
}
}
}

OUTPUT 1:
Enter the value of a: 1
Enter the value of b: 1
Enter the value of c: 1
Roots are not real.
OUTPUT 2:
Enter the value of a: 1
Enter the value of b: 5
Enter the value of c: 2
The roots are -0.4384471871911697 and -4.561552812808831

OUTPUT 3:
Enter the value of a: 1
Enter the value of b: -2
Enter the value of c: 1
The root is 1.0

You might also like