sort even and odd terms of an array

Program to sort even and odd terms of an array

#include < stdio.h>
#include < conio.h>
main()
{
int t,a[10],i,j,k;
clrscr();
printf("Enter the array");
for(i=0;i < 10;i++)
{
scanf("%d",&a[i]);
}
for(j=0;j < 10;j++)
{
for(i=0;i < 9-j;i++)
{
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
}}
for(i=0;i < 10;i++)
{
for(j=0;j < 9;j++)
{
if((a[j]%2==0)&&(a[j]%2)!=(a[j+1]%2))
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}}
printf("\nSorted Array\n");
for(i=0;i < 10;i++)
printf("%d\n",a[i]);
getch();
}

Pascal Triangle

Program to print Pascal Triangle

#include < stdio.h>
#include < conio.h>
main()
{
int i,j,n,a[2][20];
clrscr();
printf("Enter the no of rows : ");
scanf("%d",&n);
for(i=0;i < 20;i++)
{
a[0][i]=0;
a[1][i]=0;
}
for(j=0;j < n;j++)
{
for(i=0;i < n-j;i++)
printf(" ");
for(i=0;i < j;i++)
{
a[0][i]=a[1][i];
if(i==0)
a[1][0]=1;
else
a[1][i]=a[0][i-1]+a[0][i];
printf("%4d",a[1][i]);
}
printf("\n\n");
}
getch();
}

Pascal Triangle

#include< stdio.h>
#include< conio.h>
void main()
{
clrscr();
int a,b,i,j;
int ab[20][40]={0};
printf("Enter Number (less than 13): ");
scanf("%d",&a);
b=(2*a)+1;
ab[0][a+1]=1; /* CENTER */
// ab[0][a+1]=1; /* RIGHT indent*/
// ab[0][1]=1; /*LEFT indent */
for(i=1;i< a;i++)
for(j=1;j< 40;j++)
{
ab[i][j]=ab[i-1][j-1]+ab[i-1][j+1]; /* CENTER */
// ab[i][j]=ab[i-1][j]+ab[i-1][j+1]; /* RIGHT indent*/
// ab[i][j]=ab[i-1][j-1]+ab[i-1][j]; /*LEFT indent */
}
for(i=0;i< a;i++)
{
for(j=1;j< =b;j++)
{
if(ab[i][j]==0)
printf(" ");
else
printf("%3d",ab[i][j]);
}
printf("\n\n");
}
getch();
}

Sorting Even Nos in the middle of sorted Odd Nos

#include< stdio.h>
#include< conio.h>
void main()
{
/* Sorting numbers in an array and then moving all even numbers to middle*/
clrscr();
int s[15],n,t,e=0,o,k,ot,tt;
printf("\nEnter Limit : ");
scanf("%d",&n);
printf("\n\nEnter Numbers : \n\n");
for(int i=0;i< n;i++)
{
scanf("%d",&s[i]);
}
printf("\nYour Numbers are : ");
for(i=0;i< n;i++)
{
if((s[i]%2)==0)
e++;
printf("%4d",s[i]);
}
printf("\n\n\nSorted as even numbser at middle : ");
o=n-e;
ot=o/2;

/*Part 1 : Sorting all numbers */
for(i=0;i< n;i++)
{
for(int j=0;j< (n-1-i);j++)
{
if(s[j]>s[j+1])
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
else
continue;
}
}


/*Part 2 : Pushing half odd numbers to end */
for(i=0;i< ot;i++)
{
for(int j=0;j< (n-1-i);j++)
{
if((s[j+1]%2)==0&&(s[j]%2)!=0)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
else
continue;
}
}


/*Part 3 : Pushing even numbers to end at remaining place */
tt=n-1-ot;
for(i=0;i< tt;i++)
{
for(int j=0;j< (tt-i);j++)
{
if((s[j+1]%2)!=0&&(s[j]%2)==0)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
else
continue;
}
}
for(k=0;k< n;k++)
printf("%4d",s[k]);
getch();
}

Symmetric matrix

Program to check whether given matrix is symmetric or not

#include < stdio.h>
#include < conio.h>
main()
{
int a[9][9],r,i,j,n=0;
clrscr();
printf("Enter the order of the matrix:");
scanf("%d",&r);
printf("Enter the Matrix\n");
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
{
if(a[i][j]!=a[j][i])
n=1;
}}
if(n==1)
printf("\n\nUnsymmetric");
else
printf("\n\nSymmetric");
getch();
}

interchange any rows or columns of a matrix

Program to interchange any rows or columns of a matrix

#include < stdio.h>
#include < conio.h>
main()
{
int i,j,t,ch,a[10][10],r,c,r1,r2,c1,c2;
clrscr();
printf("\n\nEnter dimension of matrix : Rows : ");
scanf("%d",&r);
printf("columns: ");
scanf("%d",&c);
printf("enter the matrix:\n");
for(i=0;i < r;i++)
{
for(j=0;j < c;j++)
{
scanf("%d",&a[i][j]);
}}
printf("\n\n MENU\n1.Interchange rows\n2.interchange columns\n Enter your choice:");
scanf("%d",&ch);
if(ch==1)
{
printf("Enter the no of rows:");
scanf("%d%d",&r1,&r2);
for(i=0;i < c;i++)
{
t=a[r1-1][i];
a[r1-1][i]=a[r2-1][i];
a[r2-1][i]=t;
}}
else if(ch==2)
{
printf("Enter the no of columns:");
scanf("%d%d",&c1,&c2);
for(i=0;i < r;i++)
{
t=a[i][c1-1];
a[i][c1-1]=a[i][c2-1];
a[i][c2-1]=t;
}}
else
printf("Invalid choice");
printf("\n\nMatrix is\n");
for(i=0;i < r;i++)
{
for(j=0;j < c;j++)
printf(" %d",a[i][j]);
printf("\n");
}
getch();
}

nth power of a matrix

Program to find nth power of a matrix

#include < stdio.h>
#include < conio.h>
void mul();
int a[10][10],c[10][10],r;
main()
{
int i,j,n;
clrscr();
printf("Enter the order of the matrix:");
scanf("%d",&r);
printf("Enter the Matrix\n");
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
{
scanf("%d",&a[i][j]);
if(i==j)
c[i][j]=1;
else
c[i][j]=0;
}
}
printf("Enter the power : ");
scanf("%d",&n);
for(i=0;i < n;i++)
mul();
printf("\n\nAnswer\n");
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
printf(" %d",c[i][j]);
printf("\n");
}
getch();
}
void mul()
{
int b[10][10],i,j,k;
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
{
b[i][j]=0;
for(k=0;k < r;k++)
b[i][j]=b[i][j]+a[i][k]*c[k][j];
}}
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
c[i][j]=b[i][j];
}
}