CPP Program
CPP Program
Objective 1: C program to find absolute error, relative error and percentage error.
#include <math.h>
int main() { float
aV, tV;
float aE, rE, pE;
aE = fabs(tV - aV); rE
= (aE / tV) * 100; pE
= rE;
Output:-
1
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023
Objective 2: C program to find the real root of the given equation using Bisection method
#include<stdio.h>
#include<math.h> #define F(x) x*x*x-2*x-5 int main(){ int
i=1; float a,b,c,f; printf("Enter the value of interval a and b:");
scanf("%f %f",&a,&b);
do{ c=(a+b)/2; f=F(c);
printf("\n i=%d a=%f b=%f c=%f F(c)=%f",i,a,b,c,f);
if(F(c)<0){ a=c; } else{b=c;} i++;
}
while (fabs(F(c))>0.001);
printf("\n \n approximate root = %.4f\n",c);
return 0;}
OUTPUTT:-
2
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023
Objective 3: C program to find the real root of the given equation using Regula Falsi
method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) x*log10(x) - 1.2 int
main() { float x0, x1, x2, f0, f1, f2, e;
int step = 1; printf("\nEnter two initial
guesses:\n"); scanf("%f%f", &x0,
&x1); printf("Enter tolerable error:\n");
scanf("%f", &e); f0 = f(x0); f1 = f(x1);
if( f0*f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n"); goto up; }
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n"); do{ x2 =
x0 - (x0-x1) * f0/(f0-f1); f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);
if(f0*f2 < 0)
{
x1 = x2;
f1 = f2; } else{
x0 = x2; f0 =
f2;} step = step
+ 1;
} while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
return 0;
}
Output:-
3
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023
4
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023
Objective 4: C program to find the real root of the given equation using Secant method.
#include<stdio.h>
#include<math.h>
#define f(x) x*x*x - 2*x – 5
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
printf("\nEnter initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Entermaximum iteration:\n");
scanf("%d", &N);
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{ f0 = f(x0); f1 = f(x1); if(f0 == f1){
printf("Mathematical Error.");
exit(0);
} x2 = x1 - (x1 - x0) * f1/(f1-f0);
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,x1,x2, f2);
x0 = x1; f0 = f1; x1 = x2; f1 = f2;
step = step + 1;
if(step > N)
{ printf("Not Convergent.");
exit(0);
}
5
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023
} while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
return 0; }
OUTPUTT:-
6
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023
Objective 5: C program to find the real root of the given equation using Newton’s Raphson
method
#include<stdio.h>
#include<math.h>
#define f(x) 3*x - cos(x) -1
#define g(x) 3 + sin(x)
int main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
printf("\nEnter initial guess:\n");
scanf("%f", &x0);
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("Entermaximum iteration:\n");
scanf("%d", &N);
printf("\nStep\t\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do
{ g0 = g(x0);
f0 = f(x0);
if(g0== 0.0)
{ printf("Mathematical Error.");
exit(0);
} x1 = x0 - f0/g0;
printf("%d\t\t%f\t%f\t%f\t%f\n",step,x0,f0,x1,f1);
x0 = x1; step = step+1;
if(step > N)
{
printf("Not Convergent.");
exit(0); } f1 = f(x1);
7
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023
}while(fabs(f1)>e);
printf("\nRoot is: %f", x1);
return 0; }
Output:-
8
Name :- Pankaj Bisht Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 32 Section:- E2 (4th -sem)
Student ID :- 220411023
Objective 6: C program to find the real root of the given equation using Iterative method
#include<stdio.h>
#include<math.h>
#define f(x) cos(x)-3*x+1
#define g(x) (1+cos(x))/3
int main()
{ printf("Enter initial guess: ");
scanf("%f", &x0);
printf("Enter tolerable error: ");
scanf("%f", &e);
printf("Enter maximum iteration: ");
scanf("%d",&N);
printf("\nStep\tx0\t\tf(x0)\t\tx1\t\tf(x1)\n");
do{ x1 = g(x0);
printf("%d\t%f\t%f\t%f\t%f\n",step, x0, f(x0), x1, f(x1));
step = step + 1;
if(step>N){ printf("Not Convergent.");
exit(0);
} x0 = x1;
}while( fabs(f(x1)) > e);
printf("\nRoot is %f", x1);
return 0;}
Output:-
9
Name :- Priyanshu Saklani Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 40 Section:- E2 (4th -sem)
Student ID :-220411069
10
Name :- Priyanshu Saklani Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 40 Section:- E2 (4th -sem)
Student ID :-22041106
11
Name :- Priyanshu Saklani Subject Name:- CBNST Lab
Course:- B.C.A Subject Code:- PBC-402
Class Roll No:- 40 Section:- E2 (4th -sem)
Student ID :-220411069
12