Posts

Showing posts from June, 2014

C program for subtraction of two matrices.

source code #include<stdio.h> int main(){   int a[3][3],b[3][3],c[3][3],i,j;   printf("Enter the First matrix->");   for(i=0;i<3;i++)       for(j=0;j<3;j++)            scanf("%d",&a[i][j]);   printf("\nEnter the Second matrix->");   for(i=0;i<3;i++)       for(j=0;j<3;j++)            scanf("%d",&b[i][j]);   printf("\nThe First matrix is\n");   for(i=0;i<3;i++){       printf("\n");       for(j=0;j<3;j++)            printf("%d\t",a[i][j]);   }   printf("\nThe Second matrix is\n");   for(i=0;i<3;i++){       printf("\n");       for(j=0;j<3;j++)       printf("%d\t",b[i][j]);    }    for(i=0;i<3;i++)        for(j=0;j<3;j++)             c[i][j]=a[i][j]-b[i][j];    printf("\nThe Subtraction of two matrix is\n");    for(i=0;i<3;i++){        printf("\n");        

C program for multiplication of two matrices.

source code #include<stdio.h> int main() {   int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,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 mutiplication is not possible");       printf("\nColumn of first matrix must be same as row of second matrix");   }   else{       printf("\nEnter the First matrix->");       for(i=0;i<m;i++)       for(j=0;j<n;j++)            scanf("%d",&a[i][j]);       printf("\nEnter the Second matrix->");       for(i=0;i<o;i++)       for(j=0;j<p;j++)            scanf("%d",&b[i][j]);       printf("\nThe First matrix is\n");       for(i=0;i<m;i++) {       printf("\n");       for(j=0;j<n;j++

C program to find out sum of diagonal element of a matrix.

source code #include<stdio.h> int main() {   int a[10][10],i,j,sum=0,m,n;   printf("\nEnter the row and column of matrix: ");   scanf("%d %d",&m,&n);   printf("\nEnter the elements of matrix: ");   for(i=0;i<m;i++)       for(j=0;j<n;j++)            scanf("%d",&a[i][j]);   printf("\nThe matrix is\n"); for(i=0;i<m;i++) {       printf("\n");       for(j=0;j<m;j++) {       printf("%d\t",a[i][j]);       }  }  for(i=0;i<m;i++) {      for(j=0;j<n;j++) {           if(i==j)               sum=sum+a[i][j];      }  }  printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);   getch();  return 0; }

C program to find out transport of a matrix.

source code #include<stdio.h> int main() {   int a[10][10],b[10][10],i,j,k=0,m,n;   printf("\nEnter the row and column of matrix");   scanf("%d %d",&m,&n);   printf("\nEnter the First matrix->");   for(i=0;i<m;i++)       for(j=0;j<n;j++)            scanf("%d",&a[i][j]);   printf("\nThe matrix is\n");   for(i=0;i<m;i++) {       printf("\n");       for(j=0;j<m;j++) {            printf("%d\t",a[i][j]);       }   }   for(i=0;i<m;i++)       for(j=0;j<n;j++)            b[i][j]=0;   for(i=0;i<m;i++) {       for(j=0;j<n;j++) {            b[i][j]=a[j][i];            printf("\n%d",b[i][j]);       }   }   printf("\n\nTraspose of a matrix is -> ");   for(i=0;i<m;i++) {       printf("\n");       for(j=0;j<m;j++) {            printf("%d\t",b[i][j]);       }

C program to find inverse of a matrix.

source code #include<stdio.h> int 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();    return 0; }

C Program to print Lower triangular matrix.

source code #include<stdio.h> int 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]);   }    printf("\nSetting zero in upper triangular matrix\n");    for(i=0;i<3;i++) {       printf("\n");       for(j=0;j<3;j++)            if(i<=j)              printf("%d\t",a[i][j]);            else              printf("%d\t",0);   }   getch();   return 0; }

C Program to print Upper triangular matrix.

source code #include<stdio.h> int 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]);   }    printf("\nSetting zero in upper triangular matrix\n");    for(i=0;i<3;i++){       printf("\n");       for(j=0;j<3;j++)            if(i>=j)              printf("%d\t",a[i][j]);            else              printf("%d\t",0);   }   getch();    return 0; }

C program to open a file and write some text and close its.

source code #include<stdio.h> int main() { FILE *fp; char ch; fp=fopen("file.txt","w"); printf("\nEnter data to be stored in to the file:"); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp); getch(); return 0; }

C program to copy a data of file to other file.

source code #include<stdio.h> int main() {   FILE *p,*q;   char file1[20],file2[20];   char ch;   printf("\nEnter the source file name to be copied:");   gets(file1);   p=fopen(file1,"r");   if(p==NULL){       printf("cannot open %s",file1);       exit(0);   }   printf("\nEnter the destination file name:");   gets(file2);   q=fopen(file2,"w");   if(q==NULL){       printf("cannot open %s",file2);       exit(0);   }   while((ch=getc(p))!=EOF)       putc(ch,q);   printf("\nCOMPLETED");   fclose(p);   fclose(q);   getch();  return 0;

C program which reads string from file.

