Practice Program







  • Program of Sorting Array in Ascending order.
  • Solution:
    void main()
    {
        int a[10],temp,n,i,j;
    
    
        printf("Enter the number of elements: ");
        scanf("%d",&n);
    
    
        for(i=0;i<i<i++)
        {
            printf("\nEnter the element: ");
            scanf("%d",&a[i]);
        }
    
        for(i=0;i<n;i++)
        {
            printf("%d\t",a[i]);
        }
    
        printf("\n");
         for(int i=0 ; i<n ;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(a[i] > a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    
        printf("Array in Ascending order is : \n");
        for(int i=0;i<i++)
        {
            printf("%d\t",a[i]);
        }
    }
    

    Output1
    Enter the number of elements: 5

    Enter the Element: 5
    Enter the Element: 4
    Enter the Element: 3
    Enter the Element: 2
    Enter the Element: 1

    Array in Ascending order is :
    1     2     3     4     5


    Output2
    Enter the number of elements: 10

    Enter the Element: 10
    Enter the Element: 9
    Enter the Element: 8
    Enter the Element: 7
    Enter the Element: 6
    Enter the Element: 5
    Enter the Element: 4
    Enter the Element: 3
    Enter the Element: 2
    Enter the Element: 1

    Array in Ascending order is :
    1     2     3     4     5     6     7     8     9     10


    Output3
    Enter the number of elements: 15

    Enter the Element: 15
    Enter the Element: 14
    Enter the Element: 12
    Enter the Element: 12
    Enter the Element: 11
    Enter the Element: 10
    Enter the Element: 9
    Enter the Element: 8
    Enter the Element: 7
    Enter the Element: 6
    Enter the Element: 5
    Enter the Element: 4
    Enter the Element: 3
    Enter the Element: 2
    Enter the Element: 1

    Array in Ascending order is :
    1     2     3     4     5     6      7     8     9     10     11     12     13     14    15










    defining a function 'to_acending' which will sort the array in ascending order.

    void to_acending(int a[],int n)
    {   int j,i,temp;
        for(int i=0 ; i<n ;i++)
        {
            for(j=i+1;j<j++)
            {
                if(a[i] > a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }
    

    Using above define function 'to_acending()':
    void main()
    {
        int a[] = {10,9,8,7,6,5,4,3,2,1};
    
        to_acending(a,10);
    
        printf("Array in Ascending order is : \n");
        for(int i=0;i<10;i++)
        {
            printf("%d\t",a[i]);
        }
    }
    

    Output1
    Array in Ascending order is :
    1     2     3     4     5     6     7     8     9     10

    Using above define function 'to_acending()':
    void main()
    {
        int a[] = {5,4,3,2,1};
    
        to_acending(a,10);
    
        printf("Array in Ascending order is : \n");
        for(int i=0;i<10;i++)
        {
            printf("%d\t",a[i]);
        }
    }
    

    Output2
    Array in Ascending order is :
    1     2     3     4     5    

    Using above define function 'to_acending()':
    void main()
    {
        int a[] = {15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
    
        to_acending(a,15);
    
    
        for(int i=0;i<10;i++)
        {
            printf("%d\t",a[i]);
        }
    }
    

    Output3
    Array in Ascending order is :
    1     2     3     4     5     6     7     8     9     10     11    12     13     14     15