Monday, September 17, 2018

Sum of Negative Numbers

  • Problem Description

    Write a program to find the sum of negative 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.
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int a[50],n,i,t=0,c=0;
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
        scanf("%d",&a[i]);
        if(a[i]<0)
        {
          t=t+a[i];
        }
        else
        {
          c++;
        }
      }
      printf("sum=%d",t);
          

     return 0;
    }
  • Test Case 1

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

    Input (stdin)
    6
    
    1 2 3 4 5 6
    
    
    Expected Output
    sum=0

No comments:

Post a Comment