Anda di halaman 1dari 23

1; /*A Program in C to find the solution of a given set of simultaneous linear equations

by using Gauss's Elimination method*/


#include<stdio.h>
main(){
int i,j,k,n;
float a[20][20],x[20];
double s,p;
printf("Enter the number of equations");
scanf("%d",&n);
printf("Enter the coefficients of the equations");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("a[%d][%d]=",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("b[%d]=",i+1);
scanf("%f",&a[i][n]);
}
for(k=0;k<n-1;k++){
for(i=k+1;i<n;i++){
p=a[i][k]/a[k][k];
for(j=k;j<n+1;j++)
a[i][j]=a[i][j]-p*a[k][j];
}
}
x[n-1]=a[n-1][n]/a[n-1][n-1];
for(i=n-2;i>=0;i--){
s=0;
for(j=i+1;j<n;j++){
s=s+(a[i][j]*x[j]);
x[i]=(a[i][n]-s)/a[i][i];
}
}
printf("The result is");
for(i=0;i<n;i++)
printf("\n x[%d]=%.2f",i+1,x[i]);
}
output:
Enter the number of equations2
Enter the coefficients of the equations a[1][1]=2
a[1][2]=4

b[1]=-1
a[2][1]=3
a[2][2]=1
b[2]=2
The result is
x[1]=0.90
x[2]=-0.70

2; /*A Program in C to find the approximate solution of


an ordinary differential equation by using Runge-Kutta(4th order) method */
#include<stdio.h>
#include<math.h>
float f(float x,float y){
return (x*x+y*y*x);
}
main(){
int i,n;
float x0,y0,x,y,k1,k2,k3,k4,k,h;
printf("Enter the values of x0,y0,h and x respectively\n");
scanf("%f%f%f%f",&x0,&y0,&h,&x);
n=(x-x0)/h;
x=x0;
y=y0;
for(i=0;i<n;i++){
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+2*(k2+k3)+k4)/6;
x=x+h;
y=y+k;
}
printf("The required solution is %f",y);
}
output:
Enter the values of x0,y0,h and x respectively

0
0
0.1
0.6
The required solution is 0.072234

3; //A Program in C to find the value of ln(1+x)


#include<stdio.h>
#include<math.h>
main(){
int c;
float x,sum=0,term;
printf("Enter the value of x\n");
scanf("%f",&x);
for(c=1;;c++){
term=pow(x,c)/(float)c;
if(term>0.000001)
sum+=pow(-1,c-1)*term;
else
break;
}
printf("The value of ln(1+x) for the given value of x \n");
printf("correct upto 4 decimal places is %1.4f",sum);
}

Output:
Enter the value of x
0.5
The value of ln(1+x) for the given value of x
correct upto 4 decimal places is 0.4055

4; //A Program in C to find the value of e^x correct upto 4 decimal places
#include<stdio.h>
#include<math.h>
main(){
int factorial(int);
int i=1,n;
float x,sum=1.0,term;
printf("Enter the value of x \n");
scanf("%f",&x);
for(i=1;;i++){
term=(pow(x,i)/factorial(i));

if(term>0.000001)
sum=sum+term;
else
break;
}
printf("The value of exp(x) for x=%f correct upto 4decimal places is %1.4f",x,sum);
}
int factorial(int n){
int c,fact=1;
for(c=1;c<=n;c++)
fact=fact*c;
return fact;
}
output:
enter the value of x
value of exp(x) for x=0.500000 correct upto 4decimal places is 1.6487

5;//A Program in C to find the value of e^(-x)correct upto 4 decimal places


#include<stdio.h>
#include<math.h>
main(){
int factorial(int);

int i=1,n;
float x,sum=1.0,term;
printf("Enter the value of x \n");
scanf("%f",&x);
for(i=1;;i++){
term=(pow(-x,i)/factorial(i));
if(fabs(term)>0.000001)
sum=sum+term;
else
break;
}
printf("The value of exp(-x) for x=%f correct upto 4 decimal places is %1.4f",x,sum);
}
int factorial(int n){
int c,fact=1;
for(c=1;c<=n;c++)
fact=fact*c;
return fact;
}
output:
the value of x
alue of exp(-x) for x=0.800000 correct upto 4 decimal places is 0.4493

