| Code Index | Download | Printer Friendly Page | Links |
Note: All the source codes in this site have been compiled and tested
with Borland's Turbo C++ 3.0
[http://codeprojectc.tripod.com]
Code Index
The Hello World Program
Printing Formatted Strings
Printing Numbers
The Addition Program
Sum of integers from 1 to 10
Sum of 1 to n, where n is input value
Total marks and the average class mark program
The do-while repetition structure
Sum of first even numbers from 0 to n, where n is a input value
Sum of ODD numbers from 0 to n, where n is an input number
The Temperature Program
The Genuine Fahrenheit-Celsius Table by Brian W. Kernighan and Dennis M. Ritchie*
Area of a Circle
A Simple GCD Program-01-Euclid’s method
The Square Function
The GCD Function using Euclid's method
Maximum Integer Function
Function Property [ABCD=(AB+CD)*2]
The Switch Construct
if-selection Structure-01-A simple grading program
if-selection structure-02-Relationship between Two Numbers
Is number Even or Odd ?
Prime Numbers*
Perfect Numbers*
for-repetition structure-01-Sum of the first n even numbers
for-repetition structure-02-Sum of the first n odd numbers
The GCD Program-02
The Break Statement
Sales Value Counter Program
Arrays-01-Initializing Arrays and storing, printing their elements
Arrays-02-Printing a word from a character array*
Arrays-03-The reverse of a character array*
Arrays-04-Finding the max, min, position of max, position of min, total, average in a data array*
Bubble Sort Program*
Merge Sort Program*
Direct Insertion Sort*
Sequencial Search Program*
Binary Search Program*
The Hello World Program
#include<stdio.h>
void main()
{
printf("Hello World");
}
Printing Formatted Strings
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Welcome\n”);
printf(“to\t”);
printf(“C!”);
getch();
}
Printing Numbers
#include<stdio.h>
#include<conio.h>
main()
{
int x;
x=10;
printf(“The Square of the %d is : %d”,x,x*x);
getch();
return 0;
}
The Addition Program
#include<stdio.h> /*line1*/
#include<conio.h>
main()
{
int x; /*line2*/
int y;
int z;
printf(“Enter the first number:\n”);
scanf(“%d”,&x); /*line3*/
printf(“Enter the Second number:\n”);
scanf(“%d”,&y);
z = x + y; /*line4*/
printf(“Sum is %d\n”,z);
getch();
return 0;
}
Sum of integers from 1 to 10
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,c=1;
clrscr();
while (c<=10)
{
sum+=c;
c++;
}
printf("The total is %d\n",sum);
getch();
}
Sum of 1 to n, where n is input value
#include<stdio.h>
#include<conio.h>
main()
{
int n,c=0,sum=0;
clrscr();
printf("Enter the value of n:\n");
scanf("%d",&n);
while (c<=n)
{
sum=sum+c;
c++;
}
printf("The sum is: %d\n",sum);
getch();
return 0;
}
Total marks and the average class mark program
/*dummy mark = -1*/
#include<stdio.h>
#include<conio.h>
main()
{
int m,avg,tot=0,c=0;
clrscr();
printf("Enter the marks of the first student\n");
scanf("%d",&m);
while(m!=-1)
{
tot+=m;
c++;
printf("Enter the marks of the next student\n");
scanf("%d",&m);
}
if(c!=0)
avg=tot/c;
printf("The total of the marks is %d\n",tot);
printf("The average class mark is %d\n",avg);
getch();
return 0;
}
The do-while repeatition structure
#include<stdio.h>
#include<stdio.h>
void main()
{
int digit=1;
do
{
printf("%d\n",digit++);
}
while(digit<=4);
}
/*output is shown below*/
/*
1
2
3
4
*/
Sum of first even numbers from 0 to n, where n is a input
value
#include<stdio.h>
#include<conio.h>
main()
{
int i=0,n,c=0,sum=0;
clrscr();
printf("Enter the value of n:\n");
scanf("%d",&n);
while(c<n)
{
i+=2;
c++;
sum+=i;
}
printf("The sum is: %d\n",sum);
getch();
return 0;
}
Sum of ODD numbers from 0 to n, where n is an input number
# include <stdio.h>
# include <conio.h>
void main()
{
int c,sum=0,n;
printf("Enter the value of n:\n");
scanf("%d",&n);
for (c=1;c<=n;c++)
sum=sum+(c*2-1);
printf("Sum of first %d odd numbers is %d\n",n,sum);
getch();
}
The Temperature Program
#include<stdio.h>
#include<conio.h>
void main()
{
int c,f;
clrscr(); /*Clears the earlier contents there in the screen*/
printf(“Enter the temperature value in Celsius:”);
scanf(“%d”,&c);
f = (c * 9/5) + 32;
printf(“The temperature in Fahrenheit is %d\n”,f);
getch();
}
The Genuine Fahrenheit-Celsius Table by Brian W. Kernighan
and Dennis M. Ritchie
/************************************************************************/
/* Fahrenheit-Celsius Table */
/* The C Programming Language. Second Edition (ANSI C)
*/
/* Brian W. Kernighan */
/* Dennis M. Ritchie
*/
/************************************************************************/
#include<stdio.h>
/*Print Fahrenheit-Celsius table for fahr = 0,20,...,30;floating point
version*/
main()
{
float fahr,celsius;
int lower,upper,step;
lower=0; /*lower limit of temperature table*/
upper=300; /*upper limit*/
step=20; /*step size*/
fahr=lower;
while(fahr<=upper)
{
celsius=(5.0/9.0)*(fahr-32.0);
printf("%3.0f %6.1f\n",fahr,celsius);
fahr=fahr+step;
}
return 0;
}
/************************************************************************/
/*The printf conversion specification %3.0f says that a floating-point
*/
/*number (here fahr) is to be printed at least three characters wide,
*/
/*with no decimal point and no fraction digits. %6.1f describes
*/
/*another number(celsius) that is to be printed at least six
*/
/*characters wide, with 1 digit after the decimal point. The output
*/
/*looks like this.
*/
/*0 -17.8 */
/*20 -6.7 */
/*40 4.4 */
/************************************************************************/
Area of a Circle
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float a;
clrscr();
printf(“Enter the radius value in Cm:”);
scanf(“%d”,&r);
a = (22/7) * r * r;
printf(“The area in cm(2)is %f\n”,a);
getch();
}
A Simple GCD Program-01-Euclid’s method
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,r;
clrscr();
printf("Enter the values of m & n\n");
scanf("%d%d",&m,&n);
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
printf("The Greatest Common Divisor is: %d\n",n);
getch();
}
The Square Function
# include <stdio.h>
# include <conio.h>
int square(int);
main()
{
clrscr();
int x=10,sqr;
sqr=square(x);
printf("Area of 10 into 10 meters is : %d\n",sqr);/*function caller*/
getch();
return 0;
}
/*start of the function square*/
int square(int x)
{
int y;
y=x*x;
return y;
}
/*End of the function square*/
The GCD Function using Euclid's method
/***********************************/
/* Greatest Common Divisor */
/***********************************/
#include<stdio.h>
/*function prototype*/
int gcdivisor(int,int);
/*function main starts here*/
main()
{
int x,y;
printf(“Enter the two integer values: ”);
scanf(“%d%d”,&x,&y);
printf(“Greatest Common Divisor is: %d\n”,gcdivisor(x,y));
return 0;
}
/*function main ends here*/
/*function gcdivisor starts here*/
int gcdivisor (int m,int n)
{
int r;
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
return n;
}
/*function gcdivisor ends here*/
Maximum Integer Function
/***********************************/
/* Maximum Integer Function
*/
/***********************************/
#include<stdio.h>
/*function prototype*/
int findmax(int,int,int);
/*function main starts here*/
main()
{
int x,y,z;
printf(“Enter the three integer values: ”);
scanf(“%d%d%d”,&x,&y,&z);
printf(“Maximum integer is: %d\n”,findmax(x,y,z));
return 0;
}
/*function main ends here*/
/*function findmax starts here*/
int findmax (int p,int q,int r)
{
int max;
max=p;
if(q>max)
max=q;
if(r>max)
max=r;
return max;
}
/*function findmax ends here*/
Function Property [ABCD=(AB+CD)*2]
/*************************/
/* Property Function */
/*************************/
/*Four digit Numbers (ABCD) satisfying property {ABCD=(AB+CD)*2}*/
#include<stdio.h>
#include<conio.h>
void func(int);
void main()
{
int c;
clrscr();
for (c=1000;c<=9999;c++)
func(c);
getch();
}
/*main function ends here*/
/*second Function starts here*/
void func(int x)
{
int a,b;
a=x/100;
b=x%100;
if(((a+b)*(a+b))==x)
printf("Number satisfying property : %d\n",x);
}
/*Second function ends here*/
/*output is shown below*/
/*
Number satisfying property : 2025
Number satisfying property : 3025
Number satisfying property : 9801
*/
The Switch Construct
/*This program demonstrate how to make decisions using the switch
construct*/
#include<stdio.h>
#include<conio.h>
void main()
{
char grade;
printf("Enter Your Grade\t:[A,B,C]\n");
scanf("%c",&grade);
switch(grade)
{
case 'a':case 'A': printf("Congrats its A\n");break;
case 'b':case 'B': printf("Congrats its B\n");break;
case 'c':case 'C': printf("Congrats its C\n");break;
}
getch();
}
if-selection Structure-01-A simple grading program
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,avg;
clrscr();
printf(“Enter the marks of the subject1\n”);
scanf(“%d”,&m1);
printf(“Enter the marks of the subject2\n”);
scanf(“%d”,&m2);
avg = (m1+m2)/ 2;
printf(“The average mark of the student is %d\n”,avg);
if(avg<=34)
printf(“The Result is: F\n”);
else if(avg<=49)
printf(“The Result is: S\n”);
else if(avg<=74)
printf(“The Result is: B\n”);
else
printf(“The Result is: A\n”);
getch();
}
if-selection structure-02-Relationship between Two Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,r;
printf(“Enter the first number\n”);
scanf(“%d”,&a);
printf(“Enter the second number\n”);
scanf(“%d”,&b);
if (a!=b)
if (a>b)
printf(“First number is greater than Second number\n”);
else
printf(“Second number is greater than first number\n”);
else
if (a>=b)
printf(“First number is greater than or equal to Second number\n”);
else
printf(“Second number is greater than or equal to first number\n”);
getch();
}
Is number Even or Odd ?
#include<stdio.h>
#include<conio.h>
void main()
{
int a,r;
printf(“Enter the integer number\n”);
scanf(“%d”,&a);
if (a>0)
r = a % 2;
if (r == 0)
printf(“Number is Even\n”);
else
printf(“Number is Odd\n”);
else
printf(“Number is not a positive integer.Try again”);
getch();
}
Prime Numbers
/*Check Prime numbers*/
#include<stdio.h>
#include<conio.h>
/*start of the chkprime function*/
void chkprime(int x)
{
int halfno,rem,i=2;
halfno=(x/2);
while(i<=halfno)
{
rem=x%i;
if(rem==0)
break;
else
i++;
}
if(i>halfno)
printf("%d\t",x);
}
/*end of the chkprime function*/
/*start of the main function*/
void main()
{
clrscr();
printf("Prime Numbers between 1 to 1000 are:\n");
for(int c=2;c<=1000;c++)
chkprime(c);
getch();
}
/*end of the chkprime function*/
Perfect Numbers
/*Check Perfect numbers*/
#include<stdio.h>
#include<conio.h>
/*start of the chkperfect function*/
void chkperfect(int x)
{
int sum=0,halfno,rem,i=1;
halfno=(x/2);
while(i<=halfno)
{
rem=x%i;
if(rem==0)
{
sum=sum+i;
}
i++;
}
if(x==sum)
printf("%d\t",sum);
}
/*end of the chkperfect function*/
/*start of the main function*/
void main()
{
clrscr();
printf("Perfect Numbers between 1 to 1000 are:\n");
for(int c=2;c<=1000;c++)
chkperfect(c);
getch();
}
/*end of the chkprime function*/
for-repeatition structure-01-Sum of the first n even numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,i,n,j=2;
printf("Enter the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=sum+j;
j=j+2;
}
printf("The sum of Even numbers is: %d\n",sum);
getch();
}
for-repeatition structure-02-Sum of the first n odd numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,i,n,j=1;
printf("Enter the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=sum+j;
j=j+2;
}
printf("The sum of %d ODD numbers counting from zero is: %d\n",n,sum);
getch();
}
The GCD Program-02
/*********************************/
/* Greatest Common Divisor */
/*********************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,rem1,rem2,gcd,p,q;
clrscr();
printf("Enter the First Integer: ");
scanf("%d",&m);
printf("Enter the Second Integer: ");
scanf("%d",&n);
if(m>n)
{
gcd=n;
p=m;
}
else
{
gcd=m;
p=n;
}
q=gcd;
rem1=p%gcd;
rem2=gcd%gcd;
while(rem1!=rem2)
{
gcd=gcd-1;
rem1=p%gcd;
rem2=q%gcd;
}
printf("Greatest Common Divisor is: %d\n",gcd);
getch();
}
The Break Statement
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
for(x=1;x<=10;x++)
{
if(x==5)
break;
printf(“%d\t”,x);
}
/*************************/
/* output:
*/
/* 1 2 3 4 5
*/
/*************************/
Sales Value Counter Program
/*Dummy item code = -99*/
#include<stdio.h>
#include<conio.h>
void main()
{
int ic,qs,sp,sv,tsv=0;
clrscr();
printf("====================\n");
printf("Sales Value Counter\n");
printf("====================\n");
printf("\nEnter the Item Code:\t ");
scanf("%d",&ic);
while (ic!=-99)
{
printf("Enter the Qty Sold:\t ");
scanf("%d",&qs);
printf("Enter the Sales Price:\t ");
scanf("%d",&sp);
sv=qs*sp;
printf("\n\tThe Sales Value of the item code # %d is: %d\n",ic,sv);
tsv=tsv+sv;
printf("\nEnter the Item Code:\t ");
scanf("%d",&ic);
}
if (tsv==0)
{
printf("\n\tNo Items were entered for Processing...\n");
}
printf("\n\tThe Total Sales Value for all the Items entered, is: %d\n",tsv);
printf("\n\tPress any key to terminate...");
getch();
}
Arrays-01
/*Initializing Arrays and storing, printing their elements*/
#include<stdio.h>
#include<conio.h>
int v[10];
void main()
{
int i,q;
clrscr();
for (i=0;i<10;i++)
{
printf("Enter the #%d value into the array\t\n",i+1);
scanf("%d",&v[i]);
}
printf("\nThe values in the array is:\n");
for (i=0;i<10;i++)
{
printf("The #%d valude is: %d\n",i+1,v[i]);
}
getch();
}
Arrays-02
/*This program inputs 5 characters into a char array and output
the word*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char sw[5];
clrscr();
for(i=0;i<5;i++)
{
printf("Enter the %d character into the array\n",i+1);
scanf("%1s",&sw[i]);
}
printf("The Word you entered was:\n\n");
printf("\'");
for(i=0;i<5;i++)
{
printf("%c",sw[i]);
}
printf("\'");
getch();
}
Arrays-03
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define n 5
char a[n];
char b[n];
void main()
{
printf("Enter a string of %d characters: ",n);
gets(a);
int k=strlen(a);
if(k<=n)
{
for(int i=0;i<k;i++)
b[i]=a[k-(i+1)];
printf("The reverse of the string of %d characters is: ",k);
puts(b);
}
else
printf("WARNING : can not exceed %d characters!\nTry again!\n",n);
getch();
}
Arrays-04
/*This is user friendly program which inputs 10 integers into
an array and output the max,min,position of max,position
of min,total,average*/
#include<stdio.h>
#include<conio.h>
# define n 10
int u[n];
/*Function Prototypes*/
void input(int u[n]);
void findmax(int u[n]);
void findmin(int u[n]);
void total(int u[n]);
void average(int u[n]);
/*Function main starts here*/
void main()
{
int opt;
clrscr();
do
{
printf("\n\n=================Menu=================\n");
printf("1. Input Data into an array\n");
printf("2. Find Maximum Value and Its Position\n");
printf("3. Find Minimum Value and Its Postion\n");
printf("4. Find Total\n");
printf("5. Find Average\n");
printf("6. Exit\n");
printf("======================================\n");
printf("Enter your Option: \n");
scanf("%d",&opt);
switch(opt)
{
case 1: input(u);break;
case 2: findmax(u);break;
case 3: findmin(u);break;
case 4: total(u);break;
case 5: average(u);break;
}
}
while(opt<6);
}
/*Function main ends here*/
/*Function input starts here*/
void input(int u1[n])
{
int i;
for(i=0;i<10;i++)
{
printf("Enter Value #%d to array: ",i+1);
scanf("%d",&u1[i]);
}
}
/*Function input ends here*/
/*Function findmax starts here*/
void findmax(int u2[n])
{
int i,max,p;
max=u2[0];
for(i=0;i<n;++i)
{
if (u2[i]>max)
{
max=u2[i];
p=i;
}
}
printf("The Maximum Value is: %d\n",max);
printf("The Position of the Maximum Value is: %d\n",p+1);
}
/*Function findmax ends here*/
/*Function findmin starts here*/
void findmin(int u3[n])
{
int i,min,p;
min=u3[0];
for(i=0;i<n;++i)
{
if (u3[i]<min)
{
min=u3[i];
p=i;
}
}
printf("The Minimum Value is: %d\n",min);
printf("The Position of the Minimum Value is: %d\n",p+1);
}
/*Function findmin ends here*/
/*Function total starts here*/
void total(int u4[n])
{
int i,tot=0;
for(i=0;i<n;i++)
{
tot+=u4[i];
}
printf("The Total is: %d\n",tot);
}
/*Function total ends here*/
/*Function average starts here*/
void average(int u5[n])
{
int i;
float tot=0,avg;
for(i=0;i<n;i++)
{
tot+=u5[i];
}
avg=tot/10;
printf("The Average is: %.4f\n",avg);
}
/*Function average ends here*/
Bubble Sort
/*********************************************/
/* This compilation demonstrates bubble sort */
/*********************************************/
#include<stdio.h>
#include<conio.h>
#define n 5
int u[n];
/*Function Prototypes*/
void input(int u[n]);
void bubblesort(int u[n]);
void output(int u[n]);
/*Start of main function*/
void main()
{
clrscr();
printf("================================================\n");
printf(" BUBBLE SORT \n");
printf("================================================\n\n");
input(u);
bubblesort(u);
output(u);
getch();
}
/*End of Main function*/
/*Start of input function*/
void input(int u1[n])
{
printf("============== Enter the values into the array =====================\n");
for(int i=0;i<n;i++)
{
printf("Enter the #%d value into the array: ",i+1);
scanf("%d",&u1[i]);
}
}
/*End of input function*/
/*Start of bubblesort function*/
void bubblesort(int u2[n])
{
int i,temp,comp,flag;
for(comp=n-1;comp>0;comp--)
{
flag=0;
for(i=0;i<comp;i++)
{
if(u2[i]>u2[i+1])
{
temp=u2[i];
u2[i]=u2[i+1];
u2[i+1]=temp;
flag=1;
}
}
if(flag==0)
break;
}
}
/*End of bubblesort function*/
/*Start of output function*/
void output(int u3[n])
{
printf("\n");
printf("================== Result of the Bubble sorting =====================\n");
for(int c=0;c<n;c++)
{
printf("The #%d value in the array: ",c+1);
printf("%d\n",u3[c]);
}
}
/*End of output function*/
Merge Sort
/********************************************/
/* This compilation demonstrates merge sort */
/* Written by Rangana Withanage */
/********************************************/
#include<stdio.h>
#include<conio.h>
#define m 5
#define n 5
int u[m],v[n],w[m+n];
/*Function Prototypes*/
void input(int u[m],int v[n]);
void mergesort(int u[m],int v[n]);
void output(int u[m+n]);
/*Start of main function*/
void main()
{
clrscr();
printf("================================================\n");
printf(" MERGE SORT \n");
printf("================================================\n\n");
input(u,v);
mergesort(u,v);
output(w);
getch();
}
/*End of Main function*/
/*Start of input function*/
void input(int u1[m],int v1[n])
{
printf("Enter the values into the array one in ascending order\n");
for(int a=0;a<m;a++)
{
printf("Enter the #%d value into the array one: ",a+1);
scanf("%d",&u1[a]);
}
printf("\n");
printf("Enter the values into the array two in ascending order\n");
for(int b=0;b<n;b++)
{
printf("Enter the #%d value into the array two: ",b+1);
scanf("%d",&v1[b]);
}
}
/*End of input function*/
/*Start of mergesort function*/
void mergesort(int u2[n],int v2[m])
{
int i,j,k;
i=j=k=0;
while(i<n && j<m)
{
if(u2[i]<v2[j])
{
w[k]=u2[i];
i++;
}
else
{
w[k]=v2[j];
j++;
}
k++;
}
/**********************************************************/
/* Now one of the arrays is over, we should add */
/* the elements in the remaining array to the array three */
/**********************************************************/
if(i==n)
{
while(j<m)
{
w[k]=v2[j];
j++;
k++;
}
}
if(j==m)
{
while(i<n)
{
w[k]=u2[i];
i++;
k++;
}
}
}
/*End of mergesort function*/
/*Start of output function*/
void output(int w1[m+n])
{
printf("\n");
for(int c=0;c<m+n;c++)
{
printf("The #%d value in the array three: ",c+1);
printf("%d\n",w1[c]);
}
}
/*End of output function*/
Direct Insertion Sort
/********************************************************/
/* This compilation demonstrates direct insertion sort */
/********************************************************/
#include<stdio.h>
#include<conio.h>
#define n 5
int u[n];
/*Function Prototypes*/
void input(int u[n]);
void disort(int u[n]);
void output(int u[n]);
/*Start of main function*/
void main()
{
clrscr();
input(u);
disort(u);
output(u);
getch();
}
/*End of Main function*/
/*Start of input function*/
void input(int u1[n])
{
printf("============== Enter the values into the array =====================\n");
for(int i=0;i<n;i++)
{
printf("Enter the #%d value into the array: ",i+1);
scanf("%d",&u1[i]);
}
}
/*End of input function*/
/*Start of direct insertion sort function*/
void disort(int u2[n])
{
int temp,i,j;
for(i=0;i<n;i++)
{
temp=u2[i];
j=i-1;
while(j>=0 && u2[j]>temp)
{
u2[j+1]=u2[j];
j=j-1;
u2[j+1]=temp;
}
}
}
/*End of direct insertion sort function*/
/*Start of output function*/
void output(int u3[n])
{
printf("\n");
printf("======= Result of the Direct Insertion Sorting
=========\n");
for(int c=0;c<n;c++)
{
printf("The #%d value in the array: ",c+1);
printf("%d\n",u3[c]);
}
}
/*End of output function*/
Sequencial Search
/*Sequencial Search*/
#include<stdio.h>
#include<conio.h>
# define n 5
int a[n];
void input(int a[n]);
void search_seq(int a[n]);
/*Start of Main Function*/
void main()
{
int opt;
clrscr();
do
{
printf("\n");
printf("\t================== Menu ====================\n");
printf("\t1.Enter Values into Array \n");
printf("\t2.Search for a value using sequencial search\n");
printf("\t3.Exit\n");
printf("\t============================================\n");
printf("\tEnter your option(1-3) : ");
scanf("%d",&opt);
printf("\n");
switch(opt)
{
case 1 : input(a); break;
case 2 : search_seq(a); break;
}
}
while(opt<3);
}
/*End of Main Function*/
/*Start of Input Function*/
void input(int a1[n])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter the %d value into array : ",i+1);
scanf("%d",&a1[i]);
}
}
/*End of input finction*/
/*Start of sequencial search function*/
void search_seq(int a2[n])
{
int i,k,flag=0;
printf("Enter the Search Key : ");
scanf("%d",&k);
for(i=0;i<n;i++)
{
if(a2[i]==k)
{
printf("Search Key %d is found in %d Position\n",k,++i);
flag=1;
}
if(flag==1)
{
break;
}
}
if(flag==0)
{
printf("Search Key is not found!\n");
printf("Search is Unsuccessful!\n");
}
}
/*End of the Sequencial search function*/
Binary Search
/*This Compilation Demonstrate Binary Search*/
#include<stdio.h>
#include<conio.h>
# define n 5
int a[n];
void input(int a[n]);
void bin_search(int a[n]);
/*Start of Main Function*/
void main()
{
int opt;
clrscr();
do
{
printf("\n");
printf("\t+------------------ Menu -------------------+ \n");
printf("\t| 1.Enter Integer Values into Array | \n");
printf("\t| 2.Search for a value using Binary Search | \n");
printf("\t| 3.Exit | \n");
printf("\t+-------------------------------------------+ \n");
printf("\tEnter your option(1-3) : ");
scanf("%d",&opt);
printf("\n");
switch(opt)
{
case 1 : input(a); break;
case 2 : bin_search(a); break;
}
}
while(opt<3);
}
/*End of Main Function*/
/*Start of Input Function*/
void input(int a[n])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter the %d value into array : ",i+1);
scanf("%d",&a[i]);
}
}
/*End of input finction*/
/*Start of Binary search function*/
void bin_search(int a[n])
{
int low,high,mid,key;
low=0;
high=n-1;
mid=(low+high)/2;
printf("Enter the Search Key : ");
scanf("%d",&key);
while(high>=low)
{
if(key<a[mid])
{
high=mid-1;
}
else if(key>a[mid])
{
low=mid+1;
}
else if(key==a[mid])
{
printf("Search Key %d is found in %d Position\n",key,mid+1);
break;
}
mid=(low+high)/2;
}
if(low>high)
{
printf("Search Key is not found!\n");
printf("Search is Unsuccessful!\n");
}
}
/*End of the Binary search function*/