Sunday, 11 December 2011

Write a program to input two dimensional array of 4*4 and find out sum of his each Row and Column and Forward & Backward sum in c

#include
#include
main()
{
int mat[4][4],a,j,i;
int rtot=0,ctot,ftot=0,btot=0;
clrscr();
for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf("Enter number:"); scanf("%d",&a); mat[i][j]=a; } } printf("\n"); for(i=0;i<4;i++) { for(j=0;j<4;j++) { printf("%d\t",mat[i][j]); rtot=rtot+mat[i][j]; } printf("ROW TOTAL:%d",rtot); rtot=0; printf("\n"); } printf("\n COLUMN TOTAL\n"); for(j=0;j<4;j++) { ctot=0; for(i=0;i<4;i++) ctot=ctot+mat[i][j]; printf("%d\t",ctot); } for(i=0;i<4;i++) ftot=ftot+mat[i][i]; printf("\n\n\nFORWARD TOTAL:%d",ftot); i=0; btot=0; for(j=3;j>=0;j--)
{
btot=btot+mat[j][j];
i++;
}
printf("\n\nBACKWARD TOTAL:%d",btot);
getch();
}


OUTPUT


Enter number = 1
Enter number = 2
Enter number = 3
Enter number = 4
Enter number = 5
Enter number = 6
Enter number = 7
Enter number = 8
Enter number = 9
Enter number = 10
Enter number = 11
Enter number = 12
Enter number = 13
Enter number = 14
Enter number = 15
Enter number = 16

1 2 3 4 ROW TOTAL = 10
5 6 7 8 ROW TOTAL = 26
9 10 11 12 ROW TOTAL = 42
13 14 15 16 ROW TOTAL = 58

COLUMN TOTAL

28 32 36 40


FORWARD TOTAL : 34


BACKWARD TOTAL : 34

No comments:

Post a Comment