Sorting array elements
in ascending order.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, temp, arr[5];
clrscr();
printf("Enter the numbers \n");
for (i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < 5; i++)
{
for (j = i + 1; j < 5; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < 5; ++i)
{
printf("%d\n", arr[i]);
}
getch();
}
Sorting array elements
in descending order.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, temp, arr[5];
clrscr();
printf("Enter the numbers \n");
for (i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < 5; i++)
{
for (j = i + 1; j < 5; j++)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < 5; ++i)
{
printf("%d\n", arr[i]);
}
getch();
}
No comments:
Post a Comment