Saturday, August 18, 2018

SUM OF ODD NUMBERS

  • Problem Description

    Write a program to find the sum of and odd 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[100],i,n,sum=0;
      scanf("%d",&n);
      for(i=0;i<n;i++)
        scanf("%d",&a[i]);
      {
        for(i=0;i<n;i++)
        {
          if(a[i]%2!=0)
            sum=sum+a[i];
        }
      }
      printf("odd=%d",sum);
      return 0;
    }
  • Test Case 1

    Input (stdin)
    5
    
    1 2 3 4 5
    
    
    Expected Output
    odd=9
  • Test Case 2

    Input (stdin)
    7
    
    1 2 3 4 5 6 7
    
    
    Expected Output
    odd=16

No comments:

Post a Comment