C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言查找二次方程根

二次方程式是阶为2的多项式方程。表示为 ax 2 + bx + c = 0 ,其中a,b和c是方程式的系数变量。二次方程的通用规则定义'a'的值不能为零,并且x的值用于找到二次方程(a,b)的根。二次方程式的根由三种方式定义: 实数和唯一,实数和相等以及实数和虚数。
C程序来找到二次方程的根

根的性质

根的性质取决于判别式(D),其中D是
如果D> 0,则根是真实且不同的(不相等) 如果D = 0,则根是实数且相等。 如果D < 0,则根是实数和虚数。

找到二次方程的平方根的步骤

初始化二次方程式中使用的所有变量。 从用户那里获取所有系数变量x,y和z的输入。 然后,使用以下公式找到二次方程的判别式:
判别式=(y * y)-(4 * x * z)。
根据二次方程判别式的性质来计算根。 如果判别式> 0,则
Root1 =(-y + sqrt(det))/(2 * x)
Root2 =(-y + sqrt(det))/(2 * x)
打印出的根是真实且独特的。
如果(discriminant = 0),则
Root1 = Root2 =-y/(2 * x)。
打印两个根是真实的且相等。
否则(判别<0),根是不同的复数,其中
根的实部是: Root1 = Root2 =-y/(2 * x)或real =-y/(2 * x) 。
根的虚部是: sqrt(-discriminant)/(2 * x)。
打印两个根都是虚数的,其中第一个根是(r + i)img,第二个根是( r-i)img。
退出或终止程序。

二次方程式的伪代码

开始 输入系数变量x,y和z。 D <-sqrt(y * y-4 * x * z)。 R1 <-(-y + D)/(2 * x)。 R2 <-(-y-D)/(2 * x)。 打印根R1和R2、 停止
让我们在C程序中实现上述步骤,以找到二次方程的根。
/* C Program to Find the Roots of the Quadratic Equation */
#include<stdio.h>
#include<math.h>  // it is used for math calculation
#include<conio.h>
void main()
{
    float x, y, z, det, root1, root2, real, img;
    printf("\n Enter the value of coefficient x, y and z: \n ");
    scanf("%f %f %f", &x, &y, &z);
    // define the quadratic formula of the nature of the root
    det = y * y-4 * x * z;  
    // defines the conditions for real and different roots of the quadratic equation
    if (det > 0)
    {
    root1 = (-y + sqrt(det)) / (2 * x);
    root2 = (-y + sqrt(det)) / (2 * x);
    printf("\n Value of root1 = %.2f and value of root2 = %.2f", root1, root2);
    }
    // elseif condition defines both roots ( real and equal root) are equal in the quadratic equation
    else if (det == 0)
    {
        root1 = root2 =-y / (2 * x); // both roots are equal;
        printf("\n Value of root1 = %.2f and Value of root2 = %.2f", root1, root2);
    }
    // if det < 0, means both roots are real and imaginary in the quadratic equation.
    else {
        real =-y / (2 * x);
        img = sqrt(-det) / (2 * x);
        printf("\n value of root1 = %.2f + %.2fi and value of root2 = %.2f-%.2fi ", real, img, real, img);
    }
    getch();
    }
输出:
C程序来找到二次方程的根
让我们创建另一个我们使用函数的C程序。
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
// use function to check the nature of the roots in the quadratic equation
void R_Quadratic( int x, int y, int z)
{
    if (x == 0) // Checks the vakue of x, if x = 0, the equation is not quadratic equation.
    {
        printf(" The value of x cannot be zero");
        return;
    }
    int det = y * y-4 * x * z;
    double sqrt_det = sqrt(abs (det));
    if (det > 0)
    {
    printf("\n Both roots are real and different \n ");
    printf("%.2f\n %.2f", (double) (-y + sqrt_det) / (2 * x), (double) (-y-sqrt_det) / (2 * x));
        }
else if (det == 0)
{
    printf("\n Both roots are real and same ");
    printf("%.2f",-(double)y / (2 * x));
}
else
{
    printf("\n Both roots are complex");
    printf("\n %.2f + %.2fi \n%.2f-%.2fi",-(double)y / (2 * x), sqrt_det,-(double)y / (2 * x), sqrt_det);
    
}
}
void main()
{
    int x, y, z; // declare variables x, y and z
    printf("\n Enter the value of coefficient x, y and z: ");
    scanf("%d %d %d", &x, &y, &z);
    R_Quadratic(x, y, z);  // call function R_Quadratic()
    getch();
}
输出:
C查找二次方程根的程序
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4