0% found this document useful (0 votes)
5 views2 pages

Program to sort 5 elements in ascending order

it is a Program to sort 5 elements in ascending order in c langage

Uploaded by

siyil71448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Program to sort 5 elements in ascending order

it is a Program to sort 5 elements in ascending order in c langage

Uploaded by

siyil71448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Programming Fundamentals CS-121L Instructor: Dr.

Shakir Karim
Sketch and Program for Sorting the Elements of an Array in Ascending Order

Page 1 of 2
Programming Fundamentals CS-121L Instructor: Dr. Shakir Karim
Sketch and Program for Sorting the Elements of an Array in Ascending Order

// Program to sort 5 elements in ascending order


#include <stdio.h>

void main(void) {
int i, j, temp, n=5;
int a[5];

//Getting 5 elements of array from the user


for(i=0; i<n; i++) {
printf("\n Enter element_%d: ",i+1);
scanf("%d",&a[i]);
}

//Displaying 5 elements entered by the user


for(i=0; i<n; i++) {
printf("\n Element_%d: %d",i+1, a[i]);
}

//Sorting the elements of array

//Getting first element to compare with


for (i=0; i<n-1; i++) {

//Getting second element to compare with


for(j=i+1; j<n; j++) {

//comparing the elements


if(a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

//Displaying the sorted elements


for(i=0; i<n; i++) {
printf("\n\n Element_%d: %d",i+1, a[i]);
}
}

Page 2 of 2

You might also like