Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner input = new Scanner (System.in);
- int a = 0;
- int b = 0;
- int c = 0;
- System.out.println("Let´s solve quadratic equations");
- System.out.println("ax^2 + bx + c = 0");
- System.out.println("Please input your integer a: ");
- if (input.hasNextInt() ){
- a = input.nextInt();
- if (a ==0) {
- System.out.println("you cannot use a=0 for this quadratic calculator, the program will now close.");
- System.exit(0);
- }
- } else{
- System.out.println("You did not enter an integer, the program will now close.");
- System.exit(0);
- }
- System.out.println("Please input your integer b: ");
- if (input.hasNextInt() ){
- b = input.nextInt();
- } else{
- System.out.println("You did not enter an integer, the program will now close.");
- System.exit(0);
- }
- System.out.println("Please input your integer c: ");
- if (input.hasNextInt() ){
- c = input.nextInt();
- } else{
- System.out.println("You did not enter an integer, the program will now close.");
- System.exit(0);
- }
- double rootCheck = (b*b) -(4*a*c);
- double root1;
- double root2;
- if (rootCheck < 0) {
- System.out.println("Sorry your equation has no real roots");
- }else if ( rootCheck == 0){
- System.out.println("Your equation has 2 equal roots");
- root1 = (-b + rootCheck)/(2*a);
- System.out.println("Your roots are: " + root1 + " & " + root1);
- }else {
- System.out.println("Your equation has 2 real roots");
- root1 = (-b + rootCheck)/(2*a);
- root2 = (-b - rootCheck)/(2*a);
- System.out.println("Your roots are: " + root1 + " & " + root2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement