Arrange the Array Elements in Ascending Order Using Array in C


This program is a simple C program that sorts an array of integers in ascending order using the bubble sort algorithm. The program first prompts the user to enter the limit of the array, which is stored in the variable 'n'. The program then prompts the user to enter the values of the array, which are stored in the array 'a'. The program then uses nested for loops to compare each element of the array with every other element and swaps them if the current element is smaller than the other element. This process is repeated until all the elements are in ascending order. Finally, the program prints out the sorted array to the user.

Source Code

#include<stdio.h>
int main()
{
  int s=0,i,n,a[10],t=0,j;
  printf("ENTER LIMIT:");
  scanf("%d",&n);
  printf("ENTER VALUES:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
    {
      if(a[i]<a[j])
      {
        t=a[i];
        a[i]=a[j];
        a[j]=t;
      }
    }
  }
  printf("THE ASCENDING ORDER IS");
  for(i=0;i<n;i++)
  {
    printf("\n%d",a[i]);
  }
  return 0;
}
To download raw file Click Here

Output

ENTER LIMIT:5
ENTER VALUES:7
3
4
1
8
THE ASCENDING ORDER IS
1
3
4
7
8

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C