Bachelor of Technology
(2023-2027)
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
Numerical Methods Lab
(YCS4102)
Student Name : Bikash Kumar
Roll/Serial No : 123 (23CS2021018)
Section : CSE 4C
Contact No : 7439954482
E-Mail : bikkumar89@[Link]
JISU/[Link]/CSE 4C/Even Sem 2025/Srija das/23CS2011123 Faculty: Professor Dr DIPANKAR MISRA
SL Assignm Date of Date of Signature
No ent No Problem Statement Execution Submission of Faculty
1.1 Write a program
1
2 1 1.2 Write a program
1.3 Write a program
3
2.1 Write a program
4
2.2 Write a program
5
2.3 Write a program
6
2.4 Write a program
7
8
2 2.5 Write a program
2.6 Write a program
9
2.7 Write a program
10
2.8 Write a program
11
3.1 Write a program
12
3.2 Write a program
13
3.3 Write a program
14
3.4 Write a program
15
16
3 3.5 Write a program
3.6 Write a program
17
3.7 Write a program
18
3.8 Write a program
19
20
21
22
23
24
Bachelor of Technology
(2024-2028)
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
Programming for Problem Solving Lab
(Paper Code: YCS-1102)
ASSIGNMENT 01
Problem 1.1 Write a Program…………..
Problem 1.2 Write a Program…………..
Problem 1.3 Write a Program…………..
Problem Definition: 4. Write a Program in c to implement a system of linear
equations using Gauss-elimination method with example.
Code:
#include <stdio.h>
#define SIZE 10
void gaussElimination(float a[SIZE][SIZE], int n) {
int i, j, k;
float factor, x[SIZE];
for (i = 0; i < n - 1; i++) {
if (a[i][i] == 0.0) {
printf("Mathematical Error! Division by zero detected.\n");
return;
}
for (j = i + 1; j < n; j++) {
factor = a[j][i] / a[i][i];
for (k = i; k <= n; k++) {
a[j][k] -= factor * a[i][k];
}
}
}
for (i = n - 1; i >= 0; i--) {
x[i] = a[i][n];
for (j = i + 1; j < n; j++) {
x[i] -= a[i][j] * x[j];
}
x[i] /= a[i][i];
}
printf("\nSolution:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %.2f\n", i + 1, x[i]);
}
}
int main() {
float a[SIZE][SIZE];
int n, i, j;
printf("Enter the number of equations: ");
scanf("%d", &n);
printf("Enter the augmented matrix (coefficients and constants):\n");
for (i = 0; i < n; i++) {
for (j = 0; j <= n; j++) {
scanf("%f", &a[i][j]);
}
}
gaussElimination(a, n);
return 0;
}
Output :
Solution:
X[1] = 2.00
X[2] = 3.00
X[3] = -1.00
Problem Definition: 5.1 Write a program in c to implement Gauss-Seidel
Iteration Method.
Code :
#include <stdio.h>
#include <math.h>
#define MAX_ITER 100
#define TOLERANCE 0.0001
void gaussSeidel(int n, double A[n][n], double B[n], double X[n]) {
double X_old[n];
int iter = 0;
double error, sum;
for (int i = 0; i < n; i++) {
X[i] = 0.0;
X_old[i] = 0.0;
}
printf("\nIteration Results:\n");
do {
error = 0.0;
for (int i = 0; i < n; i++) {
sum = B[i];
for (int j = 0; j < n; j++) {
if (j != i)
sum -= A[i][j] * X[j];
}
X_old[i] = X[i];
X[i] = sum / A[i][i];
error = fmax(error, fabs(X[i] - X_old[i]));
}
printf("Iteration %d: ", iter + 1);
for (int i = 0; i < n; i++) {
printf("X[%d] = %.4f ", i, X[i]);
}
printf("\n");
iter++;
} while (error > TOLERANCE && iter < MAX_ITER);
printf("\nSolution converged in %d iterations:\n", iter);
for (int i = 0; i < n; i++) {
printf("X[%d] = %.4f\n", i, X[i]);
}
}
int main() {
int n;
printf("Enter the number of equations: ");
scanf("%d", &n);
double A[n][n], B[n], X[n];
printf("Enter the coefficients of the matrix (A):\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%lf", &A[i][j]);
}
}
printf("Enter the constant terms (B):\n");
for (int i = 0; i < n; i++) {
scanf("%lf", &B[i]);
}
gaussSeidel(n, A, B, X);
return 0;
}
Output ;
Enter the number of equations: 3
Enter the coefficients of the matrix (A):
412
351
113
Enter the constant terms (B):
4
7
3
Iteration Results:
Iteration 1: X[0] = 1.0000 X[1] = 1.2000 X[2] = 0.6000
Iteration 2: X[0] = 0.6500 X[1] = 1.1700 X[2] = 0.7267
...
Solution converged in 8 iterations:
X[0] = 0.6065
X[1] = 1.0856
X[2] = 0.7690
Problem Definition: 5.1 Write a program in c to implement Gauss-Jordan
Method.
Code :
#include <stdio.h>
#define SIZE 10
void gaussJordan(int n, float a[n][n + 1]) {
int i, j, k;
float ratio;
for (i = 0; i < n; i++) {
ratio = a[i][i];
for (j = 0; j <= n; j++) {
a[i][j] /= ratio;
}
for (k = 0; k < n; k++) {
if (k != i) {
ratio = a[k][i];
for (j = 0; j <= n; j++) {
a[k][j] -= ratio * a[i][j];
}
}
}
}
printf("\nSolution:\n");
for (i = 0; i < n; i++) {
printf("x[%d] = %.4f\n", i, a[i][n]);
}
}
int main() {
int n, i, j;
printf("Enter the number of equations: ");
scanf("%d", &n);
float a[n][n + 1];
printf("Enter the augmented matrix (including constants):\n");
for (i = 0; i < n; i++) {
for (j = 0; j <= n; j++) {
scanf("%f", &a[i][j]);
}
}
gaussJordan(n, a);
return 0;
}
Output :
Enter the number of equations: 3
Enter the augmented matrix:
2 1 -1 8
-3 1 2 -11
-2 1 2 -3
Solution:
x[0] = 2.0000
x[1] = 3.0000
x[2] = -1.0000