求线性方程解的C程序

我们可以应用软件开发的方法用C语言来求解一个变量的线性方程。

要求

  • 方程应为 ax+b=0 的形式

  • a 和 b 是输入,我们需要找到 x 的值

分析

这里,

  • 的输入是A,B的值。

  • 的输出是x值。

算法

参考下面给出的算法来找到线性方程的解。

Step 1. Start

Step 2. Read a,b values

Step 3. Call function

Jump to step 5

Step 4. Print result

Step 5:i. if(a == 0)Print value of c cannot be predictedElseCompute c=-b/aReturn cStep 6: Stop

程序

以下是寻找线性方程解的 C 程序 -

#include <stdio.h>

#include <string.h>

float solve(float a, float b){

   float c;

   if(a == 0){

      printf("value of c cannot be predicted\n");

   }else{

      c = -b / a;

   }

   return c;

}

int main(){

   float a, b, c;

   printf("\n enter a,b values: ");

   scanf("%f%f", &a, &b);

   c = solve(a, b);

   printf("\n linear eq of one variable in the form of ax+b = 0, if a=%f,b=%f,then x=    %f",a,b,c);

   return 0;

}

输出结果

执行上述程序时,会产生以下结果 -

enter a,b values: 4 8

linear eq of one variable in the form of ax+b = 0, if a=4.000000, b=8.000000,

then x= -2.000000

以上是 求线性方程解的C程序 的全部内容, 来源链接: utcz.com/z/347580.html

回到顶部