Monday, March 23, 2009

EXAMPLES OF C PROGRAMS

BASICS OF C LANGUAGE:

'C-Language' is the basic language for all programing languages.Here the basics of c and some of tha DATA STRUCTURES programs has been explained with examples. Some of the advanced concepts like pointers, linked list, that has been given with C++ example also. So rock yourself in C language.

Lets start with the basics of C...(Welcome)




Example-:

#include
#include

void main()
{
clrscr();
printf("WELCOME TO DATA STRUCTURES USING C");
getch();
}





Explaination:

The above C program is used to display the given string as out put.

OUTPUT:

WELCOME TO DATA STRUCTURES USING C



Example-:

#include
#include

void main()
{
clrscr();
int a;
a=25;
printf("My Age is: %d",&a);
getch();
}



Explaination:


The above C program is used to display the age 20 as out put.

OUTPUT:

My age is: 25



KEEP IT IN MIND:

  1. A C program Must starts with proper HEADER FILES.
  2. All the lines should be ended with semicolan (;).
  3. A C program must be written inside the MAIN() Function.
  4. The main() function should starts and ends with Flower Braces {}.

ARRAY IMPLEMENTATION

Example-: ---------(Example for array Creation,Deletion,Modification)

//#include
#include
#include
#include

void main()
{
int arr[20],l,c,p,i;
clrscr();
printf("\nEnter the limit of array----:>");
scanf("%d",&l);
printf("ENTER THE DATA---> ");
for(p=0;p<l;p++)
{

scanf("%d",&arr[p]);
}
while(1)
{
printf("\n1.INPUT\n2.DELETE\n3.VIEW\n4.EXIT\nEntert the choice--> ");
scanf("%d",&c);
switch(c)
{
case 1: clrscr();// CORDING OF INSERT BLOCK
readw:
printf("\nEnter the position---> ");scanf("%d",&p);
if(p>l)
{
printf("\n\t\tARRAY OUT OF LIMIT\n\nPlease re enter your POSITION:\n");
goto readw;
}
else if(p==0)
{
printf("\n\t\tARRAY OUT OF LIMIT\n\nPlease re enter your POSITION:\n");
goto readw;
}
for(i=l;i>=p-1;i--)
arr[i]=arr[i-1];
l=l+1;
printf("Enter the data--<> ");scanf("%d",&arr[p-1]);
break;
case 2: clrscr();// DELETE BLOCK
readd:
printf("\nEnter the position---> ");scanf("%d",&p);
if(p>l)
{
printf("\n\t\tARRAY OUT OF LIMIT\n\nPlease re enter your POSITION:\n");
goto readd;
}
else if(p==0)
{
printf("\n\t\tARRAY OUT OF LIMIT\n\nPlease re enter your POSITION:\n");
goto readd;
}
for(i=(p-1);i<l;i++)
arr[i]=arr[i+1];
l=l-1;
printf("\n\t\t<<--ONE ELEMENT DELETED-->>\n");
break;
case 3: clrscr(); // VIEW BLOCK
printf("\nARRAY ELEMNTS ARE>>\n");
for(i=0;i<l;i++)
printf("\n\t\t%5d pos=%d",arr[i],(i+1));
break;
case 4: //EXIT BLOCK
exit(0);
}
}
}

FIND MAXIMUM AND MINIMUM OF NUMBERS

EXAMPLE-:------(My shortest program to find the Maximum and Minimum of given numbers)

#include
#include
void main()
{
int a[10],n,i;
long int b=1111110,k=0;
clrscr();
printf("Enter the limit:-->");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;ia[i])
b=a[i];
if(k<a[i])
k=a[i];
}
printf("THE MAXIMUM-->%ld\nTHE MINIMUM---->%ld",k,b);
getch();

}

FIBANOCI SERIES

EXAMPLE-:-------(Program for fibonaci series)

#include
#include
void main()
{
int f1=-1,f2=1,fn,n,i;
clrscr();
printf("emter the limit");
scanf("%d",&n);
for(i=0;i<n;i++)
{
fn=f1+f2;
f1=f2;
f2=fn;
printf("%d\n",fn);
}
getch();
}

BINARY SEARCH

EXAMPLE-:-------(Program to implement binary search)

#include
#include

void main()
{
int num[20],ord[20],n,i,j,p,s,temp;
int ul,ll,mid;
clrscr();
printf("enter the limit-->");
scanf("%d",&n);
printf("Enter the DATA-->\n");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
ord[i]=num[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;jord[j])
{
temp=ord[i];
ord[i]=ord[j];
ord[j]=temp;
}
}
}
clrscr();
printf("\n<--THE SORTED LILST-->");
for(i=0;i<n;i++)
printf("\n%d",ord[i]);

printf("\n\nEnter the search element--->");
scanf("%d",&s);
ll=0;
ul=n;
while(1)
{
mid=(ll+ul)/2;
if((ul==llul<0)&&ord[mid]!=s)>s)
ul=mid-1;
else if(ord[mid]==s)
{
printf("\nExists\n\n");
printf("\nPosition-->%d",mid+1);
break;
}
else if(ord[mid]<s)
ll=mid+1;
}
getch();
}

STACK OPERATION

EXAMPLE-:-------(Program to implement Stack Operations)


//To display stack operation
#include
#include

void main()
{
int top=0,val[15],li,ch,i;
clrscr();
printf("\nEnter the capacity of the stack\n");
scanf("%d",&li);
while(1)
{
printf("\n");
printf("\t\t\t Stack operation\n");
printf("\n\t 1. Push");
printf("\n\t 2. Pop");
printf("\n\t 3. Display stack");
printf("\n\t 4. Quit\n");
printf("\n\n\n Enter the choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();
if(li==top)
{
printf("\n The stack is full");
}
else
{
printf("\nEnter the value:");
scanf("%d",&val[top]);
top++;
}
break;
case 2:
clrscr();
if(top==0)
{
printf("\nThe stack is empty\n");
}
else
{
top--;
printf("\n%d is removed from the stack ",val[top]);
val[top]=0;
}
break;
case 3:
clrscr();
if(top==0)
{
printf("There is No Element in the Stack");
}
else
printf("\n The elements of the stack are ");
for(i=top-1;i>=0;i--)
printf("\n %d",val[i]);
break;
default:
exit(0);
}
}
}

QUEUE OPERATION

EXAMPLE-:-------(Program to implement Queue Operations)

#include
#include
void main()
{
int i,li,ch,front=0,rear=0,val[100];
clrscr();
printf("Enter the capacity\n");
scanf("%d",&li);
while(1)
{
printf("\n\t\t\t Queue Operations\n");
printf("1. Insertion\n");
printf("2. Deletion\n");
printf("3. Display queue\n");
printf("4. Quit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();
if(rear==li) printf("The Queue is full");
else
{
printf("Enter the value\n");
scanf("%d",&val[rear]);
rear++;
}
break;
case 2:
clrscr();
if(rear==front)
printf("The Queue is empty\n");
else
{
printf("\n The Deleted element is %d",val[front]);
for(i=1;i<=rear;i++)
val[i-1]=val[i];
rear--;
}
break;
case 3:
clrscr();
if(rear==front)
printf("There is no element in the Queue\n");

else
printf("The Elements are\n");
for(i=0;i<rear;i++)
printf("%d\t",val[i]);

break;
default:
exit(0);
}
}
}

QUICK SORT

EXAMPLE-9:-------(Program to implement Quick sort)

#include
#include
void main()
{
int i,n,c,s,q,a[50],b[50];
clrscr();
printf("\n\t\t quick sort");
printf("\n\t\t***********");
printf("\n\t enter the no of elements:");
scanf("%d",&n);
printf("\n\t enter the values 1 by 1 :");
for(i=1;i<=n;i++) { scanf("\t %d",&a[i]); b[i]=a[i]; } q=n; s=1; quick(a,s,q); printf("\n\t before sorted\t after quicksort"); for(i=1;i<=n;i++) { printf("\n\t\t%d\t\t\t%d",b[i],a[i]); } getch(); return; } quick(int c[50],int m,int n) { int i=0,j,t,k; if(mk);
if(i {
t=c[i];
c[i]=c[j];
c[j]=t;
}
else
goto a2;
goto a1;
a2:
t=c[m];
c[m]=c[j];
c[j]=t;
quick(c,m,j-1);
quick(c,j+1,n);
}
return;
}

POLYNOMIAL ADDITION

EXAMPLE-:-------(Program to implement Polynomial addition)

