本文介绍了编写一个程序来计算一个qudratic eqation的实根,根由公式x1 = -b +√b2-4ac/ 2a x2 = -b-√b2-4ac/ 2a给出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序应该请求constanta,b和c的值并打印x1和x2的值使用以下规则

如果a和b都为零则没有解决方案

只有一个根,如果a = 0(x = -c / b)

如果b2-4ac为负则没有真正的根源

否则有是2个真正的根源



我尝试过:



the programm should request for the values of the constanta,b and c and print the values of x1 and x2 use the following rules
no solution if both a and b are zero
there is only one root if a=0 (x=-c/b)
there are no real roots if b2-4ac is negative
otherwise there are 2 real roots

What I have tried:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,xr,xi,dis;
printf("enter the number\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0&&b==0);
{
else if(a==0)
}
x=-c/b;
printf("only one root is exists");
printf("the value of x=%f",x);

推荐答案


#include <stdio.h>
#include <math.h>

int main()
{
  double a, b, c;
  printf("plaease enter the coefficients\n");
  scanf("%lf %lf %lf",&a,&b,&c);
  if( a == 0.0 )
  {
    if ( b == 0.0)
    {
      printf("sorry, no roots\n");
    }
    else
    {
      double x = - c / b;
      printf("only one root is exists: %f\n",  x);
    }
  }
  else
  { // here a != 0.0
    // ...
  }
  return 0;
}


这篇关于编写一个程序来计算一个qudratic eqation的实根,根由公式x1 = -b +√b2-4ac/ 2a x2 = -b-√b2-4ac/ 2a给出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 22:00