Canna

Thursday, 27 September 2018

Alternate

  • Problem Description

    There is a machine in a lab which is working not properly. It displays the alternative elements in a set of input. The lab instructor asked the students to write a C Program for this scenario. i.e. to Print the Alternate Elements in an Array
  • Test Case 1

    Input (stdin)
    5
    
    21 8 41 13 15
    
    
    Expected Output
    21 41 15
  • Test Case 2

    Input (stdin)
    9
    
    2 8 4 1 3 6 8 11 38
    
    
    Expected Output
    2 4 3 8 38
  • Program
  • #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int i,n;
        
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
        {
            
            scanf("%d",&a[i]);
        }
        printf("\n");
        for(i=0;i<n;i++)
            //printf("%d \t",a[i]);
    
        printf("\n\t");
        for(i=0;i<n;i=i+2)
           printf("%d \t",a[i]);
    
        return 0;
    }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.