BCA-203 Numerical and Statistical Method Lab
1. Write a program to interchange primary and secondary diagonal elements
of a square matrix.
#include <stdio.h>
#include <conio.h>
void main() {
int A[10][10], i, j, m, n, size, temp;
clrscr();
printf("Enter order of matrix\n");
scanf("%d%d", & m, & n);
printf("Enter elements in matrix of size %dx%d: \n", m, n);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & A[i][j]);
}
}
size = (m < n) ? m : n;
for (i = 0; i < size; i++) {
j = i;
temp = A[i][j];
A[i][j] = A[i][(size - j) - 1];
A[i][(size - j) - 1] = temp;
}
printf("\nMatrix after diagonals interchanged: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
2. Write a program to find the row sum, column sum, primary diagonal sum
And secondary diagonal sum of a matrix.
#include < stdio.h >
#include < conio.h >
void main() {
int array[10][10], i, j, m, n, a = 0, sum = 0;
clrscr();
printf("Enter the order of matrix\n");
scanf("%d%d", & m, & n);
if (m == n) {
printf("Enter the co efficints of the matirx\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & array[i][j]);
}
}
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) {
sum += array[i][j];
}
printf("Sum of elements of Row%d=%d\n", i + 1, sum);
}
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) {
sum += array[j][i];
}
printf("Sum of elements of column%d=%d\n", i + 1, sum);
}
sum = 0;
for (i = 0; i < m; i++) {
sum = sum + array[i][i];
a = a + array[i][m - i - 1];
}
printf("\nThe sum of the main daigonals elements is=%d\n", sum);
printf("The sum of the off diagonal elements is=%d\n", a);
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
} else {
printf("It is not a square matrix");
}
getch();
}
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
3. Write a program to check whether the given matrix is singular or not.
#include <stdio.h>
#include <conio.h>
int main() {
int i, j, k, m, det, n, a[5][5], x, y, z;
clrscr();
printf("\nenter the order of matrix\n");
scanf("%d%d", & m, & n);
printf("\nenter the element of matrix\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]);
}
}
printf("the given matrix is:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
if (m == 2 && n == 2) {
x = a[0][0] * a[1][1];
y = a[0][1] * a[1][0];
det = x - y;
} else {
x = a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1]);
y = a[0][1] * (a[1][0] * a[2][2] - a[1][2] * a[2][0]);
z = a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0]);
det = x - y + z;
}
printf(" detrminant of matrix is=%d\n", det);
if (det == 0)
printf("it is singular matrix");
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
else
printf("it is not singular matrix");
getch();
}
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
4. Write a program for the addition ,subtraction, and multiplication of two
matrices using function
#include < stdio.h >
#include < conio.h >
void sum(int a[10][10], int b[10][10], int m, int n);
void sub(int a[10][10], int b[10][10], int m, int n);
void mul(int a[10][10], int b[10][10], int m, int n);
void print(int c[10][10], int m, int n);
int main() {
int a[10][10], b[10][10], c[10][10], i, j, m, n, k;
printf("Enter the order of the matrix\n");
scanf("%d %d", & m, & n);
printf("Enter the elements of matrix A : \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]);
}
}
printf("Enter the elements of matrix B : \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j]);
}
}
printf("Addition of matrix is :\n");
sum(a, b, m, n);
printf("Subtraction of matrix is :\n");
sub(a, b, m, n);
printf("Multiplication of matrix is :\n");
mul(a, b, m, n);
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
void sum(int a[10][10], int b[10][10], int m, int n) {
int c[10][10], i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
print(c, m, n);
}
void sub(int a[10][10], int b[10][10], int m, int n) {
int c[10][10], i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
print(c, m, n);
}
void mul(int a[10][10], int b[10][10], int m, int n) {
int c[10][10], i, j, k;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
print(c, m, n);
}
void print(int c[10][10], int m, int n) {
int i, j;
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", c[i][j]);
}
printf("\n");
}}
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
5. Write a program to accept a square matrix and determine whether it is an
identify matrix of not.
#include < stdio.h >
#include < conio.h >
void main() {
int A[10][10], m, n, i, j, isIdentity;
clrscr();
printf("Enter order of matrix\n");
scanf("%d%d", & m, & n);
if (m == n) {
printf("Enter elements in matrix of size : \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & A[i][j]);
}
}
/* Check whether it is Identity matrix or not */
isIdentity = 1;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (i == j && A[i][j] != 1) {
isIdentity = 0;
} else if (i != j && A[i][j] != 0) {
isIdentity = 0;
}
}
}
if (isIdentity == 1) {
printf("\nThe given matrix is an Identity Matrix.\n");
} else {
printf("The given matrix is not Identity Matrix");
}
getch();
}
else
printf("Its not Square MAtrix\n");
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
6. Write a program to find the roots of an equation f (x) = 0 using Bisection
method.
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define f(x) (x * x) - 2
void main() {
int i, n;
float a, b, c;
clrscr();
printf("Enter the value of a and b\n");
scanf("%f%f", & a, & b);
printf("Enter the number of iteration\n");
scanf("%d", & n);
if ((f(a)) * (f(b)) < 0) {
printf("--------------------------------------------------\n");
printf(" a\t b\t c\t f(a)\t f(b)\t f(c)\t\n");
printf("--------------------------------------------------\n");
for (i = 0; i < n; i++) {
c = (a + b) / 2.0;
printf("%.3f\t %.3f\t %.3f\t %.3f\t %.3f \t %.3f\n", a, b, c, f(a), f(b), f(c));
sleep(1);
printf("----------------------------------------------------\n");
if ((f(c)) > 0) {
b = c;
} else {
a = c;
}
}
} else {
printf("Roots can't be defined");
}
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
8. Write a program to write a program to find the roots of an f (x) = 0 using
Newton — Rephson method.
#include < stdio.h >
#include < conio.h >
#include < dos.h >
#define f(x)(x * x) - 2# define fd(x) 2 * x
void main() {
float a, x[30], b, d, c, e;
int n, i;
clrscr();
printf("Enter thr initial approximation\n");
scanf("%f", & a);
printf("Enter the number of iterations\n");
scanf("%d", & n);
printf("______________________________________________\n");
printf("i\t c\t f(x)\t fd(x)\n");
printf("_________________________________________________\n");
if (a < 0)
a = -a;
x[0] = a;
for (i = 0; i < n; i++) {
b = f(x[i]);
d = fd(x[i]);
e = b / d;
x[i + 1] = x[i] - e;
printf("%d\t%f\t%f\t%f\n", i + 1, x[i + 1], f(x[i]), fd(x[i]));
printf("____________________________________________\n");
sleep(1);
}
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
9. Write a program to find the integral of a function using Trapezoidal rule.
#include < stdio.h >
#include < conio.h >
#define f(x) 1 / (1 + (x * x))
void main() {
int n, i;
float a, b, s, I, h, x[10], y[10];
clrscr();
printf("Enter the n value\n");
scanf("%d", & n);
printf("Enter the value a & b\n");
scanf("%f%f", & a, & b);
h = (b - a) / n;
printf("h=(b-a)/n =>%f\n",
h);
printf("______________________________________\n");
printf("x\t * u=f(x)\n");
printf("____________________________________\n");
for (i = 0; x[i] <= b; i++) {
x[i + 1] = x[i] + h;
y[i] = f(x[i]);
printf("%.3f\t * %.3f\t * y[%d]\n", x[i], y[i], i);
sleep(1);
printf("____________________________________\n");
}
printf("I=(h/2)*((y[0]+y[n])+2*(y[1]+y[2]+y[3]+y[4]+y[n-1]))\n");
for (i = 1; i <= (n - 1); i++) {
s = s + y[i];
printf("%f", & s);
}
I = (h / 2) * ((y[0] + y[n]) + 2 * (s));
printf("I=%f\n", I);
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
10 a)Write a program to find the integral of a function using Simpson' s
1/3rd and 3/8th rule using switch case.
#include < stdio.h >
#include < conio.h >
#include < math.h >
float sim_1(float, float, int);
int i;
float sum;
float fun(float x) {
return (1 / (1 + pow(x, 2)));
}
int main() {
float x0, x1, sum, result, x[10], y[10];
int n, cho = 0;
clrscr();
printf("Enter the lower and upper limit\n");
scanf("%f%f", & x0, & x1);
printf("Enter number of intervals:\n");
scan: scanf("%d", & n);
if (cho == 0) {
top: printf("\tEnter choice\n");
printf("\n1.for simpsons 1/3 rule\n");
scanf("%d", & cho);
}
switch (cho) {
case 1:
if (n % 2 == 0)
result = sim_1(x0, x1, n);
else {
printf("\nwrong choice of interval");
printf("Please enter even number");
goto scan;
}
break;
default:
printf("\n wrong choice enter again:");
goto top;
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
}
printf("\n The result=%f", result);
getch();
return 0;
}
float sim_1(float x0, float x1, int n) {
float result, h, x[10], y[10];
h = (x1 - x0) / n;
sum = fun(x0) + fun(x1);
x[0] = x0;
printf("~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("x\t y=f(x)\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~\n");
for (i = 0; x[i] <= x1; i++) {
x[i + 1] = x[i] + h;
y[i] = fun(x[i]);
printf("%.3f\t%.3f\ty[%d]\n", x[i], y[i], i);
printf("~~~~~~~~~~~~~~~~~~~~~~\n");
}
for (i = 1; i < n; i++) {
if (i % 2 == 0)
sum = sum + 2 * fun(x0 + i * h);
else
sum = sum + 4 * fun(x0 + i * h);
}
result = sum * (h / 3);
return (result);
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
10 b) Write a program to find the integral of a function using Simpson' s
3/8th rule using switch case.
#include < stdio.h >
#include < conio.h >
#include < math.h >
float sim_2(float, float, int);
int i;
float sum;
float fun(float x) {
return (1 / (1 + pow(x, 2)));
}
int main() {
float x0, x1, sum, result;
int n, cho = 0;
clrscr();
printf("Enter the lower and upper limit\n");
scanf("%f%f", & x0, & x1);
printf("Enter the number of intervals:\n");
scan: scanf("%d", & n);
if (cho == 0) {
top: printf("\tEnter Choice\n");
printf("\n1.for simpsons 3/8 rule\n");
scanf("%d", & cho);
}
switch (cho) {
case 1:
if (n % 3 == 0)
result = sim_2(x0, x1, n);
else {
printf("wrong choice of intervals\n");
printf("\n please enter a multiple of 3\n");
goto scan;
}
break;
default:
printf("\n wrong choice enter again:");
goto top;
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
}
printf("\n The Result=%f", result);
getch();
return 0;
}
float sim_2(float x0, float x1, int n) {
float result, h, x[10], y[10];
h = (x1 - x0) / n;
sum = fun(x0) + fun(x1);
x[0] = x0;
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("x\t y=f(x)\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for (i = 0; x[i] <= x1; i++) {
x[i + 1] = x[i] + h;
y[i] = fun(x[i]);
printf("%.3f\t%.3f\ty[%d]\n", x[i], y[i], i);
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
for (i = 1; i < n; i++) {
if (i % 3 == 0)
sum = sum + 2 * fun(x0 + i * h);
else
sum = sum + 3 * fun(x0 + i * h);
}
result = sum * (3 * h / 8);
return (result);
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
11 Write a program to solve the system of equations Ax = b using Gauss
elimination method.
#include < stdio.h >
int main() {
int i, j, k, n;
float A[20][20], c, x[10];
clrscr();
printf("\nEnter the size of matrix:");
scanf("%d", & n);
printf("\nEnter the elements of augments matrix row-wise\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= (n + 1); j++) {
printf("A[%d][%d]:", i, j);
scanf("%f", & A[i][j]);
}
}
for (j = 1; j <= n; j++) {
for (i = 1; i <= n; i++) {
if (i != j) {
c = A[i][j] / A[j][j];
for (k = 1; k <= n + 1; k++) {
A[i][k] = A[i][k] - c * A[j][k];
}
}
}
}
printf("\nThe soulution is:\n");
for (i = 1; i <= n; i++) {
x[i] = A[i][n + 1] / A[i][i];
printf("\nx%d=%f\n", i, x[i]);
}
getch();
return 0;
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
12. Write a program to solve the system of equation Ax = b using Jacobi
Iteration method
#include < stdio.h >
#include < math.h >
int main() {
int i, j, m, n, l;
float x[10], a[10][10], b[10], c[10];
printf("\nEnter the value of n : \n");
scanf("%d", & n);
printf("\nEnter the number of iterations : \n");
scanf("%d", & l);
printf("\nEnter the right hand side constants : \n");
for (i = 0; i < n; i++) {
scanf("%f", & b[i]);
}
printf("\nEnter the coefficients row wise : \n");
for (i = 0; i < n; i++) {
x[i] = 0;
for (j = 0; j < n; j++) {
scanf("%f", & a[i][j]);
}
}
m = 1;
line:
for (i = 0; i < n; i++) {
c[i] = b[i];
for (j = 0; j < n; j++) {
if (i != j) {
c[i] = c[i] - a[i][j] * x[j];
}
}
}
for (i = 1; i <= n; i++) {
x[i] = c[i] / a[i][i];
printf("x[%d]=%f\n", i, x[i]);
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
}
printf("\n");
m++;
if (m <= l) {
goto line;
} else {
printf("\nThe Solution is : \n");
for (i = 0; i < n; i++) {
printf("\nx(%d) = %f\n", i, x[i]);
}
}
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
13. Write a program to solve the system of equations Ax = b using Gauss-
Seidel method
#include < stdio.h >
#include < conio.h >
void main() {
float a[10][10], b[10], x[10], y[10];
int n = 0, m = 0, i = 0, j = 0;
printf("Enter order matrix) : ");
scanf("%d", &n);
printf("\nEnter Values to the left side of equation\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%f", &a[i][j]);
}
}
printf("\nEnter Values to the right side of equation\n");
for (i = 0; i < n; i++) {
scanf("%f", &b[i]);
}
printf("Enter initial values of x\n");
for (i = 0; i < n; i++) {
scanf("%f", &x[i]);
}
printf("\nEnter the no. of iteration : ");
scanf("%d", & m);
while (m > 0) {
for (i = 0; i < n; i++) {
y[i] = (b[i] / a[i][i]);
for (j = 0; j < n; j++) {
if (j == i)
continue;
y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
x[i] = y[i];
}
printf("x%d = %f ", i + 1, y[i]);
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
printf("\n\n");
m--;
}
getch();
}
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
1. Write a program to construct a discrete frequency distribution table and
find mean and standard deviation.
#include < stdio.h >
#include < conio.h >
#include < math.h >
int main()
{
int i;
float n, x[20], fx[20], sumfx = 0, fx2[20], N = 0, ll, ul, width, ci[20], f[20], am,
var, sd, x2[20], sumfx2 = 0;
clrscr();
printf("Enter number of observation\n");
scanf("%f", & n);
printf("Enter the element of lower limit\n");
scanf("%f", & ll);
printf("Enter the element of upper limit\n");
scanf("%f", & ul);
printf("Enter widt of ci\n");
scanf("%f", & width);
ci[0] = ll;
for (i = 0; ci[i] <= ul; i++) {
ci[i + 1] = ci[i] + width;
}
printf("ci\n");
for (i = 0; i < n; i++) {
printf("%0.2f-%0.2f\n", ci[i], ci[i + 1]);
}
printf("Enter frequency\n");
for (i = 0; i < n; i++) {
scanf("%f", & f[i]);
}
for (i = 0; i < n; i++) {
x[i] = ((ci[i] + ci[i + 1]) / 2);
N = N + f[i];
x2[i] = x[i] * x[i];
fx[i] = f[i] * x[i];
sumfx = sumfx + fx[i];
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
fx2[i] = f[i] * x2[i];
sumfx2 = sumfx2 + fx2[i];
}
am = sumfx / N;
var = ((sumfx2 / N) - (am * am));
sd = sqrt(var);
printf("\nCI\t\tf\tMP\tfx\tx2\tfx2\n");
for (i = 0; i < n; i++)
printf("%0.2f-%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\n", ci[i], ci[i + 1],
f[i], x[i], fx[i], x2[i], fx2[i]);
printf("\n");
printf("am=%f\n", am);
printf("sd=%f\n", sd);
getch();
return 0;
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
2 Write a program to construct a continuous frequency distribution table for
given data and find mean and standard deviation.
#include < stdio.h >
#include < conio.h >
#include < math.h >
int main()
{
float x[20], f[20], fx[20], sfx = 0, m = 0, sf = 0, fx2[20], sd = 0, sfx2 = 0, x2[20];
int i, n;
clrscr();
printf("Enter Limit\n");
scanf("%d", & n);
printf("Enter observation\n");
for (i = 1; i <= n; i++) {
scanf("%f", & x[i]);
}
printf("Enter Frequency\n");
for (i = 1; i <= n; i++) {
scanf("%f", & f[i]);
fx[i] = x[i] * f[i];
sf = sf + f[i];
x2[i] = x[i] * x[i];
fx2[i] = f[i] * x2[i];
sfx = sfx + fx[i];
sfx2 = sfx2 + fx2[i];
}
m = sfx / sf;
sd = sqrt((sfx2 / sf) - pow(m, 2));
printf("\nx\tf\tfx\tx2\tfx2\n");
for (i = 1; i <= n; i++)
printf("%0.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\n", x[i], f[i], fx[i], x2[i], fx2[i]);
printf("sf=%f\nsfx=%f\nsfx2=%f\nmean=%f\nsd=%f\n", sf, sfx, sfx2, m, sd);
getch();
return 0;
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
3. Write a program to find Arithmetic mean, Geometric mean and Harmonic
mean of n given number.
#include < stdio.h >
#include < conio.h >
#include < math.h >
void main()
{
int i, n;
float a[20], am, gm, hm, asum = 0, hsum = 0, gsum = 1;
clrscr();
printf("Enter the number of obversation\n");
scanf("%d", & n);
printf("Enter the elements\n");
for (i = 0; i < n; i++) {
scanf("%f", & a[i]);
}
for (i = 0; i < n; i++) {
asum = asum + a[i];
printf("asum=%f\n", asum);
hsum = (hsum + (1 / a[i]));
printf("hsum=%f\n", hsum);
gsum = gsum * a[i];
printf("gsum=%f\n.210121", gsum);
}
am = asum / n;
hm = n / hsum;
gm = pow(gsum, (float) 1 / n);
printf("Arithmetic mean=%f\n", am);
printf("Harmonic mean=%f\n", hm);
printf("Geomatric mean=%f\n", gm);
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
4. Write a program to find the mean, mode and median of continuous
Frequency distribution.
#include < stdio.h >
#include < conio.h >
int i, j, ll, up, cl, nc, l[100], u[100], f[100], sf, am, cf[100], temp, sort[100],
tempsort;
float xl, fm, f1, sfx, x[100], fx[100], f2;
void continous() {
printf("Enter the lower limit: ");
scanf("%d", & ll);
printf("Enter the class length: ");
scanf("%d", & cl);
printf("Enter the total number of class: ");
scanf("%d", & nc);
for (i = 0; i < nc; i++) {
up = ll + cl;
l[i] = ll;
u[i] = up;
ll = up;
x[i] = ((float) l[i] + (float) u[i]) / 2;
}
for (i = 0; i < nc; i++) {
printf("Enter the frequency for class %d-%d: ", l[i], u[i]);
printf("\n");
scanf("%d", & f[i]);
sort[i] = f[i];
}
for (i = 0; i < nc - 1; i++) {
for (j = i + 1; j < nc; j++) {
if (sort[i] > sort[j]) {
//Exchange them
tempsort = sort[i];
sort[i] = sort[j];
sort[j] = tempsort;
}
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
}
for (i = 0; i < nc; i++) {
if (sort[nc - 1] == f[i]) {
temp = i;
}
}
cf[0] = f[0];
for (i = 0; i < nc; i++) {
cf[i + 1] = cf[i] + f[i + 1];
}
sf = 0;
sfx = 0;
for (i = 0; i < nc; i++) {
sf += f[i];
fx[i] = x[i] * f[i];
sfx += fx[i];
}
for (i = 0; i < nc; i++) {
if ((sf / 2) >= cf[i])
temp = i + 1;
}
printf("=======MEAN=========\n");
printf(" Total %d %.2f\n", sf, sfx);
printf(" The sum of Fi is: %d\n", sf);
printf(" The sum of FiXi is: %.2f\n", sfx);
printf(" The mean is : %.4f\n", sfx / sf);
printf("--------------------------------------\n");
printf("=======Median========\n");
printf(" Total %d \n", sf);
printf(" Xl:%d\n N/2:%.2f\n F<:%d \nf:%d \nc:%d\n", l[temp], ((float)sf/(float)2),
cf[temp - 1], f[temp], cl);
printf(" The median is: %.4f\n\n", l[temp] + (((((float) sf / (float) 2) - cf[temp - 1])
/ f[temp]) * cl));
printf("========MODE=========\n");
xl = (float) l[temp];
fm = (float) f[temp];
f1 = (float) f[temp - 1];
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
f2 = (float) f[temp + 1];
printf(" Xl:%d", (int) xl);
printf(" fm:%d", (int) fm);
printf(" f1:%d", (int) f1);
printf(" f2:%d", (int) f2);
printf(" c:%d\n", cl);
printf("The mode is : %.4f\n", xl + (((fm - f1) / ((2 * fm) - f1 - f2)) * cl));
}
int main() {
continous();
}
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
5. Write a program to find the Karl Pearson correlation coefficient between
Two variables.
#include < stdio.h >
#include < conio.h >
#include < math.h >
void main() {
int i, l, m, n, x[10], y[10], sx = 0, sy = 0, sx2 = 0, sy2 = 0, sxy = 0;
float rxy;
clrscr();
printf("enter the values of n\n");
scanf("%d", & n);
printf("enter the elements of x\n");
for (i = 0; i < n; i++) {
scanf("%d", & x[i]);
}
printf("enetr the elements opf y\n");
for (i = 0; i < n; i++) {
scanf("%d", & y[i]);
}
for (i = 0; i < n; i++) {
sx = sx + x[i];
sy = sy + y[i];
sxy = sxy + (x[i] * y[i]);
sx2 = sx2 + (x[i] * x[i]);
sy2 = sy2 + (y[i] * y[i]);
}
printf("sx=%d sy=%d sxy=%d sx2=%d sy2=%d \n", sx, sy, sxy, sx2, sy2);
l = (n * sxy - sx * sy);
m = sqrt((n * sx2 - sx * sx) * (n * sy2 - sy * sy));
if (m == 0) {
printf("karl pearson co-efficientis infinity");
getch();
}
rxy = l / m;
printf("karl person co-efficient of correlation=%f", rxy);
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
6 .Write a program to find the rank correlation, coefficient between two
variables.
#include < stdio.h >
#include < conio.h >
#include < math.h >
void main()
{
int i, n;
float R1[10], R2[10], d1[10], d2[10], sum = 0, p;
clrscr();
printf("enter the limit\n");
scanf("%d", & n);
printf("enter the element of R1\n");
for (i = 0; i < n; i++) {
scanf("%f", & R1[i]);
}
printf("enter the elements of R2\n");
for (i = 0; i < n; i++) {
scanf("%f", & R2[i]);
}
printf("R1\tR2\tD1\tD2\n");
for (i = 0; i < n; i++) {
d1[i] = R1[i] - R2[i];
d2[i] = d1[i] * d1[i];
sum = sum + d2[i];
printf("d1=%f\td2=%f\n", d1[i], d2[i]);
}
p = 1 - ((6 * sum) / (n * (n * n - 1)));
printf("Rank correlation=%f", p);
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
7. Write a program to fit the linear regression equation X on Y and Y on
X.
#include <stdio.h>
#include <math.h>
void main() {
int n,i;
float sumx, sumxsq, sumy, sumxy, x, y, a0, a1, denom;
clrscr();
printf("enter the n value\n");
scanf("%d",&n);
sumx = 0;
sumxsq = 0;
sumy = 0;
sumxy = 0;
for (i=0;i<n;i++) {
scanf("%f%f",&x,&y);
sumx += x;
sumxsq += pow(x, 2);
sumy += y;
sumxy += x * y;
}
denom = n * sumxsq-pow(sumx, 2);
a0 = (sumy * sumxsq-sumx * sumxy) / denom;
a1 = (n * sumxy- sumx * sumy) / denom;
printf("y = %fx + %f", a1, a0);
getch();
}
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
8) Write a program to fit binomial distribution.
#include < stdio.h >
#include < conio.h >
#include < math.h >
int fact(int n)
{
return (n == 1 || n == 0) ? 1 : fact(n - 1) * n;
}
void main() {
float x[20], f[20], fx[20], sfx = 0, mean = 0, sf = 0, fx2[20];
float p, q, factn, factx, factnx, result = 0, nx, ncx, px, qx;
int n, i;
clrscr();
printf("enter limit\n");
scanf("%d", & n);
printf("enter observation\n");
for (i = 0; i < n; i++) {
scanf("%f", & x[i]);
}
printf("enter frequencies\n");
for (i = 0; i < n; i++) {
scanf("%f", & f[i]);
fx[i] = x[i] * f[i];
sf = sf + f[i];
sfx = sfx + fx[i];
}
mean = sfx / sf;
printf("mean=%0.3f\n", mean);
p = mean / n;
printf("p=%0.3f\n", p);
q = 1 - p;
printf("q=%0.3f\n", q);
printf("\nx\tfx\n");
for (i = 0; i < n; i++) {
nx = n - x[i];
factn = fact(n);
factx = fact(x[i]);
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
factnx = fact(nx);
ncx = factn / (factx * factnx);
px = pow(p, x[i]);
qx = pow((q), nx);
fx[i] = sf * ncx * px * qx;
printf("%0.0f\t%.3f\n", x[i], fx[i]);
result = result + fx[i];
}
printf("result=%f\n", result);
}
OUTPUT
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
9) Write a program to fit Poisson distribution
#include < stdio.h >
#include < conio.h >
#include < math.h >
#define e 2.71828
int fact(int n)
{
return (n == 1 || n == 0) ? 1 : fact(n - 1) * n;
}
void main() {
float x[20], f[20], fx[20], sfx = 0, mean = 0, sf = 0, fx2[20];
float result = 0, px[10], e1, m1, x1;
int n, i;
clrscr();
printf("enter limit\n");
scanf("%d", & n);
printf("enter observation\n");
for (i = 0; i < n; i++) {
scanf("%f", & x[i]);
}
printf("enter frequency\n");
for (i = 0; i < n; i++) {
scanf("%f", & f[i]);
fx[i] = x[i] * f[i];
sf = sf + f[i];
sfx = sfx + fx[i];
}
mean = sfx / sf;
printf("mean=%0.3f\n", mean);
printf("\nx\tpx\tfx\n");
for (i = 0; i < n; i++) {
e1 = pow(e, -mean);
m1 = pow(mean, x[i]);
x1 = fact(x[i]);
px[i] = (e1 * m1) / x1;
fx[i] = sf * px[i];
Dept of Computer Application Page No:____
BCA-203 Numerical and Statistical Method Lab
printf("%0.0f\t%0.4f\t%.3f\n", x[i], px[i], fx[i]);
result = result + fx[i];
}
printf("result=%f\n", result);
}
OUTPUT
Dept of Computer Application Page No:____