Sort array in ascending and descending order

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:


No comments:

Post a Comment

x^y using recursion

Program: #include<stdio.h> int power(int a,int b); int main() { int x,y,ans; printf("Enter x and y:\n "); scanf(...