Sunday, 14 October 2012

WAP to Find the Sum of Right Diagonal And Sum of Left Diagonal of a 3*3 array..

  •  WAP to Find the Sum of Right Diagonal And Sum of Left Diagonal.
#include <stdio.h>
#include <conio.h>
void main()
{
    int i,j,rd=0,ld=0;
    int a[3][3];
    printf("Enter the array");
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
     scanf("%d",&a[i][j]);
     }
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
        if(i==j)
        rd=rd+a[i][j];
        if(i+j==2)
        ld=ld+a[i][j];
        }
        }
        printf("\n left diagianl sum is %d",rd);
        printf("Right diagianl sum is %d",ld);
        }

2 comments:

  1. weekness result for 2nd ld right diagnol.

    ReplyDelete
  2. if(i==j)
    rd=rd+a[i][j];
    if(i==j)
    ld=ld+a[i][2-i];
    this is correct diagram for answer left and right diagnol.

    ReplyDelete