以矩阵形式表示线性方程的Java程序
让我们借助一个例子来看看线性方程组:
系数和变量的输入被考虑在内。
- 应该将扫描仪包导入到程序中,以便使用 Scanner 类的对象来获取用户的输入。
- 数组将被初始化以存储方程的变量。
- 将在 Scanner 类对象的帮助下从用户那里获取变量的系数。
- 然后在循环的帮助下,方程将转换为矩阵的形式。
下岗两个例子:
- 矩阵形式的 3 个变量线性方程。
- N个矩阵形式的变量线性方程。
说明:考虑到数学中最常用的实用线性方程,即 3 个变量线性方程。
Input: ax + by + cz = d
Output - 1 2 3 x = 10
5 1 3 y = 12
7 4 2 z = 20
示例 1:用于矩阵形式的 3 个变量线性方程的Java程序。
Java
// Java Program to Represent Linear Equations in Matrix Form
// Importing Scanner class
// to take input from user
import java.util.Scanner;
public class GFG {
// Mai driver method
public static void main(String args[])
{
// Display message for better readability
System.out.println(
"******** 3 variable linear equation ********");
// 3 variables of the linear equation
char[] variable = { 'x', 'y', 'z' };
// Creating Scanner class object
Scanner sc = new Scanner(System.in);
// Display message for asking user to enter input
System.out.println(
"Enter the coefficients of 3 variable");
System.out.println(
"Enter in the format shown below");
System.out.println("ax + by + cz = d");
// For 3*3 matrix or in other words
// Dealing with linear equations of 3 coefficients
// Input of coefficients from user
int[][] matrix = new int[3][3];
int[][] constt = new int[3][1];
// Outer loop for iterating rows
for (int i = 0; i < 3; i++) {
// Inner loop for iterating columns
for (int j = 0; j < 3; j++) {
// Reading values from usr and
// entering in the matrix form
matrix[i][j] = sc.nextInt();
}
// One row input is over by now
constt[i][0] = sc.nextInt();
}
// The linear equations in the form of matrix
// Display message
System.out.println(
"Matrix representation of above linear equations is: ");
// Outer loop for iterating rows
for (int i = 0; i < 3; i++) {
// Inner loop for iterating columns
for (int j = 0; j < 3; j++) {
// Printing matrix corresponding
// linear equation
System.out.print(" " + matrix[i][j]);
}
System.out.print(" " + variable[i]);
System.out.print(" = " + constt[i][0]);
System.out.println();
}
// Close the stream and release the resources
sc.close();
}
}
Java
import java.util.Scanner;
public class Linear_Equations_n {
public static void main(String args[])
{
System.out.println(
"******** n variable linear equation ********");
// Initializing the variables
char[] variable
= { 'a', 'b', 'c', 'x', 'y', 'z', 'w' };
System.out.println("Enter the number of variables");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(
"Enter the coefficients variable");
System.out.println(
"Enter in the format shown below");
System.out.println("ax + by + cz + ... = d");
// Input of coefficients from user
int[][] matrix = new int[num][num];
int[][] constt = new int[num][1];
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
matrix[i][j] = sc.nextInt();
}
constt[i][0] = sc.nextInt();
}
// Representation of linear equations in form of
// matrix
System.out.println(
"Matrix representation of above linear equations is: ");
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
System.out.print(" " + matrix[i][j]);
}
System.out.print(" " + variable[i]);
System.out.print(" = " + constt[i][0]);
System.out.println();
}
sc.close();
}
}
输出:
现在,让它对任何 N 值通用:“n 变量线性方程”
插图:
Input: ax + by + cz + ... = d
Output: 1 2 3 x = 10
5 1 3 y = 12
7 4 2 z = 20
...
...
示例 2:用于矩阵形式的 N 个变量线性方程的Java程序。
Java
import java.util.Scanner;
public class Linear_Equations_n {
public static void main(String args[])
{
System.out.println(
"******** n variable linear equation ********");
// Initializing the variables
char[] variable
= { 'a', 'b', 'c', 'x', 'y', 'z', 'w' };
System.out.println("Enter the number of variables");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println(
"Enter the coefficients variable");
System.out.println(
"Enter in the format shown below");
System.out.println("ax + by + cz + ... = d");
// Input of coefficients from user
int[][] matrix = new int[num][num];
int[][] constt = new int[num][1];
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
matrix[i][j] = sc.nextInt();
}
constt[i][0] = sc.nextInt();
}
// Representation of linear equations in form of
// matrix
System.out.println(
"Matrix representation of above linear equations is: ");
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
System.out.print(" " + matrix[i][j]);
}
System.out.print(" " + variable[i]);
System.out.print(" = " + constt[i][0]);
System.out.println();
}
sc.close();
}
}
输出 -