Program:
#include<stdio.h>
int main()
{
int a[10],i,j,n,temp;
printf("Enter no. of elements: ");
scanf("%d",&n);
printf("Enter %d numbers:\n",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])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Ascending order: \n");
for(i=0;i<n;i++)
{
printf("%4d",a[i]);
}
printf("\nDescending order: \n");
for(i=n-1;i>=0;i--)
{
printf("%4d",a[i]);
}
return 0;
}
Output:
#include<stdio.h>
int main()
{
int a[10],i,j,n,temp;
printf("Enter no. of elements: ");
scanf("%d",&n);
printf("Enter %d numbers:\n",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])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Ascending order: \n");
for(i=0;i<n;i++)
{
printf("%4d",a[i]);
}
printf("\nDescending order: \n");
for(i=n-1;i>=0;i--)
{
printf("%4d",a[i]);
}
return 0;
}
Output:
No comments:
Post a Comment