Friday, 27 April 2012

Printing the diagonals of a square in C



To print an X, a big one, of stars. Just swap the values in the last program. Let us see, or as Yashwant Kanitkar says, Let us C.
This program can easily be done without using the matrix. It's just that I like matrices.

#include<stdio.h>
#include<conio.h>
#define MAX 100

int main();
{
int i,j,m,sum=0,arr[MAX][MAX];
printf("Enter the dimensions of the square you want to print: \n");
scanf("%d",&m);

clrscr();

for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
sum=i+j;
if(i!=j)
{
if(sum==m-1)
{
arr[i][j]==42;
printf("%c ",arr[i][j]);
}
else
{
arr[i][j]==32;
printf("%c ",arr[i][j]);
}
}
else if(i==j)
{
arr[i][j]==42;
}
}
printf("\n");
}
getch();
return 0;
}

No comments:

Post a Comment