• Tuesday, 28 February 2017

    Swap numbers which are multiples of 10 with the next number


    Question:

    Swap two numbers in a given array in such a way that the elements which are multiples of 10 are swapped with the next number.

    Input:

        6
        91 54 50 22 30 59
    

    Output:

         91  54  22  50  59  30
    

    Explanation:

        Swap 50 and 22
    
        91  54  22  50  30  59
    
        Swap 30 and 59
    
        91  54  22  50  30  59
    



    Code:

    #include<stdio.h>
    int printer(int arr[],int n)
    {
     int i;
     for(i=0;i<n;i++)
     {
      printf("%d  ",arr[i]);
     }
     printf("\n");
    }
    int func(int arr[],int n)
    {
     int i,temp;
     for(i=0;i<n-1;i++)
     {
      if(arr[i]%10 == 0)
      {
       temp=arr[i];
       arr[i]=arr[i+1];
       arr[i+1]=temp;
       i++;
       /*
       Without i++
       91  54  50  22  30  59
       91  54  22  30  59  50
    
       With i++
       91  54  50  22  30  59
       91  54  22  50  59  30
       */
      }
     }
    }
    int main()
    {
     int n;
     scanf("%d",&n);
     int i;
     int a[n];
     for(i=0;i<n;i++)
     {
      scanf("%d",&a[i]);
     }
     printer(a,n);
     func(a,n);
     printer(a,n);
    }
    




    If you like our blog.. Please share ....

    Thanks.... :)

    No comments:

    Post a Comment

    Follow Us on Facebook: