使用 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
程式
以下是求解線性方程的 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP