answersLogoWhite

0


Best Answer

import java.io.*;

public class quadratic

{

private String str;

public quadratic()

{

str = "";

}

public quadratic(String str)

{

this.str = str;

}

public void accept()throws IOException

{

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the equation in terms of x \nEg: 1x^2+4x+5=0 \nAnd you will receive the possible results of x");

str= br.readLine();

}

public void solve()

{

String A[]=new String [8];

int l=str.length();

int j=0;

char c=' ';

String w="";

for(int i=0;i<7;i++)

{

while(true)

{

c=str.charAt(j);

if(c!='+'&&c!='-'&&c!='=')

w=w+c;

else

{

if(i==4)

A[i]="0"+w;

else

A[i]=w;

i++;

if(i>6)

break;

A[i]=" "+c;

j++;

break;

}

j++;

if(j>=l)

break;

}

w="";

}

System.out.println(A[0]);

System.out.println(A[2]);

System.out.println(A[6]);

int len=A[0].length();

String M=A[0].substring(0,(len-3));

len=A[2].length();

String B=A[2].substring(0,len-1);

int a=Integer.parseInt(M);

int b=Integer.parseInt(B);

int C=Integer.parseInt(A[4]);

if(A[3].equals(" -"))

C=-C;

if(A[1].equals(" -"))

b=-b;

double D=(double)Math.sqrt(b*b-4*a*C);

double ans1=(-b+D)/(2*a);

double ans2=(-b-D)/(2*a);

System.out.println("The values of x are: "+ans1+" and "+ans2);

}

public static void main(String args[])throws IOException

{

quadratic ob = new quadratic();

ob.accept();

ob.solve();

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

import java.util.*;

public class quadratic

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

String equation = "( ax^2 + bx + c = 0 1 )";

System.out.println("The equation is " + equation);

System.out.println("Please enter the values of coefficients a, b, c, separated by spaces");

System.out.println("a = "); double a = input.nextDouble();

System.out.println("b = "); double b = input.nextDouble();

System.out.println("c = "); double c = input.nextDouble();

System.out.println();

double discriminant = Math.pow(b,2) - 4*a*c;

double root1 = (-b + Math.sqrt(discriminant))/(2*a);

double root2 = (-b - Math.sqrt(discriminant))/(2*a);

System.out.println(" " + equation);

System.out.println("The coefficients you entered were " + "\nA = " + a + "\nB = " + b + "\nC = " + c);

System.out.println();

if(discriminant > 0)

{

System.out.println("Root1: " + root1);

System.out.println("Root2: " + root2);

}

else if(discriminant == 0)

{

System.out.println("Root: " + root1);

}

else

{

double root1complex = -b/(2*a);

double root1complex2 = Math.sqrt(-discriminant)/(2*a);

System.out.println(root1complex + " + " + root1complex2 + " i ");

System.out.println(" " + " and ");

System.out.println(root1complex + " - " + root1complex2 + " i ");

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

void main()

{

float a,b,c,x1,x2,disc;

clrscr();

printf("Enter the co-efficients\n");

scanf("%f%f%f",&a,&b,&c);

disc=b*b-4*a*c;/*to find discriminant*/

if(disc>0)/*distinct roots*/

{

x1=(-b+sqrt(disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

printf("The roots are distinct\n");

exit(0);

}

if(disc==0)/*Equal roots*/

{

x1=x2=-b/(2*a);

printf("The roots are equal\n");

printf("x1=%f\nx2=%f\n",x1,x2);

exit(0);

}

x1=-b/(2*a);/*complex roots*/

x2=sqrt(fabs(disc))/(2*a);

printf("The roots are complex\n");

printf("The first root=%f+i%f\n",x1,x2);

printf("The second root=%f-i%f\n",x1,x2);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

class Quadratic

{

public static void findRoots(double a, double b, double c)

{

double disc;

disc = (b * b) - (4 * a * c);

if ( disc > 0 )

{

double Root1 = (- b + Math.sqrt(disc)) / (2 * a);

double Root2 = (- b - Math.sqrt(disc)) / (2 * a);

System.out.println("The roots are unequal and irrational " + Root1 + " and " + Root2);

}

else if ( disc == 0 )

{

double root = - b / (2 * a);

System.out.println("The roots are equal and rational " + root + " and " + root );

}

else if ( disc < 0 )

{

System.out.println("The roots are imaginary");

}

} // function is over

} // class is over

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to find the square root of the quadratic equation?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write an algorithm to find the root of quadratic equation?

Write an algorithm to find the root of quadratic equation


Algorithm to find the roots of a quadratic equation?

The easiest way to write a generic algorithm is to simply use the quadratic formula. If it is a computer program, ask the user for the coefficients a, b, and c of the generic equation ax2 + bx + c = 0, then just replace them in the quadratic formula.


How did you write each quadratic equation in standard form?

readuse the answer


How do you write a poem on the roots of the quadratic equation?

2000X=Y2KoverZzz?


Write a program in C programming language that computes the roots of the quadratic equation ax2 plus bx plus c?

Write your program and if you are having a problem post it here with a description of the problem you are having. What you are asking is for someone to do your homework for you.


How would you go about solving a quadratic equation?

Write the quadratic equation in the form ax2 + bx + c = 0 then the roots (solutions) of the equation are: [-b &plusmn; &radic;(b2 - 4*a*c)]/(2*a)


How do you write a pseudo code for find the quadratic equation?

computer scince


How do you write a quadratic equation in standard form?

ax2 + bx + c


When the roots are equal of a quadratic equation?

Write the quadratic equation in the form ax2 + bx + c = 0 The roots are equal if and only if b2 - 4ac = 0. The expression, b2-4ac is called the [quadratic] discriminant.


Write a program to find the square root of the quadratic equation using flow chart?

You don't need a flow chart for that; just use the quadratic formula directly; most programming languages have a square root function or method. You would only need to do this in many small steps if you use Assembly programming. The formulae would be something like this: x1 = (-b + sqrt(b^2 - 4*a*c)) / (2 * a) and x2 = (-b - sqrt(b^2 - 4*a*c)) / (2 * a) where a, b, and c are the coefficients of the quadratic equation in standard form, and x1 and x2 are the solutions you want.


Write an algorithm and draw a flowchart that will calculate the roots of quadratic equation?

dejene


What are all the ways you can solve a quadratic equation?

First, write the equation in standard form, i.e., put zero on the right. Then, depending on the case, you may have the following options:Factor the polynomialComplete the squareUse the quadratic formula