Ex: Write a C program to print hollow square number pattern with diagonal. How to write a C program to print hollow square number pattern with diagonal. C program to print hollow square number pattern with diagonal.
Input from user:
Enter the number: 9
Expected output:
Step by step logic of the given program:
To understand this pattern, first you need to understand previous exercises hollow square number pattern.
1. Accept input number from user declare variable say no.
2. Run outer loop from 1 to no, which shows rows in pattern.
3.
Run inner loop from 1 to no
to print columns in pattern.
4. Inside the inner loop using if statement give conditions to print number:
if(i==1||i==no||j==1||j==no||j==ic||j==dc)
5. Otherwise print spaces.
6. Outside the block of inner for loop, print newline \n.
C program to print hollow square number pattern with diagonal:
#include<stdio.h>
void main()
{
int no,i,j;
int ic=1,dc;
printf("Enter the number:");
scanf("%d",&no);
dc=no;
for(i=1;i<=no;i++)
{
for(j=1;j<=no;j++)
{
/*Give conditions to print value of j*/ if(i==1||i==no||j==1||j==no||j==ic||j==dc)
{
printf("%d",j);
}
else
{
/*To print spaces*/
printf(" ");
}
}
ic++;
dc--;
/*for next row*/
printf("\n");
}
}
Above program shows the following output:
No comments:
Post a Comment
If you have any doubts, please discuss here...👇