Write a c program to calculate the sum of all diagonal and non diagonal elements of an matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10];
int s,i,j,r,c;
clrscr();
printf("\n enter order of matrix :");
scanf("%d%d",&r,&c);
printf("enter %d values ",r*c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("\n elements of array :");
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
}
s=0;
if(r==c)
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i+j==0 || i+j==2 || i+j==4)
s=s+a[i][j];
}
}
printf("\n sum of diagonal elements :%d",s);
}
else
printf("\n addition is not possible");
getch();
}

Comments

  1. I need a c program that count the number of same word in a sentence.

    ReplyDelete

Post a Comment