Wednesday, 20 November 2013

C-Program to Check and print the Prime numbers in the diagonals of a matrix

#include<stdio.h>
main()
{
int a[50][50],m,n,i,j,b[20],p[20],q=0,k,c[20],r[20],s[20];
  printf("Enter no. of rows and columns(order)\t");
  scanf("%d%d",&m,&n);
  printf("Enter the elements of matrix:\n");
  if(m==n)
  {
    for(i=0; i<m; i++)
    for(j=0; j<n; j++)
          scanf("%d",&a[i][j]);
  printf("matrix representation of above elemnts is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
for(k=2;k<a[i][j];k++)
{
if(a[i][j]%k==0)
break;
}
if(a[i][j]==k)
{
b[q]=a[i][j];
p[q]=i+1;
q++;
}
}
}
}
q=0;
for(i=0,j=n-1;i<m||j>=0;i++,j--)
{
for(k=2;k<a[i][j];k++)
if(a[i][j]%k==0)
break;
if(a[i][j]==k)
{
c[q]=a[i][j];
r[q]=i+1;
s[q]=j+1;
q++;
}
}
printf("The prime numbers in the diagonals are \n");
for(i=0;i<q;i++) 
printf("%d at the position %d%d\n",b[i],p[i],p[i]);
for(i=0;i<q;i++) 
printf("%d at the position %d%d\n",c[i],r[i],s[i]);
}  
else
  printf("This program is only applicable to square matrices!!!");
 
}



sakoncepts.blogspot.in
output




No comments:

Post a Comment