#include
#include
int a[10][2];
int b[10][2];
int c[10][2];
//void inp(int);
main()
{
int n,i=0,j=0,k=0;
clrscr();
printf("\n\tEnter the First Equation\n");
while(1)
{
printf("Enter the Co-efficient--->");
scanf("%d",&a[i][0]);
printf("Enter the Exponent------>");
scanf("%d",&a[i][1]);
if(a[i][1]==0)
break;
i++;
}
i++;
a[i][1]=-1;
printf("\n\tEnter the Second eq:\n");
while(1)
{
printf("Enter the Co-efficient--->");
scanf("%d",&b[j][0]);
printf("Enter the Exponent------>");
scanf("%d",&b[j][1]);
if(b[j][1]==0)
break;
j++;
}
j++;
b[i][1]=-1;
i=0;j=0;k=0;
while(a[i][1]!=-1&&b[j][1]!=-1)
{
if(a[i][1]==b[j][1])
{
c[k][0]=a[i][0]+b[j][0];
c[k][1]=a[i][1];
i++;j++;k++;
}
if(a[i][1]>b[j][1])
{
c[k][1]=a[i][1];
c[k][0]=a[i][0];
i++;k++;
}
if(a[i][1] {
c[k][1]=b[j][1];
c[k][0]=b[j][0];
j++;k++;
}
}
while(a[i][1]!=-1)
{
c[k][0]=a[i][0];
c[k][1]=a[i][1];
k++;
i++;
}
while(b[j][1]!=-1)
{
c[k][0]=b[i][0];
c[k][1]=b[i][1];
k++;
j++;
}
c[k][1]=-1;
clrscr();
i=0;j=0;k=0;
printf("THE FIRST EQ:--> ");
while(a[i][1]!=-1)
{
printf("->%d^%d ",a[i][0],a[i][1]);
i++;
}
printf("\nTHE SECOND EQ:--> ");
while(b[j][1]!=-1)
{
printf("->%d^%d ",b[j][0],b[j][1]);
j++;
}
printf("\nTHE RESULT EQ:--> ");
while(c[k][1]!=-1)
{
printf("->%d^%d ",c[k][0],c[k][1]);
k++;
}
getch();
}

TRANSEPOSE OF MATRIX

EXAMPLE:-------(Program to implement Transpose of a matrix)

// TRANSPOSE MATRIX
#include
#include

void main()
{
int mat[10][10],tran[10][10];
int i,j,m,n,tn,tm;
clrscr();
printf("Enter the row value:-->");
scanf("%d",&m);
printf("Enter the column value:-->");
scanf("%d",&n);
printf("<::Enter the elements::>\n");
for(i=0;i for(j=0;j scanf("%d",&mat[i][j]);
tn=m;
tm=n;
for(i=0;i for(j=0;j tran[i][j]=mat[j][i];

printf("\nTHE TRANSPOSE MATRIX\n");

for(i=0;i {
for(j=0;j printf("\t%d",tran[i][j]);
printf("\n");
}
getch();

}

Friday, March 20, 2009

PROGRAM TO GENERATE ASCII CODE

#include
#include
main()
{
// program for ascii chart
int ch;
clrscr();
for(ch=0;ch<=255;ch++)
{
textbackground(0);
textcolor(2);
cprintf("SCANCODE=%d, ASCII-Ch=%c",ch,ch);
printf("\n\n");
printf("**************************");
printf("\n\n");
getch();
}
}

SIMPLEST PALINDROME

#include
#include
void main()
{
char line[50],word[50],tmp[50],c;
int count=0,i=0,j=0;
clrscr();
printf("enter the sentence\n");
gets(line);
do
{
c=line[i];
if (c==' '||c=='\0')
{
word[j]='\0';
strcpy(tmp,word);
strrev(tmp);
if(strcmp(word,tmp)==0)
{
count+=1;
}
i++;
j=0;
}
else
{
word[j]=c;
i++,j++;
}
}
while(c!='\0');
printf("no.of palin=%d",count);
getch();

}

STRING FUNCTIONS

#include
#include
#include
int n,s;
char str[30];
left();
mid();
right();
main()
{
int ch;
do
{
clrscr();
printf("\n\n\t\t\t string functions ");
printf("\n\n\t\t\t **************** ");
printf("\n\t enter the string mode :");
printf("\n\t 1.left\n\t 2.mid\n\t 3.right\n\t 4.exit ");
printf("\n\t enter your chioce :");
scanf("%d",&ch);
switch(ch)
{
case 1:
left();
break;
case 2:
mid();
break;
case 3:
right();
break;
case 4:
exit(0);
}
getch();
}
while(ch!=4);
return(0);
}

left()
{
int i;
char substr[20];
printf("\n\t enter the string :");
scanf("%s",str);
printf("\n\t enter the no of charachters to be extracted :");
scanf("%d",&n);
for(i=0;isubstr[i]=str[i];
substr[i]='\0';
printf("\n\t substring of left :%s",substr);
return(0);
}


mid()
{
int k=0,i;
char substr[20];
printf("\n\t enter the string :");
scanf("%s",str);
printf("\n\t enter the start postion &no of characters to be extracted ");
scanf("%d%d",&s,&n);
for(i=s-1;i<(s+n)-1;i++)
{
substr[k]=str[i];
k++;
}
substr[k]='\0';
printf("\n\t substring of mid :%s",substr);
return(0);
}


right()
{
int i,l,k=0;
char substr[20];
printf("\n\t enter the string :");
scanf("%s",str);
printf("\n\t enter the no of charachters to be extracted :");
scanf("%d",&n);
l=strlen(str);
for(i=l-n;i{
substr[k]=str[i];
k++;
}
printf("\n\t substring of right :%s",substr);
return(0);
}

CALCULATE DAYS BETWEEN TWO DATES

#include
#include
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int d[2],m[2],y[2];
long int tot[2]={0,0},di=0;
void cal(int);
void main()
{
int i;
clrscr();
for(i=0;i<2;i++)
{
printf("Enter the DATE %d: \n",i+1);
printf("\nEnter the MONTH--> ");
scanf("%d",&m[i]);
printf("\nEnter the DATE --> ");
scanf("%d",&d[i]);
printf("\nEnter the YEAR --> ");
scanf("%d",&y[i]);
cal(i);
}
di=tot[1]-tot[0]-1;
printf("\nTHE DIFFERENCE BETWEEN TO DATES--->%d",di);
getch();
}
void cal(int n)
{
int i;
tot[n]+=(y[n]-1)*365.25;
for(i=0;i<=m[n]-2;i++)
{
if(y[n]%4==0)
tot[n]+=29;
else
tot[n]+=month[i];
}
tot[n]=tot[n]+d[n];
}

CONVERT INTEGER INTO WORDS

#include
#include
void main()
{
int l,i,s,n,h;
char *a[21]={"zero","one","two","three","four","five","six","seven","eight","nine","ten",
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
char *b[11]={" "," ","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety","hundred"};
clrscr();
printf("\n\n\n\t\t\t CONVERTING INTEGER TO WORDS");
printf("\n\n\n\t\tENTER THE NUMBER BETWEEN(1 TO 1000);");
scanf("%d",&n);
if((n>=0) && (n<=20))
printf("\n\n\t\t%s",a[n]);
if(n>20&&n<101)
{
l=n%10;
s=n/10;
printf("\n\n\t\tTHE WORD IS %s %s ",b[s],b[l]);
}
if(n>100&&n<1000)
{
if(n%100==0)
{
h=n/100;
printf("\n\n\n\t\t THE WORD IS :%s HUNDRED",a[h]);
}
else
{
l=n%10;
n=n/10;
s=n%10;
h=n/10;
printf("\n\n\n\t\t THE WORD IS :%s HUNDRED AND %s %s",a[h],b[s],a[l]);
}
}
if(n==1000)
{
printf("\n\n\n\t\tTHE WORD IS: THOUSAND");
}
getch();
}

MAGIC SQUARE

#include
#include
void main()
{
int a[100][100],r,c,mid,i,n;
clrscr();
//printf("\n\n\t\t\t magic square ");
//printf("\n\n\t\t\t ------------ ");
printf("\n\n\t\t enter the n value :");
scanf("%d",&n);
mid=(n+1)/2;
r=1;
c=mid;
for(i=1;i<=n*n;i++)
{
a[r][c]=i;
if((i%n)==0)
{
r=r+1;
}
else
{
if(r==1)
r=n;
else
r=r-1;
if(c==n)
c=1;
else
c=c+1;
}
}

printf("\n\n\t\t the matrix as :\n\n");
for(r=1;r<=n;r++)
{
printf("\n");
for(c=1;c<=n;c++)
printf("\t%d",a[r][c]);
}
getch();
//return(0);
}

PROGRAM FOR STANDARD DEVIATION

#include
#include
#include
main()
{
int i,j,n,f=0,sum=0;
float t,a[50],m,v=0,c[50]={1},big;
clrscr();
//printf("\n\t\t FIND MEAN MEDIAN MODE VARIANCE & SD");
printf("\n\n\n\t\t ENTER THE N VALUES : ");
scanf("%d",&n);
printf("\n\t\t ENTER THE ARRAY VALUES : ");
for(i=1;i<=n;i++)
{
scanf(" %f", &a[i]);
sum=sum+a[i];
}


m=sum/(float)n;
printf("\n\n\t\t ARITHMETIC MEAN : %2.f",m);

for(i=1;i<=n;i++)
v+=pow((a[i]-m),2);
v=v/(float)n;
printf("\n\n\t\t VARIANCE :%2.f",v);

printf("\n\n\t\t STADNARD DEVIATION :%2.f",sqrt(v));

if ((n%2)!=0)
printf ("\n\n\t\t MEDIAN :%.f",a[(n/2)+1]);
else
printf("\n\n\t\t MEDIAN :%.2f",(a[n/2]+a[(n/2)+1])/2);

for(i=1;i<=m;i++)
for(j=i+1;j<=n;j++)
{
if (a[i]==a[j])
++c[i];
}
big=c[1];
for(i=1;i<=n;i++)
if(c[i]>big)
big=c[i];
if (big==0)
printf("\n\n\t\t THERE IS NO MODE OCCURS IN GIVEN VALUES");
else
{
printf("\n\n\t\t MODE IS :");
for (i=1;i<=n;i++)
if(big==c[i])
{
if(f)
printf(",");
printf("% .0f",a[i]);
f=1;
}
printf("\n\n\t\t IS OCCURS %.0f TIMES ",big+1);
}
getch();
}

POLYNOMIAL ADDITION

?#include
#include

void main()
{
int r1,c1,r2,c2,i,j,k;
static int a[10][10],b[10][10];
static int ans[10][10],a2[10][10],b2[10][10],ab[10][10];
clrscr();
printf("Enter the ROW & COL VALUES MATRIX A\n");
scanf("%d%d",&r1,&c1);
printf("Enter the ROW & COL VALUES MATRIX B\n");
scanf("%d%d",&r2,&c2);
printf("Enter the values of first matrix--->\n");
for(i=0;i<r1;i++) k="0;k<r2;k++)" i="0;i<r2;i++)" j="0;j<c1;j++)">\n\n");
for(i=0;i<r1;i++)> {
for(j=0;j<c2;j++)> {
printf("\t%d",ans[i][j]);
}

printf("\n");
}

getch();
}

PALINDROME PROGRAM

// palindrome in given sentence

#include "stdio.h"
#include "conio.h"
#include "string.h"

void main()
{
int w,n,i,j=0,k=0;
char str[100],dum1[10],dum2[10];
int c=0;
clrscr();
printf("\t<-Enter the string->\n\n");
gets(str);
clrscr();
printf("<--THE GIVEN SENTENCE-->");
printf("%s",str);
for(i=0;;i++)
{
dum1[j]=str[i];
j++;
if(((dum1[j-1]==' ')||(dum1[j-1]=='\0'))&&str[i-1]!=' ')
{
dum1[j-1]='\0';
j-=2;k=0;
while(j>-1)
{
dum2[k]=dum1[j];
j--;
k++;
}
dum2[k]='\0';
n=strcmp(dum1,dum2);
if(n==0)
{
printf("\n>> %-9s is a <--PALINDROME-->",dum1);
c++;
}
else
printf("\n>> %-9s is a <--NOT PALINDROME-->",dum1);
j=0;
}
if(str[i]=='\0')
break;

}
printf("\nthe given string has %d palindrome\n",c);
getch();

}

SIMPLE SEARCHING AND SORTING

#include
#include
#include
main()
{
int n,i,j,c=0;
char temp[30],name[28][30],srch[50];
clrscr();
//printf("\n\t\t SORTING & SEARCHING");
printf("\n\t\t ENTER THE NUMBER OF STUDENTS :");
scanf("%d",&n);
printf("\n\n\t\t ENTER THE STUDENTS NAME ONE BY ONE :");
for(i=0;i {
scanf(" %s",name[i]);
}
for(i=0;i {
for(j=i+1;j {
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}

printf("\n\n\t\t THE NAMES IN THE SORTED ORDERS IS : ");
for(i=0;i printf("\n\t\t\t\t %s",name[i]);

printf("\n\n\t\t ENTER THE NAME TO BE SEARCHED :");
scanf("%s",srch);
for(i=0;i if(strcmp(name[i],srch)==0)
c=c+1;
if(c>=1)
printf("\n\n\t\t THE NAME IS FOUND");
else
printf("\n\n\t\t THE NAME IS NOT FOUND");
getch();
return;
}

PROGRAM FOR STUDENT DETAILS

#include<conio.h>
#include<process.h>
#include<stdio.h>


void main()
{
int n,i,j;
char det[8][10]={"NAME","MARK :1: ","MARK :2: ","MARK :3: ","MARK :4: ","MARK :5: ","MARK :6: ","MARK :7: "};
struct student
{
char name[30];
int m[7],tot,rn;
float a;
} s[10];
clrscr();
printf("HOW MANY STUDENTS ? \n");
scanf("%d",&n);
printf("<--Enter the student details-->\n");
for(i=0;i");
scanf("%d",&s[i].rn);
for(j=0;j<7;j++)>100s[i].m[j]<0) i="0;i%d\nAVERAGE-->%3.2f",s[i].tot,s[i].a);
getch();
}
getch();
}