0% found this document useful (0 votes)
5 views20 pages

Circle, Triangle, and Year Programs

The document contains multiple C programs that perform various mathematical and logical operations, including calculating the area and perimeter of circles and triangles, checking leap years, finding the largest of three numbers, determining even or odd numbers, calculating factorials, solving quadratic equations, performing matrix operations, and finding roots using the Bisection Method. Each program is structured with input prompts and outputs results based on user input. The document also includes example outputs demonstrating the functionality of each program.

Uploaded by

jffyneojn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views20 pages

Circle, Triangle, and Year Programs

The document contains multiple C programs that perform various mathematical and logical operations, including calculating the area and perimeter of circles and triangles, checking leap years, finding the largest of three numbers, determining even or odd numbers, calculating factorials, solving quadratic equations, performing matrix operations, and finding roots using the Bisection Method. Each program is structured with input prompts and outputs results based on user input. The document also includes example outputs demonstrating the functionality of each program.

Uploaded by

jffyneojn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3

/* PROGRAM 1: Program to find the area and perimeter of a 4


circle and a triangle */ 5
#include <stdio.h> Area of Circle = 78.549995
#include <conio.h> Perimeter of Circle = 31.420000
#include <math.h> Area of Triangle = 6.000000
int main() Perimeter of Triangle = 12.000000
{ /* PROGRAM 2: Program to check whether the given year is a
float r, AC, PC, a, b, c, S, AT, PT, pi = 3.142; leap year or not */
printf("Enter the radius of a circle\n"); #include <stdio.h>
scanf("%f", &r); #include <conio.h>
printf("Enter the sides of a triangle\n"); int main()
scanf("%f%f%f", &a, &b, &c); {
AC = pi * r * r; int y;
PC = 2 * pi * r; printf("Enter a year:\n");
S = (a + b + c) / 2; scanf("%d", &y);
AT = sqrt(S * (S - a) * (S - b) * (S - c)); if (y % 400 == 0)
PT = a + b + c; {
printf("Area of Circle = %f\n", AC); printf("%d is a leap year\n", y);
printf("Perimeter of Circle = %f\n", PC); }
printf("Area of Triangle = %f\n", AT); else if (y % 100 == 0)
printf("Perimeter of Triangle = %f\n", PT); {
return 0; printf("%d is not a leap year\n", y);
} }
PS C:\TURBOC3\BIN> ./[Link] else if (y % 4 == 0)
Enter the radius of a circle {
6 printf("%d is a leap year\n", y);
Enter the sides of a triangle }
2 else
3 {
4 printf("%d is not a leap year\n", y);
Area of Circle = 113.112000 }
Perimeter of Circle = 37.703999 return 0;
Area of Triangle = 2.904737 }
Perimeter of Triangle = 9.000000 PS C:\TURBOC3\BIN> ./[Link]
PS C:\TURBOC3\BIN> ./[Link] Enter a year:
Enter the radius of a circle 7074
5 7074 is not a leap year
Enter the sides of a triangle PS C:\TURBOC3\BIN> ./[Link]
Enter a year: 6.7
1175 24.400000 is the largest number
1175 is not a leap year PS C:\TURBOC3\BIN> ./[Link]
/* PROGRAM 3: Program to find the largest of three numbers Enter the values of a, b, c:
*/ 345
#include <stdio.h> 567
#include <math.h> 453
#include <conio.h> 567.000000 is the largest number
int main() /* PROGRAM 4: Program to check whether the given number is
{ even or odd */
float a, b, c; #include <stdio.h>
clrscr (); #include <conio.h>
printf("Enter the values of a, b, c:\n"); int main()
scanf("%f %f %f", &a, &b, &c); {
if (a > b) int n;
{ clrscr ();
if (a > c) printf("Enter the number:\n");
{ scanf("%d", &n);
printf("%f is the largest number\n", a); if (n % 2 == 0)
} {
else printf("%d is even\n", n);
{ }
printf("%f is the largest number\n", c); else
} {
} printf("%d is odd\n", n);
else if (b > c) }
{ return 0;
printf("%f is the largest number\n", b); }
} PS C:\TURBOC3\BIN> ./[Link]
else Enter the number:
{ 12
printf("%f is the largest number\n", c); 12 is even
} PS C:\TURBOC3\BIN> ./[Link]
return 0; Enter the number:
} 2763
PS C:\TURBOC3\BIN> ./[Link] 2763 is odd
Enter the values of a, b, c: /* PROGRAM 5: Program to find the factorial of a given
24.4 number */
6.5 #include <stdio.h>
#include <math.h> #include <conio.h>
#include <conio.h> int main()
int main() {
{ float a, b, c, root1, root2, d, real, imag;
double fact = 1, num; clrscr ();
int i; printf("Enter the value of a, b, c\n");
clrscr (); scanf("%f %f %f", &a, &b, &c);
printf("Enter a number\n"); if (a == 0)
scanf("%lf", &num); {
if (num < 0) printf("Roots of the equation cannot be determined\n");
{ }
printf("Invalid number\n"); else
} {
else if (num == 0) d = b * b - 4 * a * c;
{ if (d == 0)
printf("Factorial of 0 is %lf\n", fact); {
} printf("Roots are real and equal\n");
else root1 = -b / (2 * a);
{ root2 = root1;
for (i = 1; i <= num; i++) printf("root1 = %f\n", root1);
{ printf("root2 = %f\n", root2);
fact = fact * i; }
} else if (d > 0)
printf("Factorial is %lf\n", fact); {
} printf("Roots are real and distinct\n");
return 0; root1 = (-b + sqrt(d)) / (2 * a);
} root2 = (-b - sqrt(d)) / (2 * a);
PS C:\TURBOC3\BIN> ./[Link] printf("root1 = %f\n", root1);
Enter a number printf("root2 = %f\n", root2);
8 }
Factorial is 40320.000000 else if (d < 0)
PS C:\TURBOC3\BIN> ./[Link] {
Enter a number printf("Roots are imaginary\n");
7 real = -b / (2 * a);
Factorial is 5040.000000 imag = sqrt(-d) / (2 * a);
/* PROGRAM 6: Program to find the roots of a quadratic printf("root1 = %f + i%f\n", real, imag);
equation */ printf("root2 = %f - i%f\n", real, imag);
#include <stdio.h> }
#include <math.h> }
return 0; case 10: printf("October\n"); break;
} case 11: printf("November\n"); break;
PS C:\TURBOC3\BIN> ./[Link] case 12: printf("December\n"); break;
Enter the value of a, b, c default:
2 printf("Invalid month\n");
1 }
3 return 0;
Roots are imaginary }
root1 = -0.250000 + i1.198958 PS C:\TURBOC3\BIN> ./[Link]
root2 = -0.250000 - i1.198958 Enter the n
PS C:\TURBOC3\BIN> ./[Link] 5
Enter the value of a, b, c May
1 PS C:\TURBOC3\BIN> ./[Link]
1 Enter the n
1 7
Roots are imaginary July
root1 = -0.500000 + i0.866025 /* PROGRAM 8: Program to perform addition, subtraction and
root2 = -0.500000 - i0.866025 multiplication of two matrices */
/* PROGRAM 7: Program to display the month name for a given #include <stdio.h>
number using switch-case */ #include <conio.h>
#include <stdio.h> int main()
#include <conio.h> {
int main() int m, n, c, d, p, q, k;
{ float first[10][10], second[10][10], diff[10][10], sum[10][10],
int n; mult[10][10];
clrscr (); clrscr ();
printf("Enter the n\n"); printf("Enter the order of first matrix\n");
scanf("%d", &n); scanf("%d %d", &m, &n);
switch (n) printf("Enter the order of second matrix\n");
{ scanf("%d %d", &p, &q);
case 1: printf("January\n"); break; printf("Enter the elements of first matrix\n");
case 2: printf("February\n"); break; for (c = 0; c < m; c++)
case 3: printf("March\n"); break; {
case 4: printf("April\n"); break; for (d = 0; d < n; d++)
case 5: printf("May\n"); break; {
case 6: printf("June\n"); break; scanf("%f", &first[c][d]);
case 7: printf("July\n"); break; }
case 8: printf("August\n"); break; }
case 9: printf("September\n"); break; printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++) for (c = 0; c < m; c++)
{ {
for (d = 0; d < q; d++) for (d = 0; d < n; d++)
{ {
scanf("%f", &second[c][d]); diff[c][d] = first[c][d] - second[c][d];
} printf("%0.2f\t", diff[c][d]);
} }
printf("\nFirst Matrix:\n"); printf("\n");
for (c = 0; c < m; c++) }
{ }
for (d = 0; d < n; d++) else
{ {
printf("%0.2f\t", first[c][d]); printf("\nAddition and subtraction not possible\n");
} }
printf("\n"); /* Multiplication */
} if (n == p)
printf("\nSecond Matrix:\n"); {
for (c = 0; c < p; c++) printf("\nMultiplication of entered matrices:\n");
{ for (c = 0; c < m; c++)
for (d = 0; d < q; d++) {
{ for (d = 0; d < q; d++)
printf("%0.2f\t", second[c][d]); {
} mult[c][d] = 0;
printf("\n"); for (k = 0; k < n; k++)
} {
/* Addition and Subtraction */ mult[c][d] = mult[c][d] + (first[c][k] * second[k][d]);
if (m == p && n == q) }
{ printf("%0.2f\t", mult[c][d]);
printf("\nSum of entered matrices:\n"); }
for (c = 0; c < m; c++) printf("\n");
{ }
for (d = 0; d < n; d++) }
{ else
sum[c][d] = first[c][d] + second[c][d]; {
printf("%0.2f\t", sum[c][d]); printf("\nMatrix multiplication not possible\n");
} }
printf("\n"); return 0;
} }
printf("\nDifference of entered matrices:\n"); PS C:\TURBOC3\BIN> ./[Link]
Enter the order of matrix 10.00 12.00
2 Difference of entered matrices:
Enter the coefficient matrix -4.00 -4.00
2 3 -4.00 -4.00
4 7 Multiplication of entered matrices:
Enter the column matrix 19.00 22.00
8 43.00 50.00
18 PS C:\TURBOC3\BIN> ./[Link]
lower triangular matrix is Enter the order of first matrix
2.0000 0.0000 3
4.0000 1.0000 3
upper triangular matrix is Enter the order of second matrix
1.0000 1.5000 3
0.0000 1.0000 3
solution vector is Enter the elements of first matrix
x[1] = 1.0000 1 2 3
x[2] = 2.0000 4 5 6
PS C:\TURBOC3\BIN> gcc finalturbo.c 7 8 9
PS C:\TURBOC3\BIN> ./[Link] Enter the elements of second matrix
Enter the order of first matrix 11 12 13
2 5 6 7
2 9 10 11
Enter the order of second matrix First Matrix:
2 1.00 2.00 3.00
2 4.00 5.00 6.00
Enter the elements of first matrix 7.00 8.00 9.00
1 2 Second Matrix:
3 4 11.00 12.00 13.00
Enter the elements of second matrix 5.00 6.00 7.00
5 6 9.00 10.00 11.00
7 8 Sum of entered matrices:
First Matrix: 12.00 14.00 16.00
1.00 2.00 9.00 11.00 13.00
3.00 4.00 16.00 18.00 20.00
Second Matrix: Difference of entered matrices:
5.00 6.00 -10.00 -10.00 -10.00
7.00 8.00 -1.00 -1.00 -1.00
Sum of entered matrices: -2.00 -2.00 -2.00
6.00 8.00 Multiplication of entered matrices:
48.00 54.00 60.00 {
123.00 138.00 153.00 printf("\nAfter %d iterations, the root is %0.4f\n", itr,
198.00 222.00 246.00 x);
/* PROGRAM 9: Program to find the root of an equation using break;
the Bisection Method */ }
#include <stdio.h> else
#include <math.h> {
#include <conio.h> itr++;
float f(float x) x1 = x;
{ }
return ((x*x*x) - 7*x + 5); }
} if (itr == n)
int main() printf("Insufficient iterations\n");
{ }
float a, b, x, x1; return 0;
int n, itr = 1; }
clrscr (); PS C:\TURBOC3\BIN> ./[Link]
printf("Enter the initial approximation a and b\n"); Enter the initial approximation a and b
scanf("%f %f", &a, &b); 0
if (f(a) * f(b) > 0) 1
{ Enter the number of iterations
printf("Invalid initial approximation\n"); 15
} Iteration 1 x = 0.5000
else Iteration 2 x = 0.7500
{ Iteration 3 x = 0.8750
printf("Enter the number of iterations\n"); Iteration 4 x = 0.8125
scanf("%d", &n); Iteration 5 x = 0.7813
x = (a + b) / 2; Iteration 6 x = 0.7969
printf("Iteration %d\t x = %0.4f\n", itr, x); Iteration 7 x = 0.7891
itr++; Iteration 8 x = 0.7852
while (itr < n) Iteration 9 x = 0.7832
{ Iteration 10 x = 0.7822
if (f(a) * f(x) < 0) Iteration 11 x = 0.7827
b = x; Iteration 12 x = 0.7830
else Iteration 13 x = 0.7828
a = x; Iteration 14 x = 0.7828
x = (a + b) / 2; After 14 iterations, the root is 0.7828
printf("Iteration %d\t x = %0.4f\n", itr, x); PS C:\TURBOC3\BIN> ./[Link]
if (fabs(x1 - x) < 0.0001) Enter the initial approximation a and b
1 scanf("%d", &n);
2 printf("Iteration\t root\n");
Enter the number of iterations while (itr <= n)
15 {
Iteration 1 x = 1.5000 h = f(x0) / fd(x0);
Iteration 2 x = 1.2500 x1 = x0 - h;
Iteration 3 x = 1.3750 printf("%d\t\t%0.4f\n", itr, x1);
Iteration 4 x = 1.3125 if (fabs(h) < 0.0001)
Iteration 5 x = 1.3438 {
Iteration 6 x = 1.3281 root = x1;
Iteration 7 x = 1.3203 break;
Iteration 8 x = 1.3242 }
Iteration 9 x = 1.3262 else
Iteration 10 x = 1.3252 {
Iteration 11 x = 1.3247 x0 = x1;
Iteration 12 x = 1.3250 itr++;
Iteration 13 x = 1.3248 }
Iteration 14 x = 1.3248 }
After 14 iterations, the root is 1.3248 if (root == x1)
/* PROGRAM 10: Program to find the root of an equation {
using the Newton–Raphson Method */ printf("The root is %0.4f\n", root);
#include <stdio.h> }
#include <math.h> else
#include <conio.h> {
float f(float x) printf("Insufficient iterations\n");
{ }
return ((x*x*x) +x +1); return 0;
} }
float fd(float x) PS C:\TURBOC3\BIN> ./[Link]
{ Enter the first approximation x0
return ((3*x*x) + 1); 1
} Enter the number of iterations
int main() 15
{ Iteration root
float x0, h, root = 0, x1; 1 4.3333
int n, itr = 1; 2 3.2908
printf("Enter the first approximation x0\n"); 3 2.5562
scanf("%f", &x0); 4 2.0982
printf("Enter the number of iterations\n"); 5 1.8956
6 1.8569 printf("%0.4f\t", a[i][j]);
7 1.8556 }
8 1.8556 printf("\n");
The root is 1.8556 }
PS C:\TURBOC3\BIN> ./[Link] /* Partial Pivoting */
Enter the first approximation x0 for(i = 0; i < n; i++)
0 {
Enter the number of iterations for(k = i + 1; k < n; k++)
15 {
Iteration root if(fabs(a[i][i]) < fabs(a[k][i]))
1 0.7143 {
2 0.7809 p = k;
3 0.7828 for(j = 0; j < n + 1; j++)
4 0.7828 {
The root is 0.7828 temp[j] = a[p][j];
/** 11. Program to solve the system of linear equations by a[p][j] = a[i][j];
Gauss Elimination Method */ a[i][j] = temp[j];
#include <stdio.h> }
#include <conio.h> }
#include <math.h> }
int main() }
{ /* Forward Elimination */
float a[10][10], temp[10], x[10], app, sum, mult; for(i = 0; i < n; i++)
int i, j, k, p, n; {
printf("Enter the order of the matrix:\n"); for(j = i + 1; j < n; j++)
scanf("%d", &n); {
printf("Enter the elements of the augmented matrix:\n"); mult = a[j][i] / a[i][i];
for(i = 0; i < n; i++) for(k = 0; k < n + 1; k++)
{ {
for(j = 0; j <= n; j++) a[j][k] = a[j][k] - (mult * a[i][k]);
{ }
scanf("%f", &a[i][j]); }
} }
} /* Back Substitution */
printf("The augmented matrix is\n"); x[n - 1] = a[n - 1][n] / a[n - 1][n - 1];
for(i = 0; i < n; i++) for(i = n - 2; i >= 0; i--)
{ {
for(j = 0; j <= n; j++) sum = 0;
{ for(j = i + 1; j < n; j++)
{ x[1] = 7.0000
sum = sum + (a[i][j] * x[j]); x[2] = -9.0000
} x[3] = 5.0000
x[i] = (a[i][n] - sum) / a[i][i]; /** 12. Program to solve the system of linear equations by
} Gauss-Jordan Method */
printf("The solution is \n"); #include <stdio.h>
for(i = 0; i < n; i++) #include <conio.h>
{ #include <math.h>
printf("x[%d] = %0.4f\n", i + 1, x[i]); int main()
} {
return 0; float a[10][10], x[10], c;
} int i, j, k, n;
PS C:\TURBOC3\BIN> ./[Link] printf("Enter the order of the matrix\n");
Enter the order of the matrix: scanf("%d", &n);
3 printf("Enter the elements of augmented matrix\n");
Enter the elements of the augmented matrix: for(i = 1; i <= n; i++)
2 1 4 12 {
4 11 -1 33 for(j = 1; j <= n + 1; j++)
8 -3 2 20 {
The augmented matrix is scanf("%f", &a[i][j]);
2.0000 1.0000 4.0000 12.0000 }
4.0000 11.0000 -1.0000 33.0000 }
8.0000 -3.0000 2.0000 20.0000 printf("The augmented matrix is\n");
The solution is for(i = 1; i <= n; i++)
x[1] = 3.0000 {
x[2] = 2.0000 for(j = 1; j <= n + 1; j++)
x[3] = 1.0000 {
PS C:\TURBOC3\BIN> ./[Link] printf("%0.4f\t", a[i][j]);
Enter the order of the matrix: }
3 printf("\n");
Enter the elements of the augmented matrix: }
2 1 1 10 for(j = 1; j <= n; j++)
3 2 3 18 {
1 4 9 16 for(i = 1; i <= n; i++)
The augmented matrix is {
2.0000 1.0000 1.0000 10.0000 if(i != j)
3.0000 2.0000 3.0000 18.0000 {
1.0000 4.0000 9.0000 16.0000 c = a[i][j] / a[j][j];
The solution is for(k = 1; k <= n + 1; k++)
{ 2.0000 1.0000 2.0000 10.0000
a[i][k] = a[i][k] - (c * a[j][k]); The solution is
} x[1] = 2.0000
} x[2] = 2.0000
} x[3] = 2.0000
} /** 13. Program to solve the system of linear equations by
printf("The solution is\n"); using LU Decomposition Method */
for(i = 1; i <= n; i++) # include <stdio.h>
{ # include <conio.h>
x[i] = a[i][n + 1] / a[i][i]; # include <math.h>
printf("x[%d] = %0.4f\n", i, x[i]); int main()
} {
return 0; int i, j, k, n, p;
} float a[20][20] = {0}, b[20] = {0},
PS C:\TURBOC3\BIN> ./[Link] l[20][20] = {0}, u[20][20] = {0},
Enter the order of the matrix x[20] = {0}, z[20] = {0}, sum;
3 printf("Enter the order of matrix\n");
Enter the elements of augmented matrix scanf("%d", &n);
1 1 1 9 printf("Enter the coefficient matrix\n");
2 -3 4 13 for(i = 1; i <= n; i++)
3 4 5 40 {
The augmented matrix is for(j = 1; j <= n; j++)
1.0000 1.0000 1.0000 9.0000 {
2.0000 -3.0000 4.0000 13.0000 scanf("%f", &a[i][j]);
3.0000 4.0000 5.0000 40.0000 }
The solution is }
x[1] = 1.0000 printf("Enter the column matrix\n");
x[2] = 3.0000 for(i = 1; i <= n; i++)
x[3] = 5.0000 {
PS C:\TURBOC3\BIN> ./[Link] scanf("%f", &b[i]);
Enter the order of the matrix }
3 for(k = 1; k <= n; k++)
Enter the elements of augmented matrix {
1 3 1 10 u[k][k] = 1;
1 -2 -1 -4 for(i = k; i <= n; i++)
2 1 2 10 {
The augmented matrix is sum = 0;
1.0000 3.0000 1.0000 10.0000 for(p = 1; p <= k; p++)
1.0000 -2.0000 -1.0000 -4.0000 {
sum = sum + (l[i][p] * u[p][k]); z[i] = (b[i] - sum) / l[i][i];
} }
l[i][k] = a[i][k] - sum; for(i = n; i > 0; i--)
} {
for(j = k + 1; j <= n; j++) sum = 0;
{ for(p = n; p > i; p--)
sum = 0; {
for(p = 1; p <= k; p++) sum = sum + (u[i][p] * x[p]);
{ }
sum = sum + (l[k][p] * u[p][j]); x[i] = (z[i] - sum) / u[i][i];
} }
u[k][j] = (a[k][j] - sum) / l[k][k]; printf("solution vector is\n");
} for(i = 1; i <= n; i++)
} {
printf("lower triangular matrix is\n"); printf("x[%d] = %0.4f\n", i, x[i]);
for(i = 1; i <= n; i++) }
{ return 0;
for(j = 1; j <= n; j++) }
{ PS C:\TURBOC3\BIN> ./[Link]
printf("%0.4f\t", l[i][j]); Enter the order of matrix
} 2
printf("\n"); Enter the coefficient matrix
} 2 3
printf("upper triangular matrix is\n"); 4 7
for(i = 1; i <= n; i++) Enter the column matrix
{ 8
for(j = 1; j <= n; j++) 18
{ lower triangular matrix is
printf("%0.4f\t", u[i][j]); 2.0000 0.0000
} 4.0000 1.0000
printf("\n"); upper triangular matrix is
} 1.0000 1.5000
for(i = 1; i <= n; i++) 0.0000 1.0000
{ solution vector is
sum = 0; x[1] = 1.0000
for(p = 1; p < i; p++) x[2] = 2.0000
{ PS C:\TURBOC3\BIN> ./[Link]
sum = sum + (l[i][p] * z[p]); Enter the order of matrix
} 3
Enter the coefficient matrix printf("Iteration are \n");
1 1 1 for(i=1; i<=n; i++)
2 3 1 {
1 2 3 x[i] = 0;
Enter the column matrix }
6 do
14 {
14 big = 0;
lower triangular matrix is for(i=1; i<=n; i++)
1.0000 0.0000 0.0000 {
2.0000 1.0000 0.0000 sum = 0;
1.0000 1.0000 3.0000 for(j=1; j<=n; j++)
upper triangular matrix is {
1.0000 1.0000 1.0000 if(j != i)
0.0000 1.0000 -1.0000 {
0.0000 0.0000 1.0000 sum = sum + (a[i][j] * x[j]);
solution vector is }
x[1] = 0.0000 }
x[2] = 4.0000 temp = (a[i][n+1] - sum) / a[i][i];
x[3] = 2.0000 error = fabs(x[i] - temp);
/** 14. Program to solve the system of linear equations if(error > big)
using Gauss-Seidel Method */ {
# include <stdio.h> big = error;
# include <conio.h> }
# include <math.h> x[i] = temp;
int main() printf("x[%d]=%0.4f\t", i, x[i]);
{ }
int i, j, n; printf("\n");
float a[10][10], x[10], sum, temp, error, big; }
printf("Enter the number of equation\n"); while(big > 0.0001);
scanf("%d", &n); printf("\n solution is \n");
printf("Enter the coefficient of the equation\n"); for(i=1; i<=n; i++)
for(i=1; i<=n; i++) {
{ printf("\n x[%d]=%0.4f\t", i, x[i]);
for(j=1; j<=n+1; j++) }
{ return 0;
scanf("%f", &a[i][j]); }
} PS C:\TURBOC3\BIN> ./[Link]
} Enter the number of equation
3 float x[10], y[10][10], sum, p, r, temp;
Enter the coefficient of the equation int i, j, n, k = 0, f, m;
27 6 -1 85 printf("Enter the number of records\n");
6 15 2 72 scanf("%d", &n);
1 1 54 110 for(i = 0; i < n; i++)
Iteration are {
x[1]=3.1481 x[2]=3.5407 x[3]=1.9132 printf("Enter the values of x%d ", i);
x[1]=2.4322 x[2]=3.5720 x[3]=1.9258 scanf("%f", &x[i]);
x[1]=2.4257 x[2]=3.5729 x[3]=1.9260 printf("Enter the values of f(x%d) ", i);
x[1]=2.4255 x[2]=3.5730 x[3]=1.9260 scanf("%f", &y[k][i]);
x[1]=2.4255 x[2]=3.5730 x[3]=1.9260 }
solution is printf("Enter x for finding f(x) ");
x[1]=2.4255 scanf("%f", &p);
x[2]=3.5730 for(i = 1; i < n; i++)
x[3]=1.9260 {
PS C:\TURBOC3\BIN> ./[Link] for(j = 0; j < n - i; j++)
Enter the number of equation {
3 y[i][j] = y[i - 1][j + 1] - y[i - 1][j];
Enter the coefficient of the equation }
20 1 -2 17 }
3 20 -1 -18 printf("\nx\ty");
2 -3 20 25 for(i = 1; i < n; i++)
Iteration are {
x[1]=0.8500 x[2]=-1.0275 x[3]=1.0109 printf("\ty%d", i);
x[1]=1.0025 x[2]=-0.9998 x[3]=0.9998 }
x[1]=1.0000 x[2]=-1.0000 x[3]=1.0000 for(i = 0; i < n; i++)
x[1]=1.0000 x[2]=-1.0000 x[3]=1.0000 {
solution is printf("\n%0.3f\t", x[i]);
x[1]=1.0000 for(j = 0; j < n - i; j++)
x[2]=-1.0000 {
x[3]=1.0000 printf("%0.3f\t", y[j][i]);
/** 15. Program to find the value of f(x) by using Newton }
Forward Interpolation Formula */ }
#include <stdio.h> f = 0;
#include <conio.h> r = (p - x[f]) / (x[f + 1] - x[f]);
#include <math.h> printf("\nr = %0.4f\n", r);
float fact(int); sum = 0;
int main() for(i = 0; i < n; i++)
{ {
temp = 1; Enter the number of records
for(j = 0; j < i; j++) 5
temp = temp * (r - j); Enter the values of x0 40
m = fact(i); Enter the values of f(x0) 0.6428
sum = sum + temp * (y[i][f] / m); Enter the values of x1 45
} Enter the values of f(x1) 0.7071
printf("\nf(%0.4f) = %0.4f", p, sum); Enter the values of x2 50
return 0; Enter the values of f(x2) 0.7660
} Enter the values of x3 55
float fact(int a) Enter the values of f(x3) 0.8192
{ Enter the values of x4 60
if(a == 0) Enter the values of f(x4) 0.8660
return 1; Enter x for finding f(x) 42
else x y y1 y2 y3 y4
return a * fact(a - 1); 40.000 0.643 0.064 -0.005 -0.000 -0.000
} 45.000 0.707 0.059 -0.006 -0.001
PS C:\TURBOC3\BIN> ./[Link] 50.000 0.766 0.053 -0.006
Enter the number of records 55.000 0.819 0.047
5 60.000 0.866
Enter the values of x0 0 r = 0.4000
Enter the values of f(x0) 1 f(42.0000) = 0.6692
Enter the values of x1 0.1 /** 16. Program to find the value of f(x) by using Newton
Enter the values of f(x1) 1.3499 Backward Interpolation Formula */
Enter the values of x2 0.2 #include <stdio.h>
Enter the values of f(x2) 1.8221 #include <conio.h>
Enter the values of x3 0.3 #include <math.h>
Enter the values of f(x3) 2.4596 float fact(int);
Enter the values of x4 0.4 int main()
Enter the values of f(x4) 3.3201 {
Enter x for finding f(x) 0.05 float x[10], y[10][10], sum, p, r, temp;
x y y1 y2 y3 y4 int i, j, n, k = 0, f, m;
0.000 1.000 0.350 0.122 0.043 0.015 printf("Enter the number of records\n");
0.100 1.350 0.472 0.165 0.058 scanf("%d", &n);
0.200 1.822 0.637 0.223 for(i = 0; i < n; i++)
0.300 2.460 0.861 {
0.400 3.320 printf("Enter the value of x%d: ", i);
r = 0.5000 scanf("%f", &x[i]);
f(0.0500) = 1.1618 printf("Enter the value of f(x%d): ", i);
PS C:\TURBOC3\BIN> ./[Link] scanf("%f", &y[k][i]);
} if(a == 0)
printf("Enter x for finding f(x): "); return 1;
scanf("%f", &p); else
/* Backward difference table */ return a * fact(a - 1);
for(i = 1; i < n; i++) }
{ PS C:\TURBOC3\BIN> ./[Link]
for(j = i; j < n; j++) Enter the number of records
{ 6
y[i][j] = y[i - 1][j] - y[i - 1][j - 1]; Enter the value of x0: 40
} Enter the value of f(x0): 204
} Enter the value of x1: 50
printf("\nx \ty"); Enter the value of f(x1): 224
for(i = 1; i < n; i++) Enter the value of x2: 60
printf("\ty%d", i); Enter the value of f(x2): 246
for(i = 0; i < n; i++) Enter the value of x3: 70
{ Enter the value of f(x3): 270
printf("\n%0.3f\t", x[i]); Enter the value of x4: 80
for(j = 0; j <= i; j++) Enter the value of f(x4): 296
{ Enter the value of x5: 90
printf("%0.3f\t", y[j][i]); Enter the value of f(x5): 324
} Enter x for finding f(x): 84
} x y y1 y2 y3 y4 y5
f = n - 1; 40.000 204.000
r = (p - x[f]) / (x[f] - x[f - 1]); 50.000 224.000 20.000
printf("\nr = %0.4f\n", r); 60.000 246.000 22.000 2.000
sum = 0; 70.000 270.000 24.000 2.000 0.000
for(i = 0; i < n; i++) 80.000 296.000 26.000 2.000 0.000 0.000
{ 90.000 324.000 28.000 2.000 0.000 0.000 0.000
temp = 1; r = -0.6000
for(j = 0; j < i; j++) f(84.0000) = 306.9600
temp = temp * (r + j); PS C:\TURBOC3\BIN> ./[Link]
m = fact(i); Enter the number of records
sum = sum + temp * (y[i][f] / m); 5
} Enter the value of x0: 45
printf("\nf(%0.4f) = %0.4f", p, sum); Enter the value of f(x0): 20
return 0; Enter the value of x1: 55
} Enter the value of f(x1): 60
float fact(int a) Enter the value of x2: 65
{ Enter the value of f(x2): 120
Enter the value of x3: 75 k = i;
Enter the value of f(x3): 180 for(j = 0; j < n; j++)
Enter the value of x4: 85 {
Enter the value of f(x4): 200 if(k == j)
Enter x for finding f(x): 80 {
x y y1 y2 y3 y4 continue;
45.000 20.000 }
55.000 60.000 40.000 else
65.000 120.000 60.000 20.000 {
75.000 180.000 60.000 0.000 -20.000 temp = temp * ((p - x[j]) / (x[k] - x[j]));
85.000 200.000 20.000 -40.000 -40.000 -20.000 }
r = -0.5000 }
f(80.0000) = 198.2813 f[i] = y[i] * temp;
/** 17. Program to find the value of f(x) using Lagrange }
Interpolation Formula */ for(i = 0; i < n; i++)
# include <stdio.h> {
# include <conio.h> sum = sum + f[i];
# include <math.h> }
int main() printf("\n\n f(%0.3f) = %f", p, sum);
{ return 0;
int i, j, k, n; }
float x[10], y[10], f[10], sum = 0, temp, p; PS C:\TURBOC3\BIN> ./[Link]
printf("enter the number of values \n"); enter the number of values
scanf("%d", &n); 4
printf("enter the value of x \n"); enter the value of x
for(i = 0; i < n; i++) 5
{ 6
scanf("%f", &x[i]); 9
} 11
printf("enter the value of f(x) \n"); enter the value of f(x)
for(i = 0; i < n; i++) 380
{ -2
scanf("%f", &y[i]); 196
} 508
printf("enter the value of x for finding f(x) \n"); enter the value of x for finding f(x)
scanf("%f", &p); 10
for(i = 0; i < n; i++) f(10.000) = 396.666656
{ /** 18. Program to find integral of a function using
temp = 1; Trapezoidal Method */
# include <stdio.h> 1
# include <conio.h> h = 0.166667
# include <math.h> x f(x)
# define f(x) 1/(1+(x*x)) 0.0000 1.0000
int main() 0.1667 0.8571
{ 0.3333 0.7500
int i, n; 0.5000 0.6667
float x[20], y[20], s = 0, h, k, a, b; 0.6667 0.6000
printf("Enter the number of iterations\n"); 0.8333 0.5455
scanf("%d", &n); 1.0000 0.5000
printf("Enter the lower limit\n"); k = 0.694877
scanf("%f", &a); PS C:\TURBOC3\BIN> gcc finalturbo.c
printf("Enter the upper limit\n"); PS C:\TURBOC3\BIN> ./[Link]
scanf("%f", &b); Enter the number of iterations
h = (b - a) / n; 6
printf("h = %f", h); Enter the lower limit
x[0] = a; -3
printf("\n x\t f(x)\n"); Enter the upper limit
for(i = 0; i <= n; i++) 3
{ h = 1.000000
x[i+1] = x[i] + h; x f(x)
y[i] = f(x[i]); -3.0000 81.0000
printf("%0.4f\t%0.4f\n", x[i], y[i]); -2.0000 16.0000
} -1.0000 1.0000
for(i = 1; i < n; i++) 0.0000 0.0000
{ 1.0000 1.0000
s = s + y[i]; 2.0000 16.0000
} 3.0000 81.0000
printf("\n"); k = 115.000000
k = (h/2) * (y[0] + y[n] + (2 * s)); /** 19. Program to find integral of a function using
printf("k = %f\n", k); Simpson’s 1/3rd Rule */
return 0; #include <stdio.h>
} #include <math.h>
PS C:\TURBOC3\BIN> ./[Link] #define f(x) (1/(1+(x*x)))
Enter the number of iterations int main()
6 {
Enter the lower limit int i, n;
0 float x[20], y[20], s1 = 0, s2 = 0, h, k, a, b;
Enter the upper limit printf("Enter the number of iterations \n");
scanf("%d", &n); 1.000 0.500
if (n % 2 == 0) 2.000 0.200
{ 3.000 0.100
printf("Enter the lower limit\n"); 4.000 0.059
scanf("%f", &a); 5.000 0.038
printf("Enter the upper limit\n"); 6.000 0.027
scanf("%f", &b); k=1.366174
h = (b - a) / n; /** 20. Program to find integral of a function using
printf("h=%f\n", h); Simpson’s 3/8th Rule */
printf("\nx\tf(x)\n"); #include <stdio.h>
for (i = 0; i <= n; i++) #include <math.h>
{ #define f(x) (((x*x)-1)/((x*x)+1))
x[i] = a + i * h; int main()
y[i] = f(x[i]); {
printf("%0.3f\t%0.3f\n", x[i], y[i]); int i, n;
} float x[20], y[20], s1 = 0, s2 = 0, h, k, a, b;
for (i = 2; i < n; i += 2) printf("Enter the number of iterations\n");
s1 += y[i]; scanf("%d", &n);
for (i = 1; i < n; i += 2) if (n % 3 == 0)
s2 += y[i]; {
k = (h / 3) * (y[0] + y[n] + (4 * s2) + (2 * s1)); printf("Enter the lower limit\n");
printf("k=%f\n", k); scanf("%f", &a);
} printf("Enter the upper limit\n");
else scanf("%f", &b);
{ h = (b - a) / n;
printf("Simpson’s 1/3 rule is not applicable\n"); printf("h = %0.3f\n", h);
} printf("\n x\t f(x)\n");
return 0; for (i = 0; i <= n; i++)
} {
PS C:\TURBOC3\BIN> ./[Link] x[i] = a + i * h;
Enter the number of iterations y[i] = f(x[i]);
6 printf("%0.3f\t%0.3f\n", x[i], y[i]);
Enter the lower limit }
0 for (i = 1; i < n; i++)
Enter the upper limit {
6 if (i % 3 == 0)
h=1.000000 s1 = s1 + y[i]; // multiples of 3
x f(x) else
0.000 1.000 s2 = s2 + y[i]; // remaining terms
}
k = ((3 * h) / 8) * (y[0] + y[n] + (2 * s1) + (3 * s2));
printf("\nResult (k) = %f\n", k);
}
else
{
printf("Simpson’s 3/8 rule is not applicable\n");
}
return 0;
}
PS C:\TURBOC3\BIN> ./[Link]
Enter the number of iterations
9
Enter the lower limit
1
Enter the upper limit
2.8
h = 0.200
x f(x)
1.000 0.000
1.200 0.180
1.400 0.324
1.600 0.438
1.800 0.528
2.000 0.600
2.200 0.658
2.400 0.704
2.600 0.742
2.800 0.774
Result (k) = 0.915249

You might also like