Friday, July 17, 2020

You are given a two-dimensional 3*3 array starting from A [0][0]. You should add the alternate elements of the array and print its sum. It should print two different numbers the first being sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 and A 0 1, A 1 0, A 1 2, A 2 1.

Question:

You are given a two-dimensional 3*3 array starting from A [0][0]. You should add the alternate elements of the array and print its sum. It should print two different numbers the first being sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 and A 0 1, A 1 0, A 1 2, A 2 1. 
 
Input Format 
 
First and only line contains the value of array separated by single space. 
 
 
Output Format 
 
First line should print sum of A 0 0, A 0 2, A 1 1, A 2 0, A 2 2 Second line should print sum of A 0 1, A 1 0, A 1 2, A 2 1 
 
SAMPLE INPUT  
 
1 2 3 4 5 6 7 8 9 
 
SAMPLE OUTPUT  
 
25 20 


Answer:

#include <stdio.h>

int main()
{
    int a[3][3], odd_sum=0,even_sum=0;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            if((i+j)%2==0)
               even_sum=even_sum+a[i][j];
            else
                odd_sum=odd_sum+a[i][j];
        }
    }
    
    printf("%d\n",even_sum);
    printf("%d\n",odd_sum);

    return 0;
}


OUTPUT:

1 2 3 4 5 6 7 8 9                                                                                          
25                                                                                                         
20  

No comments:

Post a Comment