Skip to main content

Posts

Showing posts from June, 2018

To check matrix commutation between two matrices

/* Matrix Commutation test */ #include <stdio.h> #include <math.h> #include <stdlib.h> int main() { int i,j,n,m,p,k,a[20][20],b[20][20],e[20][20],g[20][20], flag=1; printf( "Enter the order of the matrix A:\n" ); scanf( "%d%d" , &m ,&n); printf( "Enter the order of the matrix B:\n" ); scanf( "%d%d" , &n, &p); printf( "Enter the elements of the matrix A:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++) scanf( "%d" , &a[i][j]); printf( "\n" ); } printf( "Enter the elements of the matrix B:\n" ); for (i=0;i<n;i++) { for (j=0;j<p;j++) scanf( "%d" , &b[i][j]); printf( "\n" ); } /*matrix multiplication AB */ for (i=0;i<m;i++) { for (j=0;j<p;j++) { e[i][j]=0; for (k=0;k<n;k++) e[i][j]=e[i][j]+a[i][k]*b[k][j]; } } printf( "Product matrix AB

Integration of a function by Monte carlo random dot method

/* Monte carlo random dot method */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define a 1 #define b 2 #define cycle 5000 int main() { int counter=0, r1, r2, ymax, i, y; float f, A; ymax=3*2*2; for (i=0;i<cycle;i++) { r1=a+rand()%ymax; y=3*r1*r2; r2=rand()%ymax; if (y>=r2) counter++; } f=( float )counter/( float )cycle; A=f*( float )ymax*(( float )b-( float )a); printf( "The required integration is %f" , A); return (0); }

To find the area of a unit circle by Monte Carlo random dot method

/* To find the area of a unit circle by Monte Carlo random dot method */ #include <stdio.h> #include <math.h> int main() { float x, y, r, sqr, ratio; float xmax=2, ymax=2, h=0.0005, k=0.0005, area, rad=1; float cnt_tot=0, cnt_acc=0, cnt_rej=0; for (x=0;x<=ymax;x+=h) { for (y=0;y<=ymax;y+=k) { sqr=(x-1)*(x-1)+(y-1)*(y-1); r=sqrt(sqr); if (r<=rad) { cnt_acc+=1; } else { cnt_rej+=1; } } } cnt_tot=cnt_acc+cnt_rej; printf( "Total random number of dots generated=%f" , cnt_tot); ratio=cnt_acc/cnt_tot; printf( "\nRatio=%f" , ratio); area=ratio*ymax*xmax; printf( "\nTotal area=%f" , ymax*xmax); printf( "\nArea of the circle = (Ratio) X (Total area)" ); printf( "\nArea of the circle = %f" ,area); return (0); }

To Determine Multiplication of two matices

/* Multiplication of two matices */ #include <stdio.h> #include <math.h> int main() { int i, j, n, m, p, k, a[20] [20], b[20] [20], e[20] [20]; printf( "Enter order of the matrix A(mxn):\n" ); scanf( "%d%d" , &m, &n); printf( "Enter order of the matrix B(nxp):\n" ); scanf( "%d%d" , &n, &p); 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( "\n" ); }     printf( "Entered Matrix A:\n" );     for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10d" ,a[i] [j]); printf( "\n" ); } printf( "\nEnter the elements of matrix B:\n" ); for (i=0;i<n;i++) {     for (j=0;j<p;j++) scanf( "%d" , &b[i] [j]); printf( "\n" ); } printf( "Entered Matrix B:\n" );     for (i=0;i

To determine Trace of a matrix

/* Trace of a matrix */ #include <stdio.h> #include <math.h> int main() { int i, j, n, m,  a[20] [20], sum=0; printf( "Enter the order of matrix (mxn):\n" ); scanf( "%d%d" , &m, &n); printf( "Enter the elements of the matrix:\n" ); for (i=0;i<m;i++) { for (j=0;j<n;j++)        { scanf( "%d" , &a[i] [j]);        } } printf( "your matrix:\n" ); for (i=0;i<m;i++) {     for (j=0;j<n;j++)     { printf( "%20d" ,a[i][j]); } printf( "\n" ); } for (i=0;i<m;i++) { for (j=0;j<n;j++) { if (i==j) sum=sum+a[i][j];         }     }     printf( "\n\ntrace of the matrix=%d\n" ,sum); return (0); }

To Determine Transpose of a given matrix

/* Transpose of a matrix */ #include <stdio.h> #include <math.h> int main() { int matrix[10][10], transpose[10][10]; int m, n, c, r; printf( "Enter the number of rows and columns of matrix:\n" ); scanf( "%d%d" , &m, &n); printf( "Enter the %d elements of matrix:\n" , m*n); for (r=0;r<m;r++) { for (c=0;c<n;c++) { printf( "\nEnter value in %dth row and %dth column:" , r+1, c+1); scanf( "%d" , &matrix[r][c]); } } printf( "your matrix:\n" ); for (r=0;r<m;r++) { for (c=0;c<n;c++) { printf( "%10d" , matrix[r][c]); } printf( "\n\n" ); } for (r=0;r<m;r++) { for (c=0;c<n;c++) { transpose[c][r]=matrix[r][c]; } } printf( "Transpose of entered matrix :\n" ); for (r=0;r<n;r++) { for (c=0;c<m;c++) { printf( "%10d" , transpose[r][c]); } printf( &

To Perform Addition and subtraction of two matrices

/* Addition and subtraction of two matrices */ #include <stdio.h> #include <math.h> int main() { int i, j, n, m, a[20] [20], b[20] [20], c[20] [20], d[20] [20]; printf( "Enter order of the matrices:\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( "\n" ); }     printf( "Entered Matrix A:\n" );     for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10d" ,a[i] [j]); printf( "\n" ); } printf( "\nEnter the elements of matrix B:\n" ); for (i=0;i<m;i++) {     for (j=0;j<n;j++) scanf( "%d" , &b[i] [j]); printf( "\n" ); } printf( "Entered Matrix B:\n" );     for (i=0;i<m;i++) { for (j=0;j<n;j++) printf( "%10d" ,b[i] [j]); printf( &quo

To integrate a function by Simpson's 1/3 Rule

/*To integrate a fn. by Simpson's 1/3 Rule*/ #include <stdio.h> #include <math.h> /*Define your function here*/ float f( float x) { float d; d=x/1+x; return (d); } int main () { /*denoting a and b as integral lower and upper limits respectively*/ float a, b, h, s; int i, n; printf( "Input values of a, b and no. of steps:\n" ); scanf( "%f%f%d" ,&a,&b,&n); h=(b-a)/n; s=f(a)+f(b)+4*f(a+h); for (i=3;i<=n-1;i=i+2) { s=s+4*f(a+i*h)+2*f(a+(i-1)*h); } s=(h/3)*s; printf( "\nValue of the integral =%f" ,s); return (0); }

Integration of a function by Trapezoidal method having finite limits

/* Integration of a function by Trapezoidal rule */ #include <stdio.h> #include <math.h> /*define your function here*/ float y( float x) { return x/1+x; } int main() { float a, b, h, s; int i, n; printf( "Enter a, b and no. of subintervals:\n" ); scanf( "%f%f%d" , &a, &b, &n); h=(b-a)/n; s=y(a) + y(b); for (i=0;i<n;i++) { s=2*y(a+i*h); } printf( "Value of integral is %f" ,(h/2)*s); return (0); }

To calculate the Mean, Median and Mode of a set of given numbers

