Showing posts with label c programs. Show all posts
Showing posts with label c programs. Show all posts

program for BINARY SEARCH in c language


 #include<stdio.h>

#include<conio.h>

void main()

{

int a[10],beg,end,item,i,loc,mid;

clrscr();

printf("\n Enter the array element");

for(i=0;i<=10;i++)

scanf("%d",&a[i]);

printf("\n Enter the no. to be search->");

scanf("%d",&item);

beg=0;end=9;

mid=(beg+end)/2;

while(beg<=end&&a[mid]!=item)

{

if(item<a[mid])

end=mid-1;

else

beg=mid+1;

mid=(beg+end)/2;

}

if(item==a[mid])

{

loc=mid;

printf("\n item located possition %d",loc+1);


}

else

{

loc=NULL;

printf("\n item not exist");

}

getch();

}

program for stack as an array in c language


 #include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define ms 3

int stack[ms],i,item,top=-1;

void pop();

void push();

void traverse();

void main()

{

int n;

char ch;

clrscr();

do

{

printf("\n 1 push");

printf("\n 2 pop");

printf("\n 3 traverse");

printf("\n 4 exit");

printf("\n 5 Enter your choice->");

scanf("%d",&n);

switch(n)

{

case 1:

push();

break;

case 2:

pop();

break;

case 3:

traverse();

break;

case 4:

exit(0);

default :

printf("\nPlease Enter your right choice->");




}

printf("\nDo you want to continue(y/n)->");

fflush(stdin);

scanf("%c",&ch);



}  while(ch=='y'||ch=='Y');

getch();



}

void push()

{       printf("\nenter new element->");

scanf("%d",&item);

if(top==ms-1)

{

printf("-----OVERFLOW----");

exit(0);

break;


}

else

{

top=top+1;

stack[top]= item;

}

}

void pop()

{

if(top==-1)

{

printf("\nunderflow");

exit(0);

break;

}

else

{

item=stack[top];

top=top-1;

printf("\n%d is deleted ",item);

}

}

void traverse()

{

for(i=top;i>=0;i--)

{

printf("\n%d",stack[i]);


}


}


program for adding two numbers in c language with details



#include<stdio.h> 
#inlcude<conio.h>
void main()
{
    int a,b;
   clrscr();
    printf("Enter any two number");
    scanf("%d,%d",&a,&b);
    printf("sum=%d",a+b);
    getch();
}
meaning of all c element is
# - prepocessor
include - prepocessor directive
< > - angel brackets its mean more than necessary
stdio.h, conio.h - header file
void - retun 0
main() - main function
int - data type
clrscr() - clear screen function
printf() - print function
scanf() - scan function it is the put the value in variable
getch() - that function hold the output
{ } - scope