6; /*A program in C to find the area of a triangle when its three sides are given*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float a,b,c,s,s1,s2,s3,area;
clrscr();
printf("\n Enter the sides of the triangle:");
scanf("%f%f%f",&a,&b,&c);
s1=(a+b-c);
s2=(a+c-b);
s3=(b+c-a);
if( s1>0 && s2>0 && s3>0)
{
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\n the area of the triangle :%f",area);
}
else
{
printf("\n the triangle is not possible");
}
getch();
}
output:
Enter the sides of the triangle:12
22
11
the area of the triangle :36.856987

7; /*Write a program in c to find the Multiplication of two matrices*/


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,m,n,o,p;
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix multiplication is not possible");
printf("\ncolumn of first matrix must be same as row of second
matrix");
}
else
{
printf("Enter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("The First matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++)

{
printf("\n");
for(j=0;j<p;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nThe multiplication of two matrices is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
printf("%d\t",c[i][j]);
}
getch();
}
output:
Enter the First matrix->1
2
3
5
6
45
Enter the Second matrix->8
5
6
9
7
1
The First matrix is
1
2
3
5
6
45
The Second matrix is

8
5
6
9
7
1
The multiplication of two matrices is
41
391

26
12

8; /* write a program in c to Sort the numbers in ascending order*/


#include<stdio.h>
#include<conio.h>
void main()
{
inti,j,s,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Now type the numbers: ");
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<=s-2;i++)
for(j=i+1;j<=s-1;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];

a[j]=temp;
}
printf("After sorting, the numbers are in ascending order:\n ");
for(i=0;i<s;i++)
printf(" %d\n",a[i]);
getch();
}
output:
Enter total elements: 4
Now type the numbers: 12
34
56
87
After sorting, the numbers are in ascending order:
12
34
56
87

9; /*A Program in C to find the approximate value of a given definite integral


by using Trapezoidal rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float funct(float a)
{
return ((4*a)-(3*a*a));

}
void main()
{
float a,b,h,sum,x0,xn,xi;
inti,n;
clrscr();
printf("Enter the lower and upper limit of the integration");
scanf("%f%f",&a,&b);
printf("Enter the number of intervals");
scanf("%d",&n);
h=(b-a)/n;
x0=a;
xn=b;
sum=funct(x0)+funct(xn);
for(i=1;i<n-1;i++)
{
xi=a+(i*h);
sum=sum+funct(xi)*2.0;
}
sum=(h/2.0)*sum;
printf("\n\n the required value of the integral is %5.3f",sum);
getch();
}
output:
Enter the lower and upper limit of the integration0
1
Enter the number of intervals10
the required value of the integral is 0.878

10; / *A Program in C to find the approximate value of a given definite integral


by usig Simpson's one-third rule*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float funct(float a)
{
return ((4*a)-(3*a*a));
}
void main()
{
float a,b,h,sum,x0,xn,xi,s0,s1=0,s2=0;
inti,n;
clrscr();
printf("Enter the lower and upper limit of the integration");
scanf("%f%f",&a,&b);
printf("Enter the number of intervals");
scanf("%d",&n);
h=(b-a)/n;
x0=a;
xn=b;
s0=funct(x0)+funct(xn);
for(i=1;i<n;i=i+2)
{
xi=a+(i*h);
s1=s1+funct(xi)*4.0;
}
for(i=2;i<n;i=i+2)
{
xi=a+(i*h);
s2=s2+funct(xi)*2.0;
}
sum=(s0+s1+s2)*h/3;
printf("\n\n the required value of the integral is %5.3f",sum);
getch();
}
output:
Enter the lower and upper limit of the integration0
1
Enter the number of intervals10
the required value of the integral is 1.000

11; //A Program in C to find the inverse of a 3*3 matrix


#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
float determinant=0;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3]-a[1][(i+2)%3]*a[2]
[(i+1)%3]));
printf("\nInverse of matrix is: \n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
printf("\n");
}
getch();
}
Output:
Enter the 9 elements of matrix:

5
2
8
36
7
9
4
12
The matrix is
4
5
2
8
36
7
9
4
12
Inverse of matrix is:
0.47 -0.04 -0.34
-0.06 0.03 0.03
-0.04 -0.01 0.12
/*Write a program in c to find the Sum of first n natural numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
int sum=0;
printf("Enter the n i.e. max values of series: ");
scanf("%d",&n);
sum = (n * (n + 1)) / 2;
printf("Sum of the series: ");
for(i =1;i <= n;i++)
{
if (i!=n)
printf("%d + ",i);
else
printf("%d= %d",i,sum);
}
getch();
}
output:
Enter the n i.e. max values of series: 6
Sum of the series: 1 + 2 + 3 + 4 + 5 + 6= 21

12; /*Write a program in c to find the sum of two matrices*/


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n;
printf("enter the matrix order");
scanf("%d%d",&m,&n);
printf("enter the a matrix element\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n");
}

