C language programming experience for solving the quadratic equation of one element, experience of the quadratic equation of one element

Source: Internet
Author: User

C language programming experience for solving the quadratic equation of one element, experience of the quadratic equation of one element

I have seen a lot of small programs on the Internet for solving the quadratic equation of a single element. I feel very uncomfortable when using it, so I wrote it again.

First, let's recall the root formula of the quadratic equation:

 

 

At this time, the delta value is output at the same time, which is smaller than 0 and has no solution.

 

When delta is equal to 0, there is only one real number root.

 

Now we have a question: how can we determine integers?

To avoid mutual interference, two variables are used. sqr is used to determine whether it is an integer. If it is an integer, sqr1 is used for calculation.

How can I determine whether it is an integer? The int and double types can be used here. Determine whether sqr is an integer based on the size of (int) sqr and (int) (sqr + 0.9999999. ['Double' is generally accurate to the last six digits of the decimal point, so here we use 7 9]. Then it is forcibly converted to the int type. If it is an integer, (int) (sqr + 0.9999999) will not carry, x = y. Not an integer.

If it is an integer, sqrt (delta) is first calculated.

(2) When sqrt (delta) is not an integer

Yes. Keep the root number!

 

Note: This small program was initially annoyed by the wide variety of pitfalls in solving mathematical problems. I want to finish my homework quickly ~~ So I wrote this article by the way after I finished my homework on that dark evening.


How to use C language to compile a program for solving the quadratic equation of one element (refer to the steps)

# Include <stdio. h>
# Include <math. h>
Int main (void)
{
Int a, B, c; // defines three values of a quadratic equation;
Printf ("Please input the three parameters a B c of the quadratic equation of one element in sequence, separated by spaces \ n ");
Scanf ("% d", & a, & B, & c); // input three parameters of the quadratic equation
Double delta = B * B-4 * a * c; // defines the delta value as B * B-4 * a * c.
Double x1 = (-B + sqrt (delta)/(2 * );
Double x2 = (-B-sqrt (delta)/(2 * );

If (delta> 0) // when delta is greater than 0, the equation has two solutions.
{
Printf ("a quadratic equation has two solutions \ n ");
Printf ("first solution of the quadratic equation of one element, x1 = % f \ n", x1 );
Printf ("the second solution of the quadratic equation of one element, x2 = % f \ n", x2 );
}
Else if (delta = 0) // delta is equal to 0. The equation has two identical solutions.
{
Printf ("two identical solutions for the quadratic equation \ n ");
Printf ("the solution of the quadratic equation is x1 = x2 = % f \ n", x1 );
}
When else // delta is less than 0, there is no solution to the equation.
{
Printf ("no solution for the quadratic equation \ n ");
}

Return 0;
}

How can I write a program in C to implement the solution of a quadratic equation?

# Include <stdio. h>
# Include <math. h>
Void main ()
{
Double sqrt (double x );
Int a, B, c;
Double x1, x2, x, e, d, g, f;
Scanf ("% d", & a, & B, & c );
D = B * B-4 * a * c;
If (a = 0)
{If (B! = 0)
{X =-(double) c/(double) B;
If (x = 0)
{X =-x;
Printf ("x = %. 6lf \ n", x );}
Else
Printf ("x = %. 6lf \ n", x );
}
Else
Printf ("Input error! \ N ");}
Else if (d <0)
{D =-d;
E = sqrt (d );
G =-(double) B/(2 * (double) );
F = e/(2 * );
If (g! = 0)
Printf ("x1 = %. 6lf + %. 6lfi \ nx2 = %. 6lf-%. 6lfi \ n", g, f, g, f );
Else
Printf ("x1 = %. 6lfi \ nx2 =-%. 6lfi \ n", f, f );}
Else if (d = 0)
{X1 = x2 =-B/(2 * );
Printf ("x1 = x2 = %. 6lf \ n", x1 );}
Else
{E = sqrt (d );
X1 = (-B + e)/(2 * );
X2 = (-B-e)/(2 * );
Printf ("x1 = %. 6lf \ nx2 = %. 6lf \ n", x1, x2 );}
}

This is fine, but it may not be easy enough.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.