Back to Home & C
//Enter n numbers into an array and sort it in ascending order.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,a[20],t;
printf("Enter limit value\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("Array elements in ascending order...\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
-------------------------------------------------------------------
Output
Enter limit value
3
Enter array elements
3
1
6
Array elements in ascending order...
1
3
6
===============================================
Back to Home & C
//Enter n numbers into an array and sort it in ascending order.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,a[20],t;
printf("Enter limit value\n");
scanf("%d",&n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("Array elements in ascending order...\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
-------------------------------------------------------------------
Output
Enter limit value
3
Enter array elements
3
1
6
Array elements in ascending order...
1
3
6
===============================================
Back to Home & C