printf("enter the b matrix element\n");


for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&b[i][j]);
printf("\n");
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("the matrix A is: \n");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("the matrix B is: \n ");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=n;j++)
printf("%d\t",b[i][j]) ;
printf(" \n");
}
printf("\nThe Addition of two matrices is\n");
for(i=1;i<=m;i++)
{
printf("\n");
for(j=1;j<=n;j++)
printf("%d\t",c[i][j]);
printf(" \n");
}
getch();
}
output:
enter the b matrix element
2
5
1
9
the matrix A is:

8
6
the matrix B is:
2
5
1
9
The Addition of two matrices is
9
14
9

15

13; //A Program in C to find the value of cos x


#include<stdio.h>
#include<math.h>
main(){
int factorial(int);
float x,sum=1,term;
int c,i=1;
printf("Enter the value of x in degrees:\n");
scanf("%f",&x);
x=x*3.141592654/180;
for(c=2;;){
term=pow(x,c)/
(float)factorial(c);
if(term>0.000001){
c=c+2;
sum+=pow(1,i)*term;
i=i+1;
}
else
break;
}
printf("The value of cos x for the given value of
x\n");
printf("correct upto 4 decimal places is
%1.4f",sum);
}
int factorial(int n){
int fact=1;
int c;
for(c=1;c<=n;c++)
fact=fact*c;
return fact;
}

output:
Enter the value of x in degrees:
30
The value of cos x for the given value of x
correct upto 4 decimal places is 0.8660

14; //A Program in C to find the value of sin x


#include<stdio.h>
#include<math.h>
main(){
int factorial(int);
float x,sum=0,term;
int c,i=0;
printf("Enter the value of x in degrees:\n");
scanf("%f",&x);
x=x*3.141592654/180;
for(c=1;;){
term=pow(x,c)/
(float)factorial(c);
if(term>0.000001){
c=c+2;
sum+=pow(1,i)*term;
i=i+1;
}
else
break;
}
printf("The value of sin x for the given value of
x\n");
printf("correct upto 4 decimal places is
%1.4f",sum);
}

int factorial(int n){


int fact=1;
int c;
for(c=1;c<=n;c++)
fact=fact*c;
return fact;
}
output:
Enter the value of x in degrees:
30
The value of sin x for the given value of x
correct upto 4 decimal places is 0.5000

15; // A Program in C to find the roots of a quadratic equation with real coefficients
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float a,b,c,rp,ip;
float d,root1,root2,r;
printf("Enter a, b and c of quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(d > 0)
{
printf("Roots are real numbers.\n");
root1 = ( -b + sqrt(d)) / (2* a);

root2 = ( -b - sqrt(d)) / (2* a);


printf("the real roots are:%5.3f= , %5.3f=",root1,root2);
}
else if(d<0)
{
printf("Roots are complex number and they are\n");
rp=-b/(2*a);
ip=sqrt(-d)/(2*a);
printf("r1=%5.3f+%5.3f\n",rp,ip);
printf("r2=%5.3f-%5.3f\n",rp,ip);
}
else
{
r=-b/(2*a);
printf("Roots are real and equal and are %f and %f",r,r);
}
getch();
}
output:
Enter a, b and c of quadratic equation: 9
2
7
Roots are complex number and they are
r1=-0.111+0.875
r2=-0.111-0.875

16; /*A Program in C to find the solution of x3-8x-4=0 by using Newton Raphson
method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (pow(x,3)-8*x-4);
}

float f1(float x)
{
return (3*pow(x,2)-8);
}
void main()
{
float x,x1;
printf("enter the value of x");
scanf("%f",&x);
do
{
x1=x;
x=x-(f(x)/f1(x));
}
while(fabs(x-x1)>0.00001);
printf("%f",x);
getch();
}
output:
enter the value of x3
3.051374

Anda mungkin juga menyukai