Diagonal Star and Zero Rectangle

Q. Write a C program to print the following diagonal star and zero rectangle structure as:

*000000
0*00000
00*0000
000*000
0000*00
00000*0
000000*

Ans.

/*c program for diagonal star and zero rectangle*/
#include<stdio.h>
int main()
{
 int rows=7,r,c;

 for(r=1; r<=rows; r++)
 {
  for(c=1; c<=rows; c++)
  {
    if( (c==1 && r==1) ||
        (c==2 && r==2) ||
        (c==3 && r==3) ||
        (c==4 && r==4) ||
        (c==5 && r==5) ||
        (c==6 && r==6) ||
        (c==7 && r==7)        
      )
         printf("*");
     else
         printf("0");
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Diagonal Star and Zero Rectangle C program
Figure: Screen shot for diagonal star and zero
 rectangle C program


Comments