C and C++

C program to accept N numbers and arrange them in an ascending order

#include <stdio.h>

int main() {
int arr[100], n, i, j, temp;

// Accept total number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);

// Accept array elements
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Sort using Bubble Sort
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

// Display sorted array
printf("Numbers in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
Note:
  • The program uses the Bubble Sort algorithm to sort the array.