Course Code : BCSL-058
Course Title : Computer Oriented Numerical Techniques Lab
Assignment Number : BCA(V)/L-058/Assignment/2025-26
Maximum Marks : 50
Weightage : 25%
Last Date of Submission : 31st October,2025(For July 2025 Session)
30th April, 2026 (For January 2026 Session)
This assignment has eight problems of 40 marks, each of 5 marks. All problems are
compulsory. 10 marks are for viva voce. Please go through the guidelines regarding
assignments given in the programme guide for the format of presentation.
Q1. Write a program in C that accepts a decimal number and displays its floating-point
equivalent number. You may make assumptions to simplify the program, however, your
representation of floating point number should be closer to IEEE 754 standard 32 bit
representation. (5 Marks)
Solution: Here is a C program that accepts a decimal (floating-point) number from the user and
displays its floating-point equivalent in a 32-bit IEEE 754-like representation.
This program shows the sign bit, the exponent bits, and the mantissa bits separately, which
together form the 32-bit IEEE 754 single-precision floating-point format:
#include <stdio.h>
#include <stdint.h>
typedef union {
float f;
uint32_t u;
} FloatUnion;
// Function to print the binary representation of a 32-bit number
void printBinary(uint32_t n, int bits) {
for (int i = bits - 1; i >= 0; i--) {
printf("%u", (n >> i) & 1);
}
}
int main() {
FloatUnion num;
printf("Enter a decimal number: ");
scanf("%f", &num.f);
// IEEE 754 32-bit floating-point has:
// 1 bit sign, 8 bits exponent, 23 bits mantissa
uint32_t sign = (num.u >> 31) & 1;
uint32_t exponent = (num.u >> 23) & 0xff;
uint32_t mantissa = num.u & 0x7fffff;
printf("Floating-point representation (IEEE 754 32-bit):\n");
printf("Sign bit: %u\n", sign);
printf("Exponent bits: ");
printBinary(exponent, 8);
printf("\nMantissa bits: ");
printBinary(mantissa, 23);
printf("\n");
return 0;
}
Explanation:
• This program reads a float number from the user.
• It uses a union to access the same 32 bits as both a float and an unsigned 32-bit integer.
• It extracts the sign bit (1 bit), exponent (8 bits), and mantissa (23 bits) from the 32-bit
binary representation of the float.
• It outputs each part in binary form to represent the number close to the IEEE 754 32-bit
floating point standard.
Q2. Write a program in C to implement Regula-Falsi method. (5 Marks)
Solution: Here is a C program to implement the Regula-Falsi (False Position) method for finding
a root of a continuous function within a given interval. This solution is appropriate for a 7-mark
question and includes essential explanations.
#include
#include
// Example function: change this function as needed
double func(double x) {
return x*x*x - x - 2; // f(x) = x^3 - x - 2
}
int main() {
double a, b, c; // Interval endpoints and root approximation
double fa, fb, fc;
double tol;
int max_iter, iter = 0;
printf("Enter interval endpoints a and b (such that f(a)*f(b) = 0) {
printf("Invalid interval. The function must have opposite signs at
a and b.\n");
return 1;
}
printf("Enter tolerance for root approximation: ");
scanf("%lf", &tol);
printf("Enter maximum number of iterations: ");
scanf("%d", &max_iter);
printf("Iter\t a\t\t b\t\t c\t\t f(c)\n");
do {
// Compute c using Regula-Falsi formula
c = (a * fb - b * fa) / (fb - fa);
fc = func(c);
printf("%d\t%lf\t%lf\t%lf\t%lf\n", iter+1, a, b, c, fc);
if (fabs(fc) < tol) {
printf("Root found: %lf\n", c);
break;
}
if (fa * fc < 0) {
b = c;
fb = fc;
} else {
a = c;
fa = fc;
}
iter++;
} while (iter < max_iter);
if (iter == max_iter) {
printf("Maximum iterations reached. Approximate root: %lf\n", c);
}
return 0;
}
Explanation:
• The program asks the user for the interval [a, b] where the function changes sign
(checked by f(a)*f(b) < 0), a tolerance level, and maximum iteration count.
• It then iteratively applies the Regula-Falsi formula to find the root.
• The loop continues until the function value at the current approximation is within
tolerance or the max iteration is reached.
• It prints intermediate values for transparency of the process.
Q3. Write a program to implement Gauss Elimination method for solving linear equations.
Your method should check if a given pivot is zero or not. It should also minimise the
floating-point errors. (5 Marks)
Solution: Here is a C program implementing the Gauss Elimination method for solving a system
of linear equations. The program checks if a pivot element is zero and performs partial pivoting
to minimize floating-point errors by swapping rows to use the largest available pivot element.
This approach improves numerical stability.
#include <stdio.h>
#include <math.h>
#define MAX 10
// Function to perform partial pivoting
void partialPivot(double matrix[MAX][MAX+1], int n, int pivotIndex) {
int maxRow = pivotIndex;
for (int i = pivotIndex + 1; i < n; i++) {
if (fabs(matrix[i][pivotIndex]) >
fabs(matrix[maxRow][pivotIndex])) {
maxRow = i;
}
}
// Swap rows if maxRow is different
if (maxRow != pivotIndex) {
for (int j = 0; j <= n; j++) {
double temp = matrix[pivotIndex][j];
matrix[pivotIndex][j] = matrix[maxRow][j];
matrix[maxRow][j] = temp;
}
}
}
// Gauss Elimination to solve the system
int gaussElimination(double matrix[MAX][MAX+1], double solution[MAX],
int n) {
for (int i = 0; i < n; i++) {
// Check and perform partial pivoting
partialPivot(matrix, n, i);
// Check for zero pivot element
if (fabs(matrix[i][i]) < 1e-12) {
printf("Zero pivot encountered at row %d. System may be
singular or ill-conditioned.\n", i);
return 0; // Failure
}
// Eliminate variables below pivot
for (int k = i + 1; k < n; k++) {
double factor = matrix[k][i] / matrix[i][i];
for (int j = i; j <= n; j++) {
matrix[k][j] -= factor * matrix[i][j];
}
}
}
// Back substitution
for (int i = n - 1; i >= 0; i--) {
double sum = matrix[i][n];
for (int j = i + 1; j < n; j++) {
sum -= matrix[i][j] * solution[j];
}
solution[i] = sum / matrix[i][i];
}
return 1; // Success
}
int main() {
int n;
double matrix[MAX][MAX + 1], solution[MAX];
printf("Enter the number of variables (max %d): ", MAX);
scanf("%d", &n);
if (n > MAX || n <= 0) {
printf("Invalid number of variables.\n");
return 1;
}
printf("Enter the augmented matrix coefficients (each row %d
coefficients and constant term):\n", n + 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
scanf("%lf", &matrix[i][j]);
}
}
if (!gaussElimination(matrix, solution, n)) {
printf("Failed to find a unique solution.\n");
return 1;
}
printf("Solution:\n");
for (int i = 0; i < n; i++) {
printf("x[%d] = %.6lf\n", i + 1, solution[i]);
}
return 0;
Explanation:
• This program accepts an augmented matrix for a system of linear equations.
• It uses partial pivoting before eliminating variables to handle zero or small pivots and
reduce floating-point errors.
• The partialPivot function searches for the largest pivot element in the current column
and swaps rows if needed.
• After the forward elimination process, back substitution is performed to compute the
solution vector.
• If the pivot element is zero (or near zero), the program reports that the system may be
singular or ill-conditioned and stops.
Q4. Write a program in C for the demonstration of Stirling's Formula for Interpolation. (5
Marks)
Solution: Here is a C program demonstrating Stirling's Formula for interpolation. The program
accepts equidistant data points and their function values, then estimates the value of the function
at an intermediate point using Stirling's interpolation method.
#include <stdio.h>
#define MAX 20
// Function to calculate factorial of a number
int factorial(int n) {
int fact = 1;
for (int i = 2; i <= n; i++)
fact *= i;
return fact;
}
// Function to build forward difference table
void forwardDifferenceTable(double y[MAX][MAX], int n) {
for (int j = 1; j < n; j++) {
for (int i = 0; i < n - j; i++) {
y[i][j] = y[i + 1][j - 1] - y[i][j - 1];
}
}
}
// Stirling's interpolation function
double stirlingInterpolation(double x[MAX], double y[MAX][MAX], int n,
double x_val, double h) {
int mid = n / 2; // middle index
double p = (x_val - x[mid]) / h;
double res = y[mid][0]; // initial value y0
double p_mul = 1;
int sign = 1;
for (int i = 1; i < n; i++) {
if (i % 2 != 0) {
int index = mid - (i / 2);
double diff_avg = (y[index][i] + y[index - 1][i]) / 2.0;
p_mul = p_mul * p * p - (i / 2)*(i / 2);
res += (p_mul * diff_avg) / factorial(i);
} else {
int index = mid - (i / 2);
p_mul = p_mul * (p + (i/2) - 1) * (p - (i/2));
res += (p_mul * y[index][i]) / factorial(i);
}
}
return res;
}
int main() {
int n;
double x[MAX], y[MAX][MAX];
double x_val, h;
printf("Enter the number of data points (odd number recommended):
");
scanf("%d", &n);
printf("Enter equidistant x values:\n");
for (int i = 0; i < n; i++) {
scanf("%lf", &x[i]);
}
printf("Enter corresponding y values:\n");
for (int i = 0; i < n; i++) {
scanf("%lf", &y[i][0]);
}
// Calculate the forward difference table
forwardDifferenceTable(y, n);
// Assuming uniform spacing, calculate h
h = x[1] - x[0];
printf("Enter the value of x for interpolation: ");
scanf("%lf", &x_val);
// Perform interpolation
double result = stirlingInterpolation(x, y, n, x_val, h);
printf("Interpolated value at x = %.4lf is: %.6lf\n", x_val,
result);
return 0;
}
Explanation:
• The program takes an odd number of equidistant data points (x and y).
• It builds a forward difference table to compute the differences required by Stirling's
formula.
• The interpolation point x_val is taken from the user.
• The Stirling formula implementation averages the forward and backward differences near
the midpoint of data for better accuracy.
• Factorials are used for the denominators in the formula terms.
• The program then outputs the interpolated function value at x_val.
This code demonstrates how Stirling's interpolation formula estimates intermediate values with
the benefit of averaging differences close to the center, making it suitable for points near the
middle of the dataset.
Q5. Write a C Program for solving the following system of equations using Jacobi method:
5 −1 −1 −1 𝑥! −4
−1 10 −1 −1 𝑥" 12
! & !𝑥 & = ! &
−1 −1 5 −1 # 8
−1 −1 −1 10 𝑥$ 34
Starting with 𝒙(𝟎) = (𝟎, 𝟎, 𝟎, 𝟎). 𝐓𝐡𝐞 𝐞𝐱𝐚𝐜𝐭 𝐬𝐨𝐥𝐮𝐭𝐢𝐨𝐧 𝐢𝐬 𝒙 = (𝟏, 𝟐, 𝟑, 𝟒) 𝑻𝒏 . (5 Marks)
Solution: The Jacobi method is an iterative algorithm used to solve a system of linear equations
of the form 𝐴𝐱 = 𝐛. Each variable is updated based on the previous iteration values, treating all
new estimates independently.
Given System
5 −1 −1 −1 𝑥! −4
−1 10 −1 −1 𝑥" 12
! & !𝑥 & = ! &
−1 −1 5 −1 # 8
−1 −1 −1 10 𝑥$ 34
Initial guess: 𝑥 ()) = (0,0,0,0)
Exact solution: 𝑥 = (1,2,3,4)*
C Program Using Jacobi Method
#include
#include
#define N 4 // Size of the system
#define MAX_ITER 100 // Maximum number of iterations
#define EPSILON 1e-6 // Tolerance for convergence
int main() {
// Coefficient matrix A
double A[N][N] = {
{5, -1, -1, -1},
{-1, 10, -1, -1},
{-1, -1, 5, -1},
{-1, -1, -1, 10}
};
// Right-hand side vector b
double b[N] = {-4, 12, 8, 34};
// Initial guess x^(0)
double x_old[N] = {0, 0, 0, 0};
double x_new[N] = {0, 0, 0, 0};
int iter, i, j;
double diff, sum;
printf("Iterative results:\n");
for(iter = 1; iter MAX_ITER) {
printf("Did not converge within the maximum number of
iterations.\n");
}
// Print the final solution
printf("\nFinal solution:\n");
for(i = 0; i < N; i++) {
printf("x%d = %.6f\n", i+1, x_new[i]);
}
return 0;
}
How It Works
• The matrix 𝐴 and vector 𝑏 are declared and initialized.
• Two arrays 𝑥old and 𝑥new store the previous and current estimates of the solution.
• The program iterates up to MAX_ITER times, updating each 𝑥+ using the Jacobi formula:
(,-!) 1 (,)
𝑥+ = I𝑏 − J 𝐴+. 𝑥. K
𝐴++ +
./+
• After each iteration, it checks whether the change in values is below the EPSILON
threshold (convergence criterion).
• Upon convergence, or after the maximum allowed iterations, it prints the final solution.
Sample Output
Iterative results:
Iteration 1: -0.800000 1.200000 1.600000 3.400000
...
Final solution:
x1 = 1.000000
x2 = 2.000000
x3 = 3.000000
x4 = 4.000000
Q6. Write a C program to solve the IVP y’ = -ty², y(2) = 1 and find y(2.1) and y(2.2) with h
= 0.1 using improved tangent method (modified Euler method) of O(h²). (5 Marks)
Solution:
The initial value problem (IVP) is:
𝑑𝑦
= −𝑡𝑦 " , 𝑦(2) = 1
𝑑𝑡
We are to find approximate values of 𝑦(2.1) and 𝑦(2.2) with the step size ℎ = 0.1 using the
Improved Tangent (Modified Euler) Method.
Modified Euler Method
At each step from (𝑡0 , 𝑦0 ) to (𝑡0-! , 𝑦0-! ):
1. Predict using Euler’s method:
(1)
𝑦0-! = 𝑦0 + ℎ ⋅ 𝑓(𝑡0 , 𝑦0 )
2. Correct using the average slope:
ℎ (1)
𝑦0-! = 𝑦0 + [𝑓(𝑡0 , 𝑦0 ) + 𝑓(𝑡0-! , 𝑦0-! )]
2
Where 𝑓(𝑡, 𝑦) = −𝑡𝑦 " .
This method increases accuracy compared to simple Euler by using the slope at both ends of the
interval.
Stepwise Manual Calculations
Step 0: Known values
• 𝑡) = 2.0
• 𝑦) = 1.0000
Step 1: From 𝑡) = 2.0 to 𝑡! = 2.1
• Predictor (Euler):
𝑓(𝑡) , 𝑦) ) = −2.0 × 1.0000" = −2.0000
(1)
𝑦! = 1.0000 + 0.1 × (−2.0000) = 0.8000
• Corrector:
(1)
𝑓(𝑡! , 𝑦! ) = −2.1 × (0.8000)" = −2.1 × 0.6400 = −1.3440
𝑦! = 1.0000 + 0.05 × [−2.0000 + (−1.3440)] = 1.0000 + 0.05 × (−3.3440)
0.05 × (−3.3440) = −0.1672
𝑦! = 1.0000 − 0.1672 = 0.8328
Step 2: From 𝑡! = 2.1 to 𝑡" = 2.2
• Predictor:
𝑓(𝑡! , 𝑦! ) = −2.1 × (0.8328)" = −2.1 × 0.69357 ≈ −1.4565
(1)
𝑦" = 0.8328 + 0.1 × (−1.4565) = 0.8328 − 0.1457 = 0.6871
• Corrector:
(1)
𝑓(𝑡" , 𝑦" ) = −2.2 × (0.6871)" ≈ −2.2 × 0.47211 ≈ −1.0386
𝑦" = 0.8328 + 0.05 × [−1.4565 + (−1.0386)] = 0.8328 + 0.05 × (−2.4951)
0.05 × (−2.4951) = −0.1248
𝑦" = 0.8328 − 0.1248 = 0.7080
Table of Computed Values
(𝒑) (𝒑)
Step 𝒕𝒏 𝒚𝒏 Predictor 𝒚𝒏-𝟏 𝒇(𝒕𝒏 , 𝒚𝒏 ) 𝒇(𝒕𝒏-𝟏 , 𝒚𝒏-𝟏 ) Corrected 𝒚𝒏-𝟏
0 2.0 1.0000 0.8000 -2.0000 -1.3440 0.8328
1 2.1 0.8328 0.6871 -1.4565 -1.0386 0.7080
C Program with Comments
#include
// Function defining dy/dt = -t*y^2
double f(double t, double y) {
return -t * y * y;
}
int main() {
double t = 2.0, y = 1.0, h = 0.1;
int steps = 2; // We need y at t = 2.1 and t = 2.2
printf("Step\t t\t y\t\t Predictor\t f(t_n, y_n)\t f(t_{n+1},
y_{n+1}^{(p)})\t Corrected y\n");
for(int i = 0; i 0 ? y : 1.0000));
printf("y(2.2) = %.4f\n", y);
return 0;
}
Discussion and Accuracy
• The Modified Euler method uses both the beginning and the end slopes at each interval,
improving accuracy to O(h²).
• The values calculated (𝑦(2.1) ≈ 0.8328 and 𝑦(2.2) ≈ 0.7080) are expected to closely
match true values for small ℎ.
• For higher accuracy, decrease ℎ; the method remains stable for small enough ℎ.
• This method is widely used for problems where derivative changes rapidly.
Final Answer and Results
Using the Modified Euler Method with ℎ = 0.1:
• 𝑦(2.1) ≈ 0.8328
• 𝑦(2.2) ≈ 0.7080
Q7. Write a program in C to find the approximate value of the following definite integral
using Trapezoidal rule and obtain a bound for the error. The exact value of I = ln2 =
0.693147 correct to six decimal places:
∫₀¹ dx / (1+x)
(5 Marks)
Solution:
The Trapezoidal Rule estimates definite integrals as:
04!
ℎ
𝐼 ≈ _𝑓(𝑥) ) + 2 J 𝑓 (𝑥+ ) + 𝑓(𝑥0 )`
2
+5!
647
where ℎ = 0
and 𝑥+ = 𝑎 + 𝑖ℎ.
Error Bound:
(𝑏 − 𝑎)#
|𝐸| ≤ ⋅ max|𝑓 8 (𝑥)|
12𝑛"
!
For 𝑓(𝑥) = !-9:
"
𝑓 8 (𝑥) = (!-9)! , so maximum at 𝑥 = 0 is 2.
Hand Calculation (For 𝑛 = 4)
• ℎ = 0.25, points: 0, 0.25, 0.5, 0.75, 1
• 𝑓(0) = 1.0, 𝑓(0.25) = 0.8, 𝑓(0.5) = 0.6667, 𝑓(0.75) = 0.5714, 𝑓(1) = 0.5
• Apply formula:
𝐼 ≈ 0.125[1.0 + 2(0.8 + 0.6667 + 0.5714) + 0.5] = 0.125 × 5.5762 = 0.6970
• Exact: ln2 = 0.693147
• Actual Error: 0.6970 − 0.693147 = 0.0039
!
• Error bound: 𝐸 ≤ !:" × 2 ≈ 0.0104
C Program
#include
#include
double f(double x) { return 1.0 / (1+x); }
int main() {
int n; double a=0,b=1;
printf("Enter n: ");
scanf("%d", &n);
double h=(b-a)/n, sum=f(a)+f(b);
for(int i=1;i<n;i++) sum += 2*f(a+i*h);
double approx = h/2*sum, exact = log(2.0);
double error_bound = ((b-a)*(b-a)*(b-a))/(12*n*n)*2.0;
printf("Approximate: %.6f\nExact: %.6f\nError Bound: %.6f\n",
approx, exact, error_bound);
return 0;
}
Summary Table
𝑥+ 𝑓(𝑥+ )
0 1.0000
0.25 0.8000
0.5 0.6667
0.75 0.5714
1 0.5000
Final Answers:
• Trapezoidal Approximation: ~0.6970 (for n = 4)
• Exact Value: 0.693147
• Error Bound: ~0.0104
Q8. Write a program in C to solve the equation x² y’ = 1 - xy - x² y², y(1) = -1 from x = 1 to x
= 2 by using Taylor series method of O(h²) with h = 1/3 and ¼ and find the actual error at
x=2 if the exact solution is y = -1/x. (5 Marks)
Solution: The Taylor Series Method (2nd order, O(h²)) for the given ODE 𝑥 " 𝑦 ; = 1 − 𝑥𝑦 −
𝑥 " 𝑦 " with initial condition 𝑦(1) = −1 proceeds as follows:
Rewrite the ODE as
1 − 𝑥𝑦 − 𝑥 " 𝑦 "
𝑦 ; = 𝑓(𝑥, 𝑦) =
𝑥"
The 2nd order Taylor method update formula is
ℎ"
𝑦0-! = 𝑦0 + ℎ𝑓0 + 𝑓0"
2
where
∂𝑓 ∂𝑓 ;
𝑓0 = 𝑓(𝑥0 , 𝑦0 ), 𝑓0" = + 𝑦
∂𝑥 ∂𝑦
1. Calculating derivatives
∂𝑓 −𝑦 − 2𝑥𝑦 " 1 − 𝑥𝑦 − 𝑥 " 𝑦 "
= − 2
∂𝑥 𝑥" 𝑥#
∂𝑓 −𝑥 − 2𝑥 " 𝑦 𝑥 + 2𝑥 " 𝑦
= =−
∂𝑦 𝑥" 𝑥"
At a point (𝑥, 𝑦),
∂𝑓 ∂𝑓
𝑓0" = + 𝑓(𝑥, 𝑦)
∂𝑥 ∂𝑦
2. Example step at 𝒙𝟎 = 𝟏, 𝒚𝟎 = −𝟏
𝑓(1, −1) = 1
𝑓0" = (−𝑦 − 2𝑥𝑦 " )/𝑥 " − 2(1 − 𝑥𝑦 − 𝑥 " 𝑦 " )/𝑥 # = (1 − 2) − 2(1) = −3
!
For step size ℎ = #,
1 (1/3)"
𝑦! = −1 + × 1 + × (−3) = −1 + 0.3333 − 0.1667 = −0.8334
3 2
The next steps are calculated similarly until 𝑥 = 2.
3. C code snippet for iterative calculation
#include
#include
double f(double x, double y) {
return (1 - x*y - x*x*y*y)/(x*x);
}
double dfdx(double x, double y) {
return (-y - 2*x*y*y)/(x*x) - 2*(1 - x*y - x*x*y*y)/(x*x*x);
}
int main() {
double hs[2] = {1.0/3.0, 0.25};
for(int j = 0; j < 2; j++) {
double h = hs[j];
double x = 1, y = -1;
while(x < 2 - 1e-9) {
double y_new = y + h * f(x,y) + h*h/2 * dfdx(x,y);
x += h;
y = y_new;
}
printf("h=%.3f, y(2)=%.5f, error=%.5f\n", h, y, fabs(y + 0.5));
}
return 0;
}
4. Results & error
!
The exact solution is 𝑦 = − 9, hence 𝑦(2) = −0.5.
The numerical solution approximates 𝑦(2) for both ℎ = 1/3 and ℎ = 1/4, with the error
measured by
|𝑦numerical (2) + 0.5|
Using smaller ℎ results in less error, confirming the order 𝑂(ℎ" ) accuracy of the Taylor method.