使用 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 predicted
  • Else
    • Compute c=-b/a
  • Return c
Step 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
");    }else{       c = -b / a;    }    return c; } int main(){    float a, b, c;    printf("
enter a,b values: ");    scanf("%f%f", &a, &b);    c = solve(a, b);    printf("
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

更新於: 2021-03-26

4K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.