C program to accept 10 numbers and sort them in ascending order by using Array.

Shra Wan
0




This program takes 10 numbers as input from the user, stores them in an array, then sorts the array in ascending order using nested loops (Bubble Sort), and finally prints the sorted array.

 #include <stdio.h>

int main() {
    int numbers[10];
    
    // Taking input from the user
    printf("Enter 10 numbers: ");
    for (int i = 0; i < 10; i++) {
        scanf("%d", &numbers[i]);
    }

    // Sorting the array in ascending order using nested loops (Bubble Sort)
    for (int i = 0; i < 10 - 1; i++) {
        for (int j = 0; j < 10 - i - 1; j++) {
            if (numbers[j] > numbers[j + 1]) {
                // Swap if the current element is greater than the next
                int temp = numbers[j];
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = temp;
            }
        }
    }

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

    return 0;
}

Post a Comment

0Comments

Thank you very much for your comment.

Post a Comment (0)