ads

Sunday, 28 August 2016

c program for transpose of a matrix

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[100][100],i,j,temp,n,m;
 clrscr();
 printf("enter how many rows and colums:");
 scanf("%d%d",&n,&m);
 printf("enter the matrix:");
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 printf("matrix before transpose\n");
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
 }
 for(i=0;i<4;i++)
 {
  for(j=0;j<4;j++)
  {
   temp=a[i][j];
   a[i][j]=a[j][i];
   a[j][i]=temp;
  }
 }
 printf("transpose of matrix:\n");
 for(i=0;i<n;i++)
 {
  for(j=0;j<m;j++)
  {
   printf("%d\t",a[j][i]);
  }
  printf("\n");
 }
 getch();
 }

c program for multiplication of two matrix

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[3][3],b[3][3],c[3][3],i,j;
  printf("enter first matrix:");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
      scanf("%d",&a[i][j]);
    }
  }
  printf("enter second matrix:");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
       scanf("%d",&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("MULTIPLICATION OF TWO MATRIX:\n");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
     printf("%d\t",c[i][j]);
    }
   printf("\n");
  }
getch();
}

c program for subtraction of two matrix

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[3][3],b[3][3],c[3][3],i,j;
  printf("enter first matrix:");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
      scanf("%d",&a[i][j]);
    }
  }
  printf("enter second matrix:");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
       scanf("%d",&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("SUBTRACTION OF TWO MATRIX:\n");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
     printf("%d\t",c[i][j]);
    }
   printf("\n");
  }
getch();
}

c program for addition of two matrices

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[3][3],b[3][3],c[3][3],i,j;
  printf("enter first matrix:");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
      scanf("%d",&a[i][j]);
    }
  }
  printf("enter second matrix:");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
       scanf("%d",&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("ADDITION OF TWO MATRIX:\n");
  for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
     printf("%d\t",c[i][j]);
    }
   printf("\n");
  }
getch();
}


c program to find/search a element in an array

#include<stdio.h>
#include<conio.h>
void main()
{
  int a[50],i,n,flag;
  printf("enter numbers of element in an array:");
  scanf("%d",&n);
  printf("enter the numbers:");
  for(i=0;i<n;i++)
  scanf("%d",&a[i]);
  printf("enter the element you want to search:");
  scanf("%d",&s);
  for(i=0;i<n;i++)
  {
     if(s==a[i])
     flag=1;
     break;
  }
  if(flag==1)
  printf("number is present");
  else
  printf("number is absent");
  getch();

c program to sort element of an array in ascending order

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,temp,j;
clrscr();
printf("enter the size of array:");
scanf("%d",&n);
printf("enter the number:");
for(i=0;i<n;i++)
{
 scanf("%d",&a[i]);
}
printf("array before sorting:");
for(i=0;i<n;i++)
{
 printf("\n%d",a[i]);
}
for(i=0;i<n-1;i++)
 for(j=i+1;j<n;j++)
 { 
 if(a[i]>a[j])
 {
  temp=a[i];
  a[i]=a[j];
  a[j]=temp;
 }
 }
}
printf("\narray after sorting:");
for(i=0;i<n;i++)
{
 printf("\n%d",a[i]);
}

getch();
}

c program to find minimum element from array

#include <stdio.h>
#include <conio.h> 
void main()
{
  int a[100],min,n,i,location = 1;
  printf("Enter the number of elements in array\n");
  scanf("%d", &n);
  printf("Enter the numbers\n"); 
  for (i = 0; i < n; i++)
  scanf("%d", &a[i]);
  min = a[0];
  for (i = 1; i < n; i++)
  {
    if (a[i] > min)
    {
       max  = [i];
       location = i+1;
    }
  }
  printf("Minimum element is present at location %d and it's value is %d.\n", location,               
  max);
  getch();
}