Thursday, April 3, 2014

PROGRAM TO PRINT THE FREQUENCY OF EACH ELEMENT IN AN ARRAY

//PROGRAM TO PRINT THE FREQUENCY OF EACH ELEMENT IN AN ARRAY

#include<conio.h>
#include<stdio.h>
void main()
{
int a[10],i,c,j,n;
clrscr();
printf("enter the limit of array\n");
scanf("%d",&n);
printf("enetr the elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
c=0;
for(j=0;j<n;j++)
{
if(a[i]==a[j])
{
c++;
}
}
printf("the frequency of element %d at %d index is %d\n",a[i],i,c);
}
getch();
}

PROGRAM TO PRINT THE LOWER TRIANGLE OF A MATRIX

//PROGRAM TO PRINT THE LOWER TRIANGLE OF A MATRIX

#include<conio.h>
#include<stdio.h>
#define row 3
#define col 3
void main()
{
int ar[row][col],i,j,n;
clrscr();

printf("enetr the elements of array\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&ar[i][j]);
}
}
printf(" the elements of array\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",ar[i][j]);
}
printf("\n\n");
}
if(row==col)
{
printf("the lower triangular matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(j<i||j==i)
{
printf(" %d ",ar[i][j]);
}
}
printf("\n\n");
}
}
else
printf("cant calculate the lower matrix");
getch();
}

PROGRAM TO PRINT THE UPPER TRIANGLE IN A MATRIX

//PROGRAM TO PRINT THE UPPER TRIANGLE IN A MATRIX

#include<conio.h>
#include<stdio.h>
#define row 3
#define col 3
void main()
{
int ar[row][col],i,j,n;
clrscr();

printf("enetr the elements of array\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&ar[i][j]);
}
}
printf(" the elements of array\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",ar[i][j]);
}
printf("\n\n");
}
if(row==col)
{
printf("the upper triangular matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(j>i||j==i)
{
printf(" %d ",ar[i][j]);
}
else
{
printf("   ");
}
}
printf("\n\n");
}
}
else
printf("cant calculate th e upper matrix");
getch();
}

PROGRAM FOR SUM OF ROW AND COLUMN OF NON SQUARE/SQUARE MATRIX

//PROGRAM FOR SUM OF ROW AND COLUMN OF NON SQUARE/SQUARE MATRIX

#include<stdio.h>
#include<conio.h>
main()
{
 int i,j,k,A[10][10],R_S,C_S,r,c;
 clrscr();
 printf(" ENTER THE SIZE OF A MATRIX \n");
 scanf("%d%d",&r,&c);

 printf("ENTER THE ELEMENT OF A MATRIX \n");
 for(i=0;i<r;i++)
  for(j=0;j<c;j++)
    scanf("%d",&A[i][j]);

 /* Sum of rows */
 for(i=0;i<r;i++)
 {
   R_S=0;
    for(j=0;j<c;j++)
        {
        R_S=R_S+A[i][j];
}
printf("\nSum of %d row is %d",i+1,R_S);
 }

/* Sum of Column */
for(i=0;i<c;i++)
{
        C_S=0;
          for(j=0;j<r;j++)
   {
    C_S=C_S+A[j][i];
           }
   printf("\nSum of %d Col is %d",i+1,C_S);
}

getch();
  }

PROGRAM TO PRINT THE ROW SUM,COLUMN SUM AND DIAGONAL SUM OF A SQUARE MATRIX

//PROGRAM TO PRINT THE ROW SUM,COLUMN SUM AND DIAGONAL SUM OF A SQUARE MATRIX

#include<conio.h>
#include<stdio.h>
#define row 3
#define col 3
void main()
{
int ar[row][col],i,j,r_s,c_s,d_s=0;
clrscr();

printf("enetr the elements of array\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&ar[i][j]);
}
}
printf(" the elements of array\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",ar[i][j]);
}
printf("\n\n");
}
for(i=0;i<row;i++)
{
r_s=0;c_s=0;
for(j=0;j<col;j++)
{
r_s=r_s+ar[i][j];
c_s=c_s+ar[j][i];
if(i==j)
{
d_s=d_s+ar[i][j];
}
}
printf("\nsum of row %d is %d",i,r_s);
printf("\nsum of col %d is %d",i,c_s);
}
printf("\nsum of diagonal is %d",d_s);
getch();
}

PROGRAM TO PRINT THE TRANSPOSE OF A MATRICES

PROGRAM TO PRINT THE TRANSPOSE OF A MATRICES

#include<conio.h>
#include<stdio.h>
void main()
{
int a[10][10],i,j,r,c;
clrscr();
printf("enter the no of rows\n");
scanf("%d",&r);
printf("enter the no of col\n");
scanf("%d",&c);
printf("enter the matrics\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("the elements of matrics\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf(" %d ",a[i][j]);
printf("\n\n");
}
printf("the transpose of matrics is\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
printf(" %d ",a[j][i]);
printf("\n\n");
}
getch();
}

PROGRAM TO MULTIPLY TWO MATRICES

//PROGRAM TO MULTIPLY TWO MATRICES
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],k,i,j,r1,c1,r2,c2;
clrscr();
printf("enetr the no of rows of first matrics\n");
scanf("%d",&r1);
printf("enter the no of cols of first matrics\n");
scanf("%d",&c1);
printf("enetr the no of rows of second matrics\n");
scanf("%d",&r2);
printf("enter the no of cols of second matrics\n");
scanf("%d",&c2);
printf("enter the elements of first matrics\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("the elements of first matrics are \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf(" %d ",a[i][j]);
printf("\n\n");
}
printf("enter the elements of second matrics\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
printf("the elements of second matrics are \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf(" %d ",b[i][j]);
printf("\n\n");
}

if(c1==r2)
{
printf("the multiplication of two matrices is\n");
for(i=0;i<r1;i++)
{
 for(j=0;j<c2;j++)
   {
     c[i][j]=0;
      for(k=0;k<c1;k++)
       {
        c[i][j]=c[i][j]+a[i][k]*b[k][j];
       }
     printf(" %d ",c[i][j]);
   }
  printf("\n\n");
}
}
else
printf("multiplication is not possible\n");

getch();
}

//PROGRAM TO ADD TWO MATRICES

//PROGRAM TO ADD TWO MATRICES
#include<conio.h>
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,r1,c1,r2,c2;
clrscr();

printf("enetr the no of rows of first matrics\n");
scanf("%d",&r1);
printf("enter the no of cols of first matrics\n");
scanf("%d",&c1);
printf("enetr the no of rows of second matrics\n");
scanf("%d",&r2);
printf("enter the no of cols of second matrics\n");
scanf("%d",&c2);

printf("enter the elements of first matrics\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}

printf("the elements of first matrics are \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf(" %d ",a[i][j]);
printf("\n\n");
}

printf("enter the elements of second matrics\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}

printf("the elements of second matrics are \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf(" %d ",b[i][j]);
printf("\n\n");
}

if(r1==r2&&c1==c2)
{
printf("the addition of two matrices is\n");
for(i=0;i<r1;i++)
{
  for(j=0;j<c1;j++)
    {
     c[i][j]=a[i][j]+b[i][j];
    }
 printf(" %d ",C[i][j]);}
}
else
printf("addition is not possible\n");
getch();
}

PROGRAM TO ADD 10 TO EACH ELEMENT OF A 2-D ARRAY AND PRINT IT IN MATRIX FORM

//PROGRAM TO ADD 10 TO EACH ELEMENT OF A 2-D ARRAY AND PRINT IT IN MATRIX FORM

#include<conio.h>
#include<stdio.h>
void main()
{
int ar[10][10],i,j,row,col;
clrscr();
printf("enetr the no of rows\n");
scanf("%d",&row);
printf("enter the no of cols\n");
scanf("%d",&col);
printf("enter the elements of 2d array\n");
for(i=0;i<row;i++)
{
  for(j=0;j<col;j++)
  {
    scanf("%d",&ar[i][j]);
  }
}
printf("the elements of 2d array are\n");
for(i=0;i<row;i++)
{
   for(j=0;j<col;j++)
   {
     printf(" %d ",ar[i][j]+10);
   }
printf("\n\n");
}
getch();
}

PROGRAM TO CONVERT A DECIMAL NUMBER INTO BINARY NUMBER(NUMBER SHOULD BE A WHOLE NUMBER)

//PROGRAM TO CONVERT A DECIMAL NUMBER INTO BINARY NUMBER(NUMBER SHOULD BE A WHOLE NUMBER)

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,t;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
t=n;
for(i=0;n>=1;i++)
{
a[i]=n%2;
n=n/2;
}
printf("the binary representation of %d is ",t);
for(i=i-1;i>=0;i--)
printf("%d",a[i]);
getch();

PROGRAMME TO SORT AN ARRAY

//PROGRAMME TO SORT AN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],t,i,j,m;
clrscr();
printf("enter the limit of first array\n");
scanf("%d",&m);
printf("enter the elements of array\n");
for(i=0;i<m;i++)
{scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
{
  for(j=0;j<m-1-i;j++)
  {
   if(a[j]>a[j+1])
      {
       t=a[j];
       a[j]=a[j+1];
       a[j+1]=t;
      }
  }
}
printf("the elements of array in sorted order are\n");
for(i=0;i<m;i++)
{
printf("%d\n",a[i]);
}
getch();
}

PROGRAM TO MERGE TWO ARRAYS PROVIDED THAT ARRAYS ARE NOT ENTERED IN SORTED ORDER. A MERGED ARRAY CONTAINS ELEMENTS IN SORTED ORDER

//PROGRAM TO MERGE TWO ARRAYS PROVIDED THAT ARRAYS ARE NOT ENTERED IN SORTED ORDER. A MERGED ARRAY CONTAINS ELEMENTS IN SORTED ORDER

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[10],t,i,j,k,l,n,m;
clrscr();
printf("enter the limit of first array\n");
scanf("%d",&m);
printf("enter the limit of second array\n");
scanf("%d",&n);
printf("enter the elements of array1\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<m;i++)
{
 for(j=0;j<m-1-i;j++)
  {
   if(a[j]>a[j+1])
     {
      t=a[j];
      a[j]=a[j+1];
      a[j+1]=t;
     }
  }
}

printf("the elements of array1\n");
for(i=0;i<m;i++)
{
printf("%d\n",a[i]);
}

printf("enter the elements of array2\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}


for(i=0;i<n;i++)
{
 for(j=0;j<n-1-i;j++)
  {
   if(b[j]>b[j+1])
   {
     t=b[j];
     b[j]=b[j+1];
     b[j+1]=t;
   }
  }
}


printf("the elements of array2\n");
for(i=0;i<n;i++)
{
 printf("%d\n",b[i]);
}

for(i=0,j=0,k=0;i<m&&j<n;)
{
  if(a[i]<b[j])
    {
      c[k]=a[i];
      i++;
      k++;
    }
  else if(a[i]>b[j])
    {
     c[k]=b[j];
     k++;
     j++;
    }
  else
    {
     c[k]=a[i];
     i++;
     j++;
     k++;
    }
}


for(;i<m;i++)
{
  c[k]=a[i];
  k++;
}

for(;j<n;j++)
{
  c[k]=b[j];
  k++;
}

printf("elements of third array are\n");
for(i=0;i<k;i++)
printf("%d\n",c[i]);
getch();
}

PROGRAM TO ADD TWO UNEQUAL SIZED ARRAYS

//PROGRAM TO ADD TWO UNEQUAL SIZED ARRAYS

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]={0},b[10]={0},c[10]={0},i,l,n,m;
//PARTIAL INITIALIZATION  WILL SET ALL INDEX BY 0 OF ARRAY A, B & C
clrscr();
printf("enter the limit of first array\n");
scanf("%d",&m);
printf("enter the limit of second array\n");
scanf("%d",&n);
printf("enter the elements of array1\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("enter the elements of array2\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}

if(m>n)
{
l=m;
for(i=0;i<m;i++)
{
c[i]=a[i]+b[i];
}
}
else
{
l=n;
for(i=0;i<n;i++)
{
c[i]=a[i]+b[i];
}
}
printf("elements of third array are\n");
for(i=0;i<l;i++)
printf("%d\n",c[i]);
getch();
}

