Practice Program







  • Program of Sorting Array in Descending 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 descending order is : \n");
        for(int i=0;i<i++)
        {
            printf("%d\t",a[i]);
        }
    }
    

    Output1
    Enter the number of elements: 5

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

    Array in descending order is :
    5     4     3     2     1


    Output2
    Enter the number of elements: 10

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

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


    Output3
    Enter the number of elements: 15

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

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










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

    void to_descending(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_descending()':
    void main()
    {
        int a[] = {1,2,3,4,5};
    
        to_descending(a,5);
    
        printf("Array in Ascending order is : \n");
        for(int i=0;i<5;i++)
        {
            printf("%d\t",a[i]);
        }
    }
    

    Output1
    Array in descending order is :
    5     4     3     2     1

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

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

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

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