LATEST PROGRAMS

WAP in C to sort all elements in an array in ascending order.

#include<stdio.h>
#define MAX 100

void main()
{
    int arr[MAX], counter=1;
    int i, n, temp;
    printf("Enter size of array:\n");
    scanf("%d",&n);
    printf("Enter the elements of array:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("\nElements in array are:\n");
    for(i=0;i<n;i++)
    {
        printf("%d ",arr[i]);
    }
    while (counter <= n - 1)
    {
        for (i = 0; i < n - counter; i++)
        {
            if (arr[i] > arr[i + 1])
            {
                temp = arr[i + 1];
                arr[i + 1] = arr[i];
                arr[i] = temp;
            }
        }
        counter++;
    }
    printf("\n\nElements of array after sorting are:\n");
    for(int i=0;i<n;i++)
    {
        printf("%d ",arr[i]);
    }
}
22/02/2023, 1:50 am Read : 147 times