Wednesday, 4 September 2013

Kth SMALLEST NUMBER C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM:C program to find Kth smallest number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[20],i,j,temp,find;
clrscr();
printf("enter the no. of elemnets to be enterd");
scanf("%d",&n);
//entering data in array
 printf("enter the elements");
 for(i=0;i<n;i++)
 scanf("%d",&arr[i]);
//performing binary sort
  for(i=0;i<n;i++)
  {
  for(j=0;j<n-i;j++)
  {
  if(arr[j]>arr[j+1])
  {temp=arr[j];
  arr[j]=arr[j+1];
  arr[j+1]=temp;}}}
//input the term to be find
printf("enter the Kth minimum element to be found\n");
scanf("%d",&find);
//checking the existence of index
 if(find<0 || find >n)
 printf("index not exist");
else
//printing the output
printf("element is %d",arr[find-1]);
getch();
}
/*OUTPUT:
enter the no. of elemnts to be enterd 6
enter the elements
5  2  28  0   7  6
enter the Kth minimum element to be found
3
element is 5*/

1 comment: