C 语言查找二次方程根的程序

如何编写一个 C 语言程序来查找二次方程的根并附带示例?二次方程可以有两个根,它们完全取决于判别式。二次方程的数学表示为 ax²+bx+c = 0。如果判别式 > 0,则该方程有两个不同的实根。

Two Distinct Real Roots 3

如果判别式 = 0,则有两个相等的实根,公式如下所示。

Two Equal and Real Roots 4

如果判别式 < 0,则有两个不同的复根。

Two Distinct Complex Roots 5

使用 Else If 查找二次方程根的 C 语言程序

此程序允许用户输入 a、b 和 c 的三个值。接下来,此程序使用 Else If 语句sqrt 函数 查找二次方程的根。

#include <stdio.h>
#include<math.h>
int main()
{
	float a, b, c;
	float root1, root2, imaginary, discriminant;
	
	printf("\n Please Enter values of a, b, c :  ");
  	scanf("%f%f%f", &a, &b, &c);
  	
  	discriminant = (b * b) - (4 * a *c);
  	
  	if(discriminant > 0)
  	{
  		root1 = (-b + sqrt(discriminant) / (2 * a));
  		root2 = (-b - sqrt(discriminant) / (2 * a));
		printf("\n Two Distinct Real Roots Exists: root1 = %.2f and root2 = %.2f", root1, root2);
  	}
  	else if(discriminant == 0)
  	{
  		root1 = root2 = -b / (2 * a);
  		printf("\n Two Equal and Real Roots Exists: root1 = %.2f and root2 = %.2f", root1, root2);
  	}
  	else if(discriminant < 0)
  	{
  		root1 = root2 = -b / (2 * a);
  		imaginary = sqrt(-discriminant) / (2 * a);
  		printf("\n Two Distinct Complex Roots Exists: root1 = %.2f+%.2f and root2 = %.2f-%.2f", root1, imaginary, root2, imaginary);
  	}
	
  	return 0;
}
C Program to find Roots of a Quadratic Equation 1

在此查找二次方程根的 C 语言程序示例中,用户输入的数值为 10、15 和 -25。这意味着 a = 10,b = 15,c = -25,二次方程为 10x²+15x-25 = 0

判别式 = (b * b) – (4 * a *c) = (15 * 15) – (4 * 10 *(-25))
判别式 = (225) – (- 1000) = 225 + 1000 = 1225

这意味着判别式 > 0,因此
根1 = (-b + sqrt(判别式) / (2 * a)) = (-15 + sqrt(1225) / (2 * 10))
根1 = (-15 + 35 / (20)) = -15 + 1.75 = -13.25

根2 = (-b – sqrt(判别式) / (2 * a)) = (-15 – sqrt(1225) / (2 * 10))
根2 = (-15 – 35 / (20)) = -15 – 1.75 = -16.75

使用 Switch Case 计算二次方程根的 C 语言程序

这个查找二次方程根的程序与上面相同,但这次我们在编程中使用Switch case

#include <stdio.h>
#include<math.h>
int main()
{
	float a, b, c;
	float root1, root2, imaginary, discriminant;
	
	printf("\n Please Enter values of a, b, c :  ");
  	scanf("%f%f%f", &a, &b, &c);
  	
  	discriminant = (b * b) - (4 * a *c);
  	
  	switch(discriminant > 0)
  	{
  		case 1:
  			root1 = (-b + sqrt(discriminant) / (2 * a));
  			root2 = (-b - sqrt(discriminant) / (2 * a));
			printf("\n Two Distinct Real Roots Exists: root1 = %.2f and root2 = %.2f", root1, root2);
			break;
		case 0:
			switch(discriminant < 0)
			{
				case 1: //True
					root1 = root2 = -b / (2 * a);
					imaginary = sqrt(-discriminant) / (2 * a);
					printf("\n Two Distinct Complex Roots Exists: root1 = %.2f+%.2f and root2 = %.2f-%.2f", root1, imaginary, root2, imaginary);
					break;
				case 0: // False (Discriminant = 0)
					root1 = root2 = -b / (2 * a);
					printf("\n Two Equal and Real Roots Exists: root1 = %.2f and root2 = %.2f", root1, root2);
					break;
			}
  	}
	
  	return 0;
}
Program to find Roots of a Quadratic Equation using Switch Case

在此查找根的程序示例中,用户输入的数值为 2 3 5。这意味着 a = 2,b = 3,c = 5,二次方程为 2x²+3x+5 = 0

判别式 = (b * b) – (4 * a *c) = (3 * 3) – (4 * 2 * 5)
判别式 = (9) – (40) = -31

这意味着判别式 < 0,因此
根1 = 根2 = -b / (2 * a)
根1 = 根2 = -3 / (2 * 2) = -0.75

虚部= sqrt(-判别式) / (2 * a)
虚部= sqrt(- -31) / (2 * 2) =  5.567 / 4 = 1.3919

根1= 根1+虚部
根1= -0.75+1.3919

根2= 根2-虚部
根2= -0.75-1.3919

评论已关闭。