Showing posts with label Finding roots. Show all posts
Showing posts with label Finding roots. Show all posts

Wednesday, June 29, 2022

Finding roots

 Finding roots

Problem Statement
Write a program to find roots of a quadratic equation using switch case.

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
    Scanner sc = new Scanner(System.in);
    
    float a = (float)sc.nextFloat();
    float b =(float) sc.nextFloat();
    float c = (float)sc.nextFloat();
    float d = (float)((b*b)-(4*a*c));
    float e=(float)(-b/(2*a));
    float f=0.0f;
    float g=0.0f;
    int h = 0;
    if(d>0){
        h=1;
    }
    else if(d<0){
        h=-1;
    }
    else{
        h=0;
    }

    switch(h){

      case 1 :f=(float)Math.sqrt(d);

            g=(float)f/(2*a);

            float r1=e+g;

            float r2=e-g;

            System.out.printf("%.2f",r1);

            System.out.println();

            System.out.printf("%.2f",r2);

            break;
     
      case -1 : f=(float)Math.sqrt(-d);

            g=(float)f/(2*a);

            String str1=String.format("%.2f",e);

            String str2=String.format("%.2f",g);

            System.out.printf(str1+"+i"+str2);

            System.out.println();

            System.out.printf(str1+"-i"+str2);

            break;

      case 0 :  

            f=(float)Math.sqrt(d);

            g=(float)f/(2*a);

            System.out.printf("%.2f",e);

            break;



    }
    }
}

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...