Pages

Sunday

wipro coding question

2) Program to sort array in ascending & descending order.
Input:
5
8 6 9 2 7
Output:
2 6 7 8 9
9 8 7 6 2
Program:

// C program to sort the given array elements in ascending and
//descending order
#include<stdio.h>
int main(void)
{
int arr[10], i=0, j=0, size, temp;
// Get the size of an array
scanf (“%d”, &size);
// Get the array elements as an input
for (i = 0; i <size; i++)
{
scanf (“%d”, &arr[i]);
}
// Sorting elements in ascending order
for (j=0 ; j<(size-1) ; j++)
{
for (i=0 ; i<(size-1) ; i++)
{
if (arr[i+1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
// Print the elements in ascending order
for (i=0 ; i {
printf (“%d “, arr[i]);
}
printf(“\n”);

// Print the elements in descending order
for (i=size-1; i>=0 ; i–)
{
printf (“%d “, arr[i]);
}
return 0;
}

No comments:

Post a Comment