Roots of quadratic equation
Send Feedback
Write a program to calculate the roots of a given quadratic equation -
Print roots specifying their nature. If roots are imaginary, no need to print the roots.
Print the nature of roots in the form of an integer -
Round off the roots and then print the integral part only i.e. without any decimal places.
You can assume that, input will always be a quadratic equation.
Input format :
Output format :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
- import java.util.Scanner;
- public class Main
- {
- public static void main(String[] args)
- {
- Scanner s = new Scanner (System.in);
- // System.out.println("enter the values a,b and c");
- int a = s.nextInt();
- int b = s.nextInt();
- int c = s.nextInt();
- double r1=0;
- double r2=0;
- int temp = b*b-4*a*c;
- r1 = (-b + Math.sqrt(temp)) / (2 * a);
- r2 = (-b - Math.sqrt(temp)) / (2 * a);
- if (temp<0)
- {
- System.out.println(-1);
- }
- else if(temp>0)
- {
- System.out.println(1);
- }
- else
- {
- System.out.println(0);
- }
- System.out.println();
- System.out.print(Math.round (r1)+" ");
- System.out.print(Math.round(r2));
- }
- }
No comments:
Post a Comment