PROGRAM TO ADD TWO EQUAL SIZED ARRAY

//PROGRAM TO ADD TWO EQUAL SIZED ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[10],i,m;
clrscr();
printf("enter the limit for array\n");
scanf("%d",&m);
printf("enter the elements of array1 and array2\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
scanf("%d",&b[i]);
c[i]=a[i]+b[i];
}
printf("the elements of third array are\n");
for(i=0;i<m;i++)
printf("%d\n",c[i]);
getch();
}

PROGRAM TO SEARCH AN ELEMENT IN AN ARRAY

//PROGRAM TO SEARCH AN ELEMENT IN AN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[10]={0},flag=0,i,item,n;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the value to be searched\n");
scanf("%d",&item);
printf("enter the elements\n");

for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
for(i=0;i<n;i++)
{
if(ar[i]==item)
{
flag=1;
printf("element %d found at index %d",item,i);
break;
}
}
if(flag==0)
printf("element not found");
getch();
}

PROGRAM TO FIND MIN AND MAX ELEMENT IN AN ARRAY

//PROGRAM TO FIND MIN AND MAX ELEMENT IN AN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[10]={0},min,max,i,n,j,temp;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the elements\n");

for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
min=ar[0];
max=ar[0];
for(i=0;i<n;i++)
{
if(ar[i]>max)
max=ar[i];
if(ar[i]<min)
min=ar[i];
}
printf("max=%d min=%d",max,min);
getch();
}

PROGRAM TO REVERSE AN ARRAY WITHOUT USING ANOTHER ARRAY

//PROGRAM TO REVERSE AN ARRAY WITHOUT USING ANOTHER ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[10],i,n,j,temp;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
printf("reverse of array is\n");
for(i=0,j=n-1;i<j;i++,j--)
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
for(i=0;i<n;i++)
{
printf("\n%d",ar[i]);
}
getch();
}

PROGRAMME TO PRINT THE ELEMNTS OF AN ARRAY IN REVERSE ORDER

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[10],i,n,s=0,c=0,avg;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
i--;
printf("reverse of array is\n");
for(;i>=0;i--)
printf("%d\n",ar[i]);
getch();
}

PROGRAM TO PRINT THE ELEMENTS OF AN ARRAY IN REVERSE ORDER

//PROGRAM TO PRINT THE ELEMENTS OF AN ARRAY IN REVERSE ORDER

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[10],i,n,s=0,c=0,avg;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
i--;
printf("reverse of array is\n");
for(;i>=0;i--)
printf("%d\n",ar[i]);
getch();
}

PROGRAMME TO PRINT ALL THE ELEMENTS GRATER THAN THE AVERAGE OF ALL ELEMENTS AND ALSO PRINT THE TOTAL COUNT

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[10],i,n,s=0,c=0;
int avg;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
s=s+ar[i];
}
avg=s/n;
printf("average is %d",avg);
for(i=0;i<n;i++)
{
if(ar[i]>avg)
{
printf("\n%d is grater than avg",ar[i]);
c++;
}
}
printf("\ntotal elemnts grater than avg are %d",c);
getch();
}

PROGRAMME TO FIND THE SUM OF ALL ELEMENTS OF AN ARRAY

#include<stdio.h>
#include<conio.h>
void main()
{
int ar[10],i,n,s=0;
clrscr();
printf("enter the limit\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
s=s+ar[i];
}
printf("the sum is %d",s);
getch();
}