/*Mean, Median and Mode calculation*/ #include <stdio.h> #include <math.h> int main() { int i,j,n; float a[100],t,sum=0.0,mean,median,mode; printf( "Number of data:" ); scanf( "%d" ,&n); printf( "Type the input data:\n" ); for (i=0;i<n;i++) { scanf( "%f" ,&a[i]); sum=sum+a[i]; } mean=sum/n; printf( "Mean=%f\n" ,mean); /*sorting of the array*/ for (j=0;j<n;j++) { for (i=0;i<n;i++) { if (a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } if (n%2==0) median=(a[n/2]+a[(n-2)/2])/2; else median=a[(n-1)/2]; mode=3*median-2*mean; printf( "Median=%f\n" ,median); printf( "Mode=%f\n" ,mode); return (0); }

To find square root of a real number by Newton-Raphson method

/* Solution of an algebraic equation x*x-n=0 by Newton-Raphson method */ #include <stdio.h> #include <math.h> int main() { int count=0; float x0, xn, fx, dfx, n; printf( "Enter the number whose sq. root is required:" ); scanf( "%f" , &n); printf( "Initial guess value x0:" ); scanf( "%f" , &x0); /* Iteraion begins */ label: fx=x0*x0-n; dfx=2*x0; xn=x0-fx/dfx; if (fabs(xn-x0)<0.0001) printf( "The square root of %f is:%f" , n, xn); else { x0=xn; count=count+1; if (count>100) printf( "Solution does not converge" ); else goto label; } return (0); }

Solution of an algebraic equation by Newton-Raphson method

/* Solution of an algebraic equation x^5+3x^2-10=0 by Newton-Raphson method */ #include<stdio.h> #include<math.h> int main() {     int count=0;     float x0, xn, fx, dfx;     printf("Initial guess value x0:");     scanf("%f", &x0);     /* Iteraion begins */     label:         fx=pow(x0,5)+3*x0*x0-10;         dfx=5*pow(x0,4)+6*x0;         xn=x0-fx/dfx;         if(fabs(xn-x0)<0.0001)         printf("The final root is:%f\n",xn);         else         {             x0=xn;             count=count+1;             if(count>100)             printf("Solution does not converge,choose another guess value.\n");             else             goto label;         }         return(0); }

Least Square Fitting of several data points as a straight line

/* Least Square Fit */ #include<stdio.h> #include<math.h> int main() {     int i, n;     float m, c, X[50], Y[50], Sx=0, Sy=0, Sxy=0, Sx2=0;     printf("Enter the no. of input data=");     scanf("%d", &n);     printf("\n Enter the values of x:\n");     for (i=0;i<n;i++)     {         scanf("%f", &X[i]);     }     printf("\n Enter the values of y:\n");     for (i=0;i<n;i++)     {     scanf("%f", &Y[i]);     }     for (i=0;i<n;i++)     {         Sx=Sx+X[i];         Sy=Sy+Y[i];         Sx2=X[i]*X[i];         Sxy=Sxy+X[i]*Y[i];         }     m=(n*Sxy-Sx*Sy)/(n*Sx2-Sx*Sx);     c=(Sx2*Sy-Sx*Sxy)/(n*Sx2-Sx*Sx);     printf("\n The line of best fit is : y=%fx+%f\n", m,c);     return(0); }

To arrange a series of numbers in ascending order by selection sorting scheme

/*Selection Sort*/ #include<stdio.h> #include<math.h> int main() {     int n,i,j,position,swap;     float a[100];     printf("enter no. of elements:");     scanf("%d",&n);     printf("enter %d numbers:\n",n);     for(i=0;i<n;i++)     scanf("%f",&a[i]);     for(i=0;i<n-1;i++)     {         position=i;         for(j=i+1;j<n;j++)         {             if(a[position]>a[j])             position=j;         }         if(position!=i)         {             swap=a[i];             a[i]=a[position];             a[position]=swap;         }     }     printf("sorted list in ascending order is:\n");     for(i=0;i<n;i++)     printf("%f\n",a[i]);     return(0); }

C Code for Solution of a quadratic equation

/*Solution of a quadratic equation*/ #include<math.h> #include<stdio.h> int main() {     float a, b, c, d, x1, x2, rp, imp; /*consider the quadratic equation as ax^2+bx+c=0*/     printf("Enter the values of a:");     scanf("%f", &a);     printf("Enter the values of b:");     scanf("%f", &b);     printf("Enter the values of c:");     scanf("%f", &c);     d=b*b-4*a*c;     printf("\n The value of discriminant is : %f",d);     if(d>=0)     {         printf("\n The root is real");         x1=(-b+sqrt(d))/(2*a);         x2=(-b-sqrt(d))/(2*a);         printf("\n The 1st root is: %f", x1);         printf("\n The 2nd root is: %f\n", x2);     }     else     {         printf("\n The root is complex");         rp=-b/(2*a);         imp=sqrt(abs(d))/(2*a);         printf("\n The 1st root is: %f+%fi", rp,imp);         printf("\n The 2nd root is: %f-%

To arrange a series of numbers in descending order

/* To arrange the series in descending order */ #include<stdio.h> #include<math.h> int main() {     int i, j, n;     float A[50], t;     printf("Input the no. of data:");     scanf("%d",&n);     printf("\nEnter the data:\n");     for(i=0;i<n;i++)     scanf("%f",&A[i]);     for(i=0;i<n;i++)     {         for(j=0;j<n-1;j++)         {             if(A[i]>A[j])             {                 t=A[i];                 A[i]=A[j];                 A[j]=t;             }         }     }     printf("\nSorted array in descending order is:\n");     for(i=0;i<n;i++)     printf(" %f\n",A[i]);     return(0); }

To arrange a series of numbers in ascending order

/* To arrange the series in ascending order */ #include<stdio.h> #include<math.h> int main() {     int i, j, n;     float A[50], t;     printf("Input the no. of data:");     scanf("%d",&n);     printf("\nEnter the data:\n");     for(i=0;i<n;i++)     scanf("%f",&A[i]);     for(i=0;i<n;i++)     {         for(j=0;j<n-1;j++)         {             if(A[i]<A[j])             {                 t=A[i];                 A[i]=A[j];                 A[j]=t;             }         }     }     printf("\nSorted array in ascending order is:\n");     for(i=0;i<n;i++)     printf(" %f\n",A[i]);     return(0); }

To evaluate (1+x)^m series by counting upto n terms

/*To expand (1+x)^m series*/ #include<stdio.h> #include<math.h> int main() {     float sum, term, x;     int n, i, m;     printf("Enter the values of x & No. of countable terms(n):\n");     scanf("%f %d", &x, &n);     printf("Enter the power(m):");     scanf("%d", &m);     sum=1+m*x;         term=m*x;            for(i=1;i<=(n-1);i++)     {         term =term*x*(m-i)/(i+1);         sum=sum+term;     }     printf("\n(1+(%f))^%d=%f \n", x, m, sum);     return(0); }

To evaluate (1-x)^m series by counting upto n terms

/*To expand (1-x)^m series*/ #include<stdio.h> #include<math.h> int main() {     float sum, term, x;     int n, i, m;     printf("Enter the values of x & No. of countable terms(n):\n");     scanf("%f %d", &x, &n);     printf("Enter the power(m):");     scanf("%d", &m);     sum=1-m*x;     term=-m*x;            for(i=1;i<=(n-1);i++)     {         term =-term*x*(m-i)/(i+1);         sum=sum+term;     }     printf("\n(1-(%f))^%d=%f \n", x, m, sum);     return(0); }

To evaluate the value of e^-x upto a predefined accuracy

/* To evaluate the value of e^-x */ #include<stdio.h> #include<math.h> int main() {     float  sum, t;     int i, x;     printf("Value of x:\n");     scanf("%d", &x);     sum=1.0;     t=1;     i=0;     label:         i=i+1;         t=-t*x/i; /*Setting a predefined accuracy=0.00001*/         if(fabs(t)>0.00001)     {         sum=sum+t;         goto label;     }     else     printf("e^-%d=%f\n", x, sum);     return(0); }

To evaluate the value of e^x upto a given accuracy

/* To evaluate the value of e^x */ #include<stdio.h> #include<math.h> int main() {     float  sum, t;     int i,x;     printf("Value of x:\n");     scanf("%d", &x);     sum=1.0;     t=1;     i=0;     label:         i=i+1;         t=t*x/i; /*Setting a predefined accuracy=0.00001*/         if(fabs(t)>0.00001)     {         sum=sum+t;         goto label;     }     else     printf("e^%d=%f\n", x, sum);     return(0); }

To evaluate the value of Cosx upto n terms in its series expansion

/* To evaluate the value of Cosx */ #include<stdio.h> #include<math.h> int main() {     int i, n;     float x, sum, t, x1; /*n=counting upto n terms in expansion of cosx series*/     printf("Enter the value of x in degree & n:\n");     scanf("%f%d", &x, &n);         x1=x; /*To convert the angle from degree to Radian*/     x=x*3.14159/180;     sum=1;     t=1;     for(i=1;i<=(n-1);i++)     {         t=-t*x*x/((2*i-1)*2*i);         sum=sum+t;     }     printf("Cos(%f)=%f\n",x1,sum);     return(0); }

How to use if-else statement in c programming

 In the program below, we will learn about how the if-else statement works. I wanted to calculate 4th power of a given number if and only if it is greater than 2, else if the value of given number lowers than and equal to 2, it always shows result by adding up with 3.         /*To verify the if else statement */         #include<stdio.h>         #include<math.h>         int main()         {             float a, b;             printf("Enter the value of a:");             scanf("%f", &a);             if (a>2)             b=pow(a,4);             else b=a+3;             printf("\n The calculated value is: %f\n", b);             return(0);         }

How to use if statement in c programming

In the program below, we will learn about how the if statement works. I wanted to calculate square root value of a given number if and only if it is greater than equal to zero. If, however, the value of given number lowers than zero, it always print a zero.            /*To verify the if statement */         #include<stdio.h>         #include<math.h>         int main()         {             float a, b;             printf("Enter the value of a:");             scanf("%f", &a);             if (a>=0)             b=sqrt(a);             printf("\n The square root of %f is: %f\n", a,b);             return(0);         }

To find area of the triangle by knowing Base and Height

/*To find area of the triangle*/ #include<math.h> #include<stdio.h> int main() {     int a, b;     float area;     printf("Enter the value of height: ");     scanf("%d", &a);     printf("Enter the value of base: ");     scanf("%d", &b);     area=.5*a*b;     printf("\n The area of the triangle is: %f\n",area);     return(0); }

To find the value of Sine function for specific value of angle in degree

/*Sum of infinite series with specified accuracy*/ #include<stdio.h> #include<math.h> int main() {     int i;     float x, sum, t, x1;     printf("Value of x in degrees:");     scanf("%f",&x);     x1=x;     x=x*3.14159/180;     sum=x;     t=x;     i=1;     label: i=i+2;     t=(-t)*x*x/(i*(i-1));     if (fabs (t)>0.00001)     {         sum=sum+t;         goto label;     }     else     printf("sin(%f)=%f\n",x1,sum);     return(0); }

Sorting of n numbers in ascending order in bubble sorting scheme

/* Bubble Sort */ #include<stdio.h> #include<math.h> int main() {     float a[100], temp;     int i, j, n;     printf("Enter the # of elements are to be sorted:");     scanf("%d", &n);     printf("---: Put value of the elements :---\n");     for(i=0;i<n;i++)     {         scanf("%f", &a[i]);     }     for(i=0;i<n;i++)     {         for(j=0;j<n-i-1;j++)         {             if(a[j]>a[j+1])             {                 temp=a[j];                 a[j]=a[j+1];                 a[j+1]=temp;             }         }     }     printf("----: The sorted list in ascending order is :----\n");     for(i=0;i<n;i++)     {         printf("%f\n", a[i]);     }     return(0); }

To find angle between two vectors in degree

/* To find angle between two vectors */ #include<stdio.h> #include<math.h> int main() {     float ax, ay, az, bx, by, bz, AdotB, ModA, ModB, ModAModB, AngleAB;     printf("Enter the values of ax, ay, az:\n");     scanf("%f%f%f", &ax, &ay, &az);     printf("A=(%f)i+(%f)j+(%f)k", ax, ay, az);     printf("\nEnter the values of bx, by, bz:\n");     scanf("%f%f%f", &bx, &by, &bz);     printf("B=(%f)i+(%f)j+(%f)k", bx, by,bz);     AdotB=ax*bx+ay*by+az*bz;     printf("\nA.B=%f", AdotB);     ModA=sqrt(ax*ax+ay*ay+az*az);     ModB=sqrt(bx*bx+by*by+bz*bz);     ModAModB=ModA*ModB;     AngleAB=AdotB/ModAModB;     AngleAB=AngleAB*180/3.14159;     printf("\nAngle between A and B in degree is  Cos/_A,B=%f\n", AngleAB);     return(0); }

To determine dot product, cross product and resultant of two vectors

/* To determine dot product, cross product and resultant of two vectors */ #include<stdio.h> #include<math.h> int main() {     float a1, a2, a3, b1, b2, b3, AdotB, AXBx, AXBy, AXBz, AplusBx, AplusBy, AplusBz;     printf("Enter the 1st vector components:\n");     scanf("%f%f%f", &a1, &a2, &a3);     printf("Enter the 2nd vector components:\n");     scanf("%f%f%f", &b1, &b2, &b3);     AdotB=a1*b1+a2*b2+a3*b3;     AXBx=(a2*b3-a3*b2);     AXBy=(a3*b1-a1*b3);     AXBz=(a1*b2-a2*b1);     AplusBx=(a1+b1);     AplusBy=(a2+b2);     AplusBz=(a3+b3);     printf("The dot product of the vectors is: A.B=%f", AdotB);     printf("\nThe cross product of the vectors is: AXB=%fi+%fj+%fk", AXBx, AXBy, AXBz);     printf("\nThe resultant of the vectors is: A+B=%fi+%fj+%fk\n", AplusBx, AplusBy, AplusBz);     return(0); }

Sum of GP series š¯˛¢ar^i , i=0,1,2....n

/* Sum of GP series term by term */ #include<stdio.h> #include<math.h> int main() {     float a, r, sum=0;     int n, i;     printf("Enter the values of a, r, n:\n");     scanf("%f%f%d", &a, &r, &n);     for(i=0;i<=n;i++)     {         sum=sum+a*pow(r,i);     }     printf("Sum of GP series:%f\n", sum);     return(0); }

Factorial of a integer number in C

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example: 5! = 5*4*3*2*1 = 120   3! = 3*2*1 = 6   Here, 5! is pronounced as "5 factorial", it is also called "5 bang" or "5 shriek". The factorial is normally used in Combinations and Permutations (mathematics).  /* To find factorial of a integer */ #include<math.h> #include<stdio.h> int main() {     int n, i, fact=1;     printf("Enter the No.:");     scanf("%d", &n);     for (i=1;i<=n;i++)     fact=fact*i;     printf("\n The factorial of %d is : %d\n", n,fact);     return(0); }

C program to compare two numbers using the if-else C syntax for greater than, less than or equal

Comparing two integer variables is one of the simplest programs you can write at ease. In this program, you can either take input from user using  scanf()  function or statically define in the program itself. We expect it to be a simple program for you as well. We are just comparing two integer variables.  /*To evaluate greater or lesser numbers*/ #include<math.h> #include<stdio.h> int main() {     float x, y;     printf("Enter the value of x:");     scanf("%f", &x);     printf("Enter the value of y:");     scanf("%f", &y);     if(x==y)     printed("x is equal to y\n");     elseif(x>y)     printf("x is greater than y\n");     else     printf("y is greater than x\n");     return(0); } Output: