Canna

Thursday, 27 September 2018

Sum of positive Numbers

  • Problem Description

    Write a program to find the sum of positive numbers in an array.

    Input Format:
    Input consists of n+1 integers. The first integer corresponds to n , the size of the array. The next ?n? integers correspond to the elements in the array. Assume that the maximum value of n is 15.

    Output Format:
    Refer sample output for details
  • Test Case 1

    Input (stdin)
    5
    
    2 3 6 8 -1
    
    
    Expected Output
    sum=19
  • Test Case 2

    Input (stdin)
    5
    
    -1 -2 -3 -4 -5
    
    
    Expected Output
    sum=0
  • Program
  • #include <stdio.h>
    int main()
    {
      int num,a[100],sum=0,i;
      scanf("%d",&num);
      for(i=0;i<num;i++)
        scanf("%d",&a[i]);
      for(i=0;i<num;i++)
      {
        if(a[i]>=0)
          {
            sum=sum+a[i];
          }
      }
      printf("sum=%d",sum);
      return 0;
    }

No comments:

Post a Comment

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