One-dimensional quadratic equation and one-dimensional quadratic equation in C Language

Source: Internet
Author: User

One-dimensional quadratic equation and one-dimensional quadratic equation in C Language

When solving a quadratic equation using C language, we first agree that this equation is in the form of AX2 + BX + C = 0. Next we need to consider the parameter problem of this quadratic equation. We can discuss in three cases. When A = 0, B = 0, such A parameter cannot constitute an appropriate equation. When A = 0, B = 0, this equation is A one-dimensional equation. When A is less than 0, A quadratic equation is formed. The logic in these three cases can be easily implemented using the if statement. Next, we need to select the parameter type. Here we choose the float type, because the data stored in the computer is binary, there will be some errors when storing floating point numbers, we need to define an error precision, solve the problem within the error range. Finally, in the third case, We nested the if statement to solve the problem of different solutions when △is greater than or equal to 0 in a one-dimensional quadratic equation. The Code is as follows:

// Author: fl // date: 17.9.17 // function: solving the quadratic equation # define _ CRT_SECURE_NO_WARNINGS 1 # include
 
  
# Include
  
   
# Define disc 0.00000001 // error precision int main () {printf ("Please input the parameter a B C in the form of AX ^ 2 + BX + C = 0; \ n "); printf (" \ n "); float a = 0.0, B = 0.0, c = 0.0, d = 0.0, x1 = 0.0, x2 = 0.0; scanf ("% f", & a, & B, & c); printf ("\ n "); d = B * B-4 * a * c; // used to determine the number of real root numbers x1 = (-B) + sqrt (d)/(2 * ); // used to solve the real number root x2 = (-B)-sqrt (d)/(2 * a); if (a>-disc) & (a <disc) & (B>-disc) & (B <disc) // if the value of a and B is zero, cannot form an appropriate function {pri Ntf ("Enter reasonable parameters! \ N "); printf (" \ n ");} else if (a>-disc) & (a <disc )) & (B <-disc) | (B> disc) // if a is 0 and B is not 0, the parameters a, B, c Constitutes a one-dimensional equation {printf ("X = % f \ n", (-c)/B); printf ("\ n ");} else {if (d> disc) // d is greater than 0, and the equation has two different solutions {printf ("X1 = % f \ n", x1 ); printf ("X2 = % f \ n", x2); printf ("\ n");} else if (d>-disc) & (d <disc )) // d equals 0, and the equation has two identical solutions {printf ("X = X1 = X2 = % d \ n", x1); printf ("\ n ");} else // d is less than 0, and the equation has no solution {printf ("This equation has no real number root! \ N "); printf (" \ n ") ;}} return 0 ;}
  
 

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.