Canna

Thursday, 27 September 2018

Counting and Summing

  • Problem Description

    Return the count of positives numbers and the sum of negative numbers for the given array
  • Test Case 1

    Input (stdin)
    15
    
    1 2 3 4 5 6 7 8 9 10 -11 -12 -13 -14 -15
    
    
    Expected Output
    Positive Count=10
    
    Negative Sum=-65
  • Test Case 2

    Input (stdin)
    5
    
    1 2 3 4 -4
    
    
    Expected Output
    Positive Count=4
    
    Negative Sum=-4
  • Program
  • #include <stdio.h>
    int main()
    {
      int a,b,i,pos,neg;
      scanf("%d",&a);
      for(i=0;i<a;i++)
      {
        scanf("%d",&b);
        if(b>0)
          pos=pos+1;
        else
          neg=neg+b;
      }
      printf("Positive Count=%d\n",pos);
      printf("Negative Sum=%d\n",neg);
      return 0;
    }

No comments:

Post a Comment

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