Monday, June 18, 2012

To find the sum of even numbers and odd numbers in an array


/* Program to find the sum of even numbers and odd numbers in an array */

#include<stdio.h>

#include<conio.h>

void main()

{

  int arr[10],i,even_sum=0,odd_sum=0;

  clrscr();

  /* To enter elements in a 1-D array */

  printf("Enter the elements of the array:\n");

  for(i=0;i<10;i++)

  {

    scanf("%d",&arr[i]);

  }

  /* To find even or odd numbers and calculate their sum */

  for(i=0;i<10;i++)

  {

    if(arr[i]%2 == 0)

      even_sum = even_sum+arr[i];

    else

      odd_sum = odd_sum+arr[i];

  }

  printf("\nSum of even numbers in array=%d",even_sum);

  printf("\nSum of odd numbers in array=%d",odd_sum);

  getch();

}

 

OUTPUT

Enter the elements of the array:

12

15

24

22

31

13

26

27

19

33

 

Sum of even numbers in array=84

Sum of odd numbers in array=138

 

No comments:

Post a Comment

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