C Exercises

Apr 24, 2020

C program to print given data in ascending order

Ex: What is the logic to print given number in ascending order. How to print given array elements in ascending order. C program to print given  array elements in ascending order.



Program:

#include<stdio.h>
void main()
{
int a[]={5,2,7,1,9,10}; 
int i,j,temp=0;
/*Find size of array a*/
  int length=sizeof(a)/sizeof(a[0]);
//Run loops
for(i=0;i<length;i++)
{
for(j=i+1;j<length;j++)
{
if(a[i]>a[j])
{
  /*Use swaping logic*/
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Data in asending order is = ");
for(i=0;i<length;i++)
printf("%d ",a[i]);
}



Above program shows the following output:




No comments:

Post a Comment

If you have any doubts, please discuss here...