3rd Sem CC-6 Project
3rd Sem CC-6 Project
1
INDEX
SERIAL NO. TOPICS PAGE NO. SIGNATURE
METHOD
METHOD
ORDER METHOD
METHOD
METHOD
METHOD
2
𝟐 𝟐)𝒅𝒙
ALGORITHM:
1. Input the upper limit (a), lower limit (b), and number of intervals (n).
- Calculate y = f(x).
- Update x to x + 2h.
- Set x to a + 2h.
- Calculate y = f(x).
- Update x to x + 2h.
3
8. Output the result (r).
CODE:
#include<stdio.h> int
main()
float a,b,x,y,yn,y0,s,r,h;
int i,n;
float f(float);
scanf("%f", &a);
scanf("%f", &b);
scanf("%d",
&n); h=(b-a)/n;
y0=f(a); yn=f(b);
x=a+h;
s=0;
for(i=1;i<=(n-1);i++)
4
{
y=f(x);
s=s+y;
x=x+h;
r=(h/2)*((y0+yn)+(2*s));
return 0;
float f(float x)
return 1/(1+(x*x));
OUTPUT
5
𝟐
QUESTION 2: WRITE A PROGRAM IN C TO EVALUATE ∫𝟏 𝟏/(𝟏 + 𝒙)𝒅𝒙
ALGORITHM:
1. Get user input for upper limit (a), lower limit (b), and number of intervals (n).
- y0 = f(a)
- yn = f(b)
4. Initialize variable x to a + h.
a. For i = 1 to n - 1:
i. Calculate y = f(x).
6
7. Output the result (r).
CODE:
#include<stdio.h> int
main()
float a,b,x,y,yn,y0,s,r,h;
int i,n;
float f(float);
scanf("%f", &a);
scanf("%f", &b);
scanf("%d",
&n); h=(b-a)/n;
y0=f(a); yn=f(b);
7
x=a+h;
s=0;
for(i=1;i<=(n-1);i++)
y=f(x);
s=s+y;
x=x+h;
r=(h/2)*((y0+yn)+(2*s));
return 0;
float f(float x)
return 1/(1+(x*x));
OUTPUT:
8
QUESTION 3: WRITE A C PROGRAM TO FIND A REAL ROOT OF THE
ALGORITHM:
1. Initialize variables x0, x1, x2, f0, f1, f2. 2.
c. Compute f2 = F(x2).
9
d. Display the current iteration data: x0, x1, x2, f0, f1, f2.
CODE:
#include<stdio.h>
#include<math.h>
int main()
float x0,x1,x2,f0,f1,f2;
do
scanf("%f", &x0);
while(F(x0)>0);
do
10
printf("Enter the value of x1 ");
scanf("%f", &x1);
while(F(x1)<0);
printf("\n________________________________________________________________
________\n");
printf("\n________________________________________________________________
________\n");
do
f0=F(x0);
f1=F(x1);
x2=x0-(f0*(x1-x0)/(f1-f0));
f2=F(x2);
if(f0*f2<0)
x1=x2;
else
11
{
x0=x2;
while(fabs(f2)>ESP);
printf("\n________________________________________________________________
________\n");
OUTPUT:
12
QUESTION 4: WRITE A C PROGRAM TO FIND A REAL ROOT OF THE
ALGORITHM:
function newtonRaphson(coef, u, x1, tolerance):
c=0
repeat
c=c+1
fx1 = 0
fdx1 = 0
// Display iteration details print("Iteration:", c, "x1:", x1, "fx1:", fx1, "fdx1:", fdx1)
13
// Check for convergence if abs(x2 - x1) < tolerance:
break
CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
coef[10]={0}; float
x1=0,x2=0,t=0; float
fx1=0,fdx1=0; int
main()
&u); for(i=0;i<=u;i++)
printf("\n\t x^%d::",i);
scanf("%d", &coef[i]);
14
printf("\n"); printf("The
polynomial\n");
for(i=u;i>=0;i--)
printf("%dx^%d ",coef[i],i);
scanf("%f", &x1);
do {
c++;
fx1=fdx1=0;
for(i=u;i>=1;i--)
fx1+=coef[i]* (pow(x1,i));
fx1+=coef[0];
for(i=u;i>=0;i--)
fdx1+=coef[i] * (i*pow(x1,(i-1)));
15
}
t=x2;
x2=(x1-(fx1/fdx1));
x1=x2;
while((fabs(t-x1))>=0.0001);
return 0;
OUTPUT:
16
QUESTION 5: WRITE A C PROGRAM TO FIND A REAL ROOT OF THE
ALGORITHM:
function secantMethod(x1, x2, tolerance):
/ (f2 - f1)) print("x1:", x1, "x2:", x2, "x3:", x3, "f(x1):", f1,
break
CODE:
#include<stdio.h>
17
#include<math.h>
main()
float x1,x2,x3,f1,f2,t;
do
f1=F(x1);
f2=F(x2);
x3=x2-(f2*(x2-x1)/(f2-f1)); printf("%f\t
x1=x2;
x2=x3;
if(f2<0)
t=fabs(f2);
18
else
t=f2;
while(t>ESP);
return 0;
OUTPUT:
19
Y=2 WHEN X=0 AND H=0.001 USING EULER’S METHOD.
ALGORITHM:
function eulerMethod(x, y, h, xf):
n = (xf - x) / h
repeat n times
find: ")
eulerMethod(x, y, h, xf)
CODE:
#include<stdio.h> int
main()
float x,y,h,n,xf;
int i;
20
float f(float, float);
scanf("%f", &x);
scanf("%f", &y);
scanf("%f", &h);
for(i=1;i<=(int)n;i++)
y=y+(h*f(x,y));
x=x+h;
return 0;
y)
return 1+(x*y);
21
OUTPUT:
Y=0 WHEN X=0 AND H=0.2 USING RUNGE KUTTA 4TH ORDER
METHOD.
22
ALGORITHM:
function runRungeKutta(x0, y0, h, n):
k1) print("k2 =", k2) print("k3 =", k3) print("k4 =", k4) print("y(",
# Input values x0 = input("Enter the value of x0: ") y0 = input("Enter the value of
y0: ") h = input("Enter the value of h: ") n = input("Enter the value of the last
point: ")
y0, h, n)
CODE:
#include<stdio.h>
#include<math.h>
main()
double x0,y0,y1,h,f,n,k1,k2,k3,k4;
23
of y0:"); scanf("%lf", &y0);
of last point:");
scanf("%lf", &n);
f=F(x0,y0);
k1=h*f;
f=F(x0+(h/2),y0+(k1/2));
k2=h*f;
f=F(x0+(h/2),y0+(k2/2));
k3=h*f;
f=F(x0+h,y0+k3);
k4=h*f;
y0=y1;
24
}
return 0;
OUTPUT:
METHOD.
2x + y + z = 10
3x + 2y + 3z = 18
X + 4y + 9z = 16
ALGORITHM:
25
function gaussianElimination(mat, n):
for j from 0 to n:
2:
for j from 0 to n:
26
mat[0][0] print("\n\nx =", x) print("\ny =", y)
print("\nz =", z)
Elimination gaussianElimination(mat, n)
CODE:
#include<stdio.h>
#include<math.h> int
main()
float mat[4][4],temp,temp1,x,y,z;
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
scanf("%f", &mat[i][j]);
27
printf("\nEnter the value of the constant:");
scanf("%f", &mat[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n+1;j++)
printf("\n\n");
temp=mat[1][0]/mat[0][0];
temp1=mat[2][0]/mat[0][0]; for(i=0,j=0;j<n+1;j++)
mat[i+1][j]=mat[i+1][j] - (mat[i][j]*temp);
mat[i+2][j]=mat[i+2][j] - (mat[i][j]*temp1);
temp=mat[2][1]/mat[1][1];
for(i=1,j=0;j<n+1;j++)
28
mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);
for(i=0;i<n;i++) {
for(j=0;j<n+1;j++) {
printf("\n\n");
z=mat[2][3]/mat[2][2]; y=(mat[1][3]-
mat[1][2]*z)/mat[1][1]; x=(mat[0][3]-mat[0][2]*z-
return 0;
OUTPUT:
29
QUESTION 9: WRITE A C PROGRAM TO SOLVE THE FOLLOWING
2x + y + z = 10
3x + 2y + 3z = 18
X + 4y + 9z = 16
ALGORITHM:
function jacobiIteration(x1, x2, x3):
30
# Set the convergence tolerance ESP = 0.0001
# Initialize variables x1 = 0 x2 = 0 x3 = 0 i = 0
y1, y2, y3 = jacobiIteration(x1, x2, x3) if abs(y1 - x1) < ESP and
i = 1 else x1 = y1 x2 = y2
x3 = y3 print("\n%f\t%f\t%f",
until i = 1 return 0
CODE:
#include<stdio.h>
#include<math.h>
main()
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=0;
31
printf("\n x1\t\t x2\t\t x3\n");
do
y1=x1(x2,x3);
y2=x2(x1,x3); y3=x3(x1,x2);
printf("\n\nx1 = %.3lf",y1);
%.3lf",y3); i=1; }
else
x1=y1;
x2=y2;
x3=y3;
printf("\n%f\t%f\t%f", x1,x2,x3);
while(i!=1);
return 0;
32
OUTPUT:
2x + y + z = 10
3x + 2y + 3z = 18
X + 4y + 9z = 16
ALGORITHM:
function jacobiIteration(x1, x2, x3):
33
# Set the convergence tolerance
ESP = 0.0001
# Initialize variables
x1 = 0 x2 = 0 x3 = 0
i=0
x2, x3)
if abs(y1 - x1) < ESP and abs(y2 - x2) < ESP and abs(y3 - x3) < ESP:
=", y3)
i=1 else x1 = y1 x2 =
y2 x3 = y3
34
until i = 1
return 0
CODE:
#include<stdio.h>
#include<math.h>
main()
double x1=0,x2=0,x3=0,y1,y2,y3;
int i=0;
do
y1=x1(x2,x3);
y2=x2(y1,x3); y3=x3(y1,y2);
35
printf("\n\nx1 = %.3lf",y1);
%.3lf",y3); i=1;
else
x1=y1;
x2=y2;
x3=y3;
printf("\n%f\t%f\t%f", x1,x2,x3);
while(i!=1);
return 0;
OUTPUT:
36
37