Monday, August 20, 2018

SUM OF ODD AND EVEN NUMBERS

  • Problem Description

    Write a program to find the sum of even and odd numbers in an array.
  • CODING ARENA::
  • #include <stdio.h>
    int main()
    {
      int a[50],n,i,t=0,f=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)
        {
          t=a[i]+t;
        }
        else
        {
         f=f+a[i];
        }
      }
      printf("even=%d\n",t);
      printf("odd=%d",f);

    return 0;
    }
  • Test Case 1

    Input (stdin)
    5 
    
    2 3 6 8 3
    
    
    Expected Output
    even=16
    
    odd=6
  • Test Case 2

    Input (stdin)
    4 
    
    1 3 5 7
    
    
    Expected Output
    even=0
    
    odd=16

No comments:

Post a Comment