sort strings

Program to sort strings

#include < stdio.h>
#include < conio.h>
#include < string.h>
char a[5][20],t[20];

void swap(int n);
main()
{
int i,j,m,k;
clrscr();
printf("\nEnter the strings\n");
for(i=0;i < 5;i++)
gets(a[i]);
for(m=0;m < 5;m++)
{
for(i=0;i < 4-m;i++)
{
j=0;k=0;
while(k==0)
{
if(a[i][j]>a[i+1][j])
swap(i);
k++;
if(a[i][j]==a[i+1][j])
{
k=0;
j++;
}
}
}}

printf("Sorted strings\n\n");
for(i=0;i < 5;i++)
puts(a[i]);
getch();
}

void swap(int n)
{
int i;
for(i=0;a[n][i]!='\0';i++)
t[i]=a[n][i];
t[i]='\0';
for(i=0;a[n+1][i]!='\0';i++)
a[n][i]=a[n+1][i];
a[n][i]='\0';
for(i=0;t[i]!='\0';i++)
a[n+1][i]=t[i];
a[n+1][i]='\0';
}



Determinant of Matrix

Program to find Determinant of Matrix

#include < stdio.h>
#include < conio.h>
int det(int n,int c,int r);
int a[10][10],b[10][10];
powr(int a);
void main()
{
int i,j,k,m,ans;
clrscr();
printf("Enter the order : ");
scanf("%d",&m);
printf("enter the matrix\n");
for(i=0;i < m;i++)
{
for(j=0;j < m;j++)
{
scanf("%d",&a[i][j]);
b[i][j]=a[i][j];
}
}
ans=det(m,0,0);
printf("answer=%d",ans);
getch();
}

int det(int n,int x,int y)
{
int ans,i,j,c[10][10],ii,jj,k;
ans=0;
for(i=0;i < n;i++)
{
for(j=0;j < n;j++)
c[i][j]=a[i][j];
}
if(n==1)
return(c[x][y]);

else
{
for(k=0;k < n;k++)
{
for(i=0,ii=0;i < n;i++,ii++)
{
for(j=0,jj=0;j < n;j++,jj++)
{
if(i==x)
i++;
if(j==k)
j++;
a[ii][jj]=c[i][j];
}}
ans=ans+powr(x+k)*c[x][k]*det(n-1,0,0);
}
return(ans);
}

}


powr(int a)
{
if(a%2==0)
return(1);
else
return(-1);
}

Convert between Bin,Oct,Dec and Hex form

Program to Convert between Bin,Oct,Dec and Hex form

#include < stdio.h>
#include < conio.h>
#include < string.h>
int conv(char c);
tobin(int a);
tooct(int b);
tohex(int a);
main()
{
int l,m,k,i,j,a;
char n[25];
clrscr();
printf("Enter the no followed by type(as 1001b,234d,654o,4AC6h etc) : ");
scanf("%s",n);
l=strlen(n);
switch(n[l-1])
{
case 'b':
j=2;
break;
case 'o':
j=8;
break;
case 'h':
j=16;
break;
case 'd':
j=10;
break;
default:
j=10;
l++;
break;
}
a=0;
m=1;
for(i=l-2;i>=0;i--)
{
k=conv(n[i]);
a=a+k*m;
m=m*j;
}
printf("\n\n Dec : %d",a);
tobin(a);
tooct(a);
tohex(a);
getch();
}
int conv(char a)
{
int p;
p=char(a);
if(p>47)
return(p-48);
else if(p>64)
return(p-55);
else if(p>96)
return(p-97);
}

tobin(int a)
{
char h[300];
int l,i,k;
for(i=0;a!=0;i++)
{
k=a%2;
h[i]=char(48+k);
a=a/2;
}
l=i-1;
printf("\n\n Bin : ");
for(i=l;i>=0;i--)
printf("%c",h[i]);

}

tooct(int a)
{
int b=0,k,m=1;
while(a!=0)
{
k=a%8;
b=b+m*k;
m=m*10;
a=a/8;
}
printf("\n\n Oct : %d",b);
}

tohex(int a)
{
char h[30];
int l,i,k;
for(i=0;a!=0;i++)
{
k=a%16;
if(k < 10)
h[i]=char(48+k);
else
h[i]=char(55+k);
a=a/16;
}
l=i-1;
printf("\n\n Hex : ");
for(i=l;i>=0;i--)
printf("%c",h[i]);
}

String-Replace

program to replace a string by another string

#include < stdio.h>
#include < conio.h>
#include < string.h>
main()
{
int i,j,mm,k=0,l3,l1,l2;
char a[200],b[50],c[50],ch;
clrscr();
printf("\nEnter the string : ");
gets(a);
printf("\nEnter the string to be deleted : ");
gets(b);
printf("\nReplace it with : ");
gets(c);
l1=strlen(a);
l2=strlen(b);
l3=strlen(c);
for(i=0;a[i]!='\0';i++)
{
if(b[0]==a[i])
{
k=1;
for(j=0;b[j]!='\0';j++)
{
if(a[i+j]!=b[j])
{
k--;
break;
}
}
if(b[j]=='\0')
{
printf("\nWord found do u want to replace it(y/n) : ");
ch=getche();
if(ch=='y'||ch=='Y')
{
for(j=i;j < =l1-l2;j++)
a[j]=a[j+l2];
l1=strlen(a);
for(mm=0;mm < l3;mm++)
for(j=l1+l3-1;j>i;j--)
a[j]=a[j-1];
for(j=0;j < l3;j++)
a[i+j]=c[j];
l1=strlen(a);
printf("\n\n%s",a);
}
}
}
}
if(k==0)
printf("Not found");
getch();
}C-Programming Standards & Guidelines: Version USearch Amazon.com for cprogramming

String -Delete

program to delete a substring from a string

#include < stdio.h>
#include < conio.h>
#include < string.h>
main()
{
int i,j,k=0,l1,l2;
char a[200],b[50],ch;
clrscr();
printf("\nEnter the string : ");
gets(a);
printf("\nEnter the string to be deleted : ");
gets(b);
l1=strlen(a);
l2=strlen(b);
for(i=0;a[i]!='\0';i++)
{
if(b[0]==a[i])
{
k=1;
for(j=0;b[j]!='\0';j++)
{
if(a[i+j]!=b[j])
{
k--;
break;
}
}
if(b[j]=='\0')
{
printf("\nWord found do u want to delete it(y/n) : ");
ch=getche();
if(ch=='y'||ch=='Y')
{
for(j=i;j < l1-l2;j++)
a[j]=a[j+l2];
printf("\n\n%s",a);
}
}
}
}
if(k==0)
printf("Not found");
getch();
}

String Search

Program to search a string

#include < stdio.h>
#include < conio.h>
#include < string.h>
main()
{
int i,j,k=0;
char a[200],b[50];
clrscr();
printf("\nEnter the string : ");
gets(a);
printf("\nEnter the string to be found : ");
gets(b);
for(i=0;a[i]!='\0';i++)
{
if(b[0]==a[i])
{
k=1;
for(j=0;b[j]!='\0';j++)
{
if(a[i+j]!=b[j])
{
k--;
break;
}
}}
if(k==1)
break;
}
if(k==1)
printf("Found");
else
printf("Not found");
getch();
}

set operations

Program to do set operations

#include < stdio.h>
#include < conio.h>
void setu();
void ints();
void min();
void canc(int i);
int a[20],b[20],c[40],s[20],d1,d2,d3,d4;
main()
{
int i,ch;
clrscr();
printf("Enter dim of set A: ");
scanf("%d",&d1);
printf("Enter set A : ");
for(i=0;i < d1;i++)
{
scanf("%d",&a[i]);
c[i]=a[i];
}
printf("Enter dim of set B: ");
scanf("%d",&d2);
printf("Enter set B : ");
for(i=0;i < d2;i++)
scanf("%d",&b[i]);
d3=d1;
ints();
printf("\n\n\n\tMENU");
printf("\n1.A U B\n2.A ( B\n3.A - B");
printf("\n\nEnter your choice : ");
scanf("%d",&ch);
printf("Result : [ ");
switch(ch)
{
case 1:
setu();
for(i=0;i < d3;i++)
printf("%d ",c[i]);
break;
case 2:
ints();
for(i=0;i < d4;i++)
printf("%d ",s[i]);
break;
case 3:
min();
for(i=0;i < d3;i++)
printf("%d ",c[i]);
break;
default:
printf("Invalid Operation ");
break;
}
printf("]");
getch();
}

void ints()
{
int i,j;
d4=0;
for(i=0;i < d1;i++)
{
for(j=0;j < d2;j++)
{
if(a[i]==b[j])
{
s[d4]=a[i];
d4++;
break;
}
}}
}

void canc(int s)
{
int i;
for(i=s;i < d3-1;i++)
c[i]=c[i+1];
}

void min()
{
int i,j;
for(i=0;i < d4;i++)
{
for(j=0;j < d3;j++)
{
if(c[j]==s[i])
{
canc(j);
d3--;
}
}}
}

void setu()
{
int i;
d3=d1+d2;
for(i=d1;i < d1+d2;i++)
{
c[i]=b[i-d1];
}
min();
}

Sort array (modified)

Program to sort an array in odd even odd fashion

#include < stdio.h>
#include < conio.h>
int a[10];
void swap(int i);
main()
{
int e,o=0,x,t,i,j,k;
clrscr();
printf("Enter the array");
for(i=0;i < 10;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==1)
o++;
}
for(j=0;j < 10;j++)
{
for(i=0;i < 9-j;i++)
{
if(a[i]>a[i+1])
swap(i);
}}

for(i=0;i < 10;i++)
{
for(j=0;j < 9;j++)
{
if((a[j]%2==1)&&(a[j+1]%2==0))
swap(j);
}}


x=o/2;
e=10-o;
for(j=0;j < x;j++)
{
t=a[e+j];
for(i=e+j;i>j;i--)
a[i]=a[i-1];
a[j]=t;
}
printf("\nSorted Array\n");
for(i=0;i < 10;i++)
printf("%d\n",a[i]);
getch();
}
void swap(int i)
{
int t;
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}

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];
}
}

Different types of triangles/Pyramids

Different types of triangles/Pyramids

#include < stdio.h>
#include < conio.h>
main()
{
int m,i,j,r,k,ch;
char x;
do
{
clrscr();
printf("\n ***STARS***\n\n\n PATTERNS\n -----------\n 1.RIGHT TRIANGLE-up\n 2.RIGHT TRIANGLE-down\n 3.TRIANGLE-common\n 4.EQUILATERAL TRIANGLE-up\n 5.EQUILATERAL TRIANGLE-down\n 6.DIAMOND\n\n ");
printf("ENTER NO. OF ROWS:");
scanf("%d",&r);
printf("\n ENTER THE PATTERN:");
scanf("%d",&ch);
clrscr();
printf("\n\n");


//1
if(ch==1)
{
for(i=1;i<=r;i++)
{
for(j=0;j{
printf(" *");
}
printf("\n");
}}


//2
if(ch==2)
{
for(i=r;i>=0;i--)
{
for(j=0;j{
printf(" *");
}
printf("\n");
}}


//3
if(ch==3)
{
for(i=1;i<=r;i++)
{
for(j=0;j{
printf(" *");
}
printf("\n");
}
for(i=r-1;i>=0;i--)
{
for(j=0;j{
printf(" *");
}
printf("\n");
}}

//4
if(ch==4)
{
for(i=1;i<=r;i++)
{
m=r-i;
for(k=0;kprintf(" ");
for(j=0;j{
printf(" *");
}
printf("\n");
}}

//5
if(ch==5)
{
for(i=r;i>=0;i--)
{
m=r-i;
for(k=0;kprintf(" ");
for(j=0;j{
printf(" *");
}
printf("\n");
}}


//6
if(ch==6)
{
for(i=1;i<=r;i++)
{
m=r-i;
for(k=0;kprintf(" ");
for(j=0;j{
printf(" *");
}
printf("\n");
}
for(i=r-1;i>=0;i--)
{
m=(r)-i;
for(k=0;kprintf(" ");
for(j=0;j{
printf(" *");
}
printf("\n");
}}



//
if(ch>6)
{
printf("invalid code\n");
}
printf(" DO YOU WANT TO CONTINUE(y/n)..");
scanf("%s",&x);
}
while(x=='y');
clrscr();
printf("\n\n\n\n\n\n\n\n ***THANK YOU***\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n programer:jitkaramel(e-mail:jithinkaramel@gmail.com)");
getch();
}

Amstong Nos upto a given Limit

Amstong Nos upto a given Limit

#include < stdio.h>
#include < conio.h>
main()
{
int i,j,a,k=0,n;
clrscr();
printf("\n enter the Limit:");
scanf("%d",&n);
for(a=1;a<=n;a++)
{
i=a;
k=0;
while(i!=0)
{
j=i%10;
k=k+(j*j*j);
i=i/10;
}
if(k==a)
printf("%d\n",a);
}
getch();
}

Leap Year

WAP to find whether given year is a Leap Year

#include
#include
main()
{
int y;
clrscr();
printf("\n enter the year\n ");
scanf("%d",&y);
if(y%4==0)
{
if(y%100==0)
{
if(y%400==0)
{
printf(" common year");
if(y%1000==0)
{
if(y%4000==0)
printf(" common year");
}}}
else
printf(" leap year");
}
else
printf(" common year");
getch();
}

Palindrome - String

check whether the given string is a palindrome

#include < stdio.h>
#include < conio.h>
#include < string.h>
main()
{
char a[20],b[20]="";
int i,l,j;
clrscr();
printf("ENTER THE STRING:");
gets(a);
l=strlen(a);
for(i=0;i{
b[i]=a[l-i-1];
}
j=strcmp(a,b);
if(j==0)
printf("palindrom");
else
printf("not palindrom");
getch();
}

Amstong no

Find whether given no is an Amstrong no?


#include < stdio.h>
#include < conio.h>
main()
{
int i,j,a,k=0;
clrscr();
printf("\n enter the no:");
scanf("%d",&i);
a=i;
while(i!=0)
{
j=i%10;
k=k+(j*j*j);
i=i/10;
}
if(k==a)
printf("amstrong no");
else
printf("not an amstrong no");
getch();
}