source code #include<stdio.h> int main() { char str[70]; FILE *p; if((p=fopen("string.txt","r"))==NULL){ printf("\nUnable t open file string.txt"); exit(1); } while(fgets(str,70,p)!=NULL) puts(str); fclose(p); getch(); return 0; }

C program which writes array in the file.

source code #include<stdio.h> int main() {   FILE *p;   int i,a[10];   if((p=fopen("myfile.dat","wb"))==NULL) {       printf("\nUnable to open file myfile.dat");       exit(1);   }   printf("\nEnter ten values, one value on each line\n");   for(i=0;i<10;i++)   scanf("%d",&a[i]);   fwrite(a,sizeof(a),1,p);   fclose(p);  getch();   return 0; }

C program which concatenate two file and write it third file.

source code #include<stdio.h>  void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);  int main(int argc,char *argv[]) {    FILE *fp1,*fp2;    concatenate(fp1,fp2,argv,argc);    getch();    return 0;  } void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc){    int i,ch;    fp2=fopen("files","a");    for(i=1;i<argc-1;i++){       fp1=fopen(argv[i],"r");       while((ch=getc(fp1))!=EOF)       putc(ch,fp2);    }  } 

C program to find out size of any file.

source code #include <time.h> #include <sys\stat.h> #include <stdio.h> void main() {     struct stat status;     FILE *fp;     fp=fopen("test.txt","r");     fstat(fileno(fp),&status);     clrscr();     printf("Size of file : %d",status.st_size);     printf("Drive name   : %c",65+status.st_dev);     getch(); }

C program to know type of file

source code #include "time.h" #include "sys\stat.h" #include "stdio.h" void main() {     struct stat status;     FILE *fp;     stat("c:\\tc\\bin",&status);     clrscr();     if (status.st_mode & S_IFDIR)          printf("It is directory.\n");     if (status.st_mode & S_IFCHR)          printf("It is chracter file.");     if (status.st_mode & S_IFREG)          printf("It is reggular file.");     getch(); }

C program to know permission of any file.

source code #include "time.h" #include "sys\stat.h" #include "stdio.h" void main() {     struct stat status;     FILE *fp;     stat("test.txt",&status);     clrscr();     if (status.st_mode & S_IREAD)          printf("You have read permission.\n");     if (status.st_mode & S_IWRITE)          printf("You have write permission.");     getch(); }

C program to know last date of modification of any file.

source code #include "time.h" #include "sys\stat.h" #include "stdio.h" int main() {     struct stat status;     FILE *fp;     fp=fopen("test.txt","r");     fstat(fileno(fp),&status);       printf("Last date of modification : %s",ctime(&status.st_ctime));     return 0; }

C program to find size and drive of any file.

source code #include "time.h" #include "sys\stat.h" #include "stdio.h" int main() {     struct stat status;     FILE *fp;     fp=fopen("test.txt","r");     fstat(fileno(fp),&status);     printf("Size of file : %d",status.st_size);     printf("Drive name   : %c",65+status.st_dev);   getch();     return 0; }

C program for addition and subtraction of two complex numbers.

source code #include<stdio.h> int main() {   int a,b,c,d,x,y;   printf("\nEnter the first complex number:");   scanf("%d%d",&a,&b);   printf("\nEnter the second complex number:");   scanf("%d%d",&c,&d);   if(b<0)       printf("%d-i\n",a-b);   else       printf("d+i\n",a+b);   if(d<0)       printf("d-i\n",c-d);   else       printf("%d+i\n",c+d);   printf("\nADDITION ");   x=a+c;   y=b+d;   if(y>0)       printf("%d-i%d",x,-y);   else       printf("%d+i%d",x,+y);   printf("\n\nSUBTRACTION ");   x=a-c;   y=b-d;   if(y<0)       printf("%d-i%d",x,-y);   else       printf("%d+i%d",x,+y);   getch();

C program to find out the sum of series 1 + 2 + …. + n.

source code #include<stdio.h> int 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();     return 0; }

Sum of 1^2 + 2^2 + …. + n^2 series in c programming language

source code #include<stdio.h>    int 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 * n + 1 )) / 6;     printf("Sum of the series : ");     for(i =1;i<=n;i++){          if (i != n)              printf("%d^2 + ",i);          else              printf("%d^2 = %d ",i,sum);     }     getch();     return 0; }

C program to find out the sum of series 1^3 + 2^3 + …. + n^3.

source code #include<stdio.h> #include<math.h> int main() {     int n,i;     int sum=0;     printf("Enter the n i.e. max values of series: ");     scanf("%d",&n);     sum = pow(((n * (n + 1) ) / 2),2);    printf("Sum of the series : ");     for(i =1;i<=n;i++){          if (i != n)              printf("%d^3 + ",i);          else              printf("%d^3 = %d ",i,sum);     }    getch();     return 0; }

C program to find out the sum of given G.P

