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