Computer Scientists are Pretty Pessimistic

Friday 4 September 2015

WAP to find out the sum of negative, positive, odd and even integers separately from a two dimensional array.

#include <stdio.h>
int main ()
{
    int a[5][5],i,j,neg=0,pos=0,odd=0,even=0;
    printf("Enter Matrix (5x5):\n");
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            if(a[i][j]<0)
                neg=neg+a[i][j];
            if(a[i][j]>=0)
                pos=pos+a[i][j];
            if(a[i][j]%2==0)
                even=even+a[i][j];
            if(a[i][j]%2!=0)
                odd=odd+a[i][j];
        }
    }
    printf("Sum of positive numbers: %d\n",pos);
    printf("Sum of negative numbers: %d\n",neg);
    printf("Sum of even numbers: %d\n",even);
    printf("Sum of odd numbers: %d\n",odd);
    return 0;
}

No comments:

Post a Comment