source code #include<math.h> int main() {     float a,r,i,tn;     int n;     float sum=0;     printf("Enter the first number of the G.P. series: ");     scanf("%f",&a);     printf("Enter the total numbers in the G.P. series: ");     scanf("%d",&n);     printf("Enter the common ratio of G.P. series: ");     scanf("%f",&r);     sum = (a*(1 - pow(r,n+1)))/(1-r);        tn = a * (1 -pow(r,n-1));     printf("tn term of G.P.: %f",tn);     printf("\nSum of the G.P.: %f",sum);     getch();     return 0; }

C program to find out the sum of given H.P

source code #include <stdio.h> void main() {     int n;     float i, sum, term;     printf("1 + 1 / 2 + 1 / 3 +......+1 / n \n");     printf("Enter the value of n \n");     scanf("%d", &n);     sum = 0;     for (i = 1; i <= n; i++)            {      term = 1 / i;      sum = sum + term;     }  printf("the Sum of H.P Series is = %f", sum); getch(); }

C program to find out the sum of given A.P

source code #include<stdio.h> #include<math.h> int main() {     int a,d,n,i,tn;     int sum=0;     printf("Enter the first number of the A.P. series: ");     scanf("%d",&a);     printf("Enter the total numbers in the A.P. series: ");     scanf("%d",&n);     printf("Enter the common difference of A.P. series: ");     scanf("%d",&d);     sum = ( n * ( 2 * a + ( n -1 ) * d ) )/ 2;     tn = a + (n-1) * d;     printf("Sum of the series A.P.: ");     for(i=a;i<=tn; i= i + d ){          if (i != tn)              printf("%d + ",i);          else              printf("%d = %d ",i,sum);     }     gech();     return 0; }

C program to find out largest element of an array.

source code #include<stdio.h> int main() {   int a[50],size,i,big;   printf("\nEnter the size of the array: ");   scanf("%d",&size);   printf("\nEnter %d elements in to the array: ”, size);   for(i=0;i<size;i++)   scanf("%d",&a[i]);   big=a[0];   for(i=1;i<size;i++) {  if(big<a[i])            big=a[i];   }   printf("\nBiggest: %d",big);   getch();   return 0; }

C program to find out second smallest element of an unsorted array

source code #include<stdio.h> int main() {   int a[50],size,i,j=0,small,secondsmall;   printf("Enter the size of the array: ");   scanf("%d",&size);   printf("Enter %d elements in to the array: ", size);   for(i=0;i<size;i++)          scanf("%d",&a[i]);   small=a[0];   for(i=1;i<size;i++) {          if(small>a[i]) {                small=a[i];                j = i;       }   }   secondsmall=a[size-j-1];   for(i=1;i<size;i++){          if(secondsmall > a[i] && j != i)               secondsmall =a[i];   }   printf("Second smallest: %d", secondsmall);   getch();   return 0; }

C program which deletes the duplicate element of an array.

source code #include<stdio.h> int main() {   int arr[50];   int *p;   int i,j,k,size,n;   printf("\nEnter size of the array: ");   scanf("%d",&n);   printf("\nEnter %d elements into the array: ",n);   for(i=0;i<n;i++)     scanf("%d",&arr[i]);   size=n;   p=arr;   for(i=0;i<size;i++) {     for(j=0;j<size;j++) {          if(i==j) {              continue;          }          else if(*(p+i)==*(p+j)) {              k=j;              size--;              while(k < size) {                  *(p+k)=*(p+k+1);                  k++;               }               j=0;           }       }   }   printf("\nThe array after removing duplicates is: ");

C program for delete an element at desired position in an array.

source code #include<stdio.h> int main() {   int a[50],i,pos,size;   printf("\nEnter size of the array: ");   scanf("%d",&size);   printf("\nEnter %d elements in to the array: ",size);   for(i=0;i<size;i++)   scanf("%d",&a[i]);   printf("\nEnter position where to delete: ");   scanf("%d",&pos);   i=0;   while(i!=pos-1)             i++;   while(i<10) {             a[i]=a[i+1];             i++;   }   size--;   for(i=0;i<size;i++)             printf(" %d",a[i]);   getch();   return 0; }

C program for insert an element at desired position in an array.

source code #include<stdio.h> int main() { int a[50],size,num,i,pos,temp; printf("\nEnter size of the array: "); scanf("%d",&size); printf("\nEnter %d elements in to the array: ",size);                  for(i=0;iscanf("%d",&a[i]);               printf("\nEnter position and number to insert: "); scanf("%d %d",&pos,&num); i=0; while(i!=pos-1) i++; temp=size++; while(i) { a[temp]=a[temp-1]; temp--; } a[i]=num; for(i=0;iprintf(" %d",a[i]); getch(); return 0; }

C program to find largest and smallest number in an array.

source code #include<stdio.h> int main() {   int a[50],size,i,big,small;   printf("\nEnter the size of the array: ");   scanf("%d",&size);   printf("\nEnter %d elements in to the array: ", size);   for(i=0;i<size;i++)       scanf("%d",&a[i]);   big=a[0];   for(i=1;i<size;i++) {       if(big<a[i])            big=a[i];   }   printf("Largest element: %d",big);   small=a[0];   for(i=1;i<size;i++) {       if(small>a[i])            small=a[i];   }   printf("Smallest element: %d",small);   getch();   return 0; }