C Program to Print Hollow Square Number Pattern with Diagonals

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:

C program to print hollow square number pattern with diagonals





  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:


C program to print hollow square number pattern with diagonals


C program to print square number pattern 4

Ex: Write a C program to print square number pattern 4. How to write a C program to print square number pattern 4. C program to print square number pattern 4.


Input from user:

Enter the number: 5


Expected output:

1    2    3    4    5

2    3    4    5    6

3    4    5    6    7

4    5    6    7    8

5    6    7    8    9



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Make N=no.


3. Run outer for loop from 1 to no, for print rows:

for(i=1;i<=no;i++)


4. Run inner for loop from z to N, to print values in incremented manner:

for(j=z;j<=N;j++)


5. Inside the inner loop print value of j with one space.


6. After that, outside the block of inner loop print new line \n [for next row].



  C program to print square number pattern:


#include<stdio.h>

void main()

{

int i,j,no;

int N,z=1;

printf("Enter the number:");

scanf("%d",&no);

N=no;

for(i=1;i<=no;i++)

{

for(j=z;j<=N;j++)

{

   /*print j with 1 space to print it like a square*/                                                 printf("%d ",j);

}

/*increment z and N by 1*/

z++;

        N++;

printf("\n");

}

}


Above program shows the following output:


C program to print square number pattern 4


C program to print square number pattern 3

Ex: Write a C program to print square number pattern 3. How to write a C program to print square number pattern 3. C program to print square number pattern 3.


Input from user:

Enter the number: 5


Expected output:

12345

12345

12345

12345

12345



Given pattern is same as previous square number pattern 2:

11111

22222

33333

44444

55555


Difference is:

In this pattern we only need to replace the value of i with value of .




Program:


#include<stdio.h>

void main()

{

int no;

int i,j;

printf("Enter the number:");

scanf("%d",&no);

/*Loop to print rows */

for(i=1;i<=no;i++)

{

          /*Loop to print columns */

for(j=1;j<=no;j++)

{

/*print value of j*/

                   printf("%d",j);

}

/*for new line*/

printf("\n");

}

}


Above program shows the following output:


C program to print square number pattern

C program to print square number pattern 2

Ex: Write a C program to print square number pattern 2. How to write a C program to print square number pattern 2. C program to print square number pattern 2.


Input from user:

Enter the number: 5


Expected output:

11111

22222

33333

44444

55555


Given pattern is same as previous square number pattern:

11111

11111

11111

11111

11111


Difference is:

In this pattern we only need to replace the 1 with value of .




Program:


#include<stdio.h>

void main()

{

int no;

int i,j;

printf("Enter the number:");

scanf("%d",&no);

/*Loop to print rows */

for(i=1;i<=no;i++)

{

          /*Loop to print columns */

for(j=1;j<=no;j++)

{

/*print value of i*/

               printf("%d",i);

}


/*for new line*/

        printf("\n");

}

}


Above program shows the following output:


C program to print square number pattern



C program to print hollow square number pattern

Ex: Write a C program to print hollow square number pattern. How to write a C program to print hollow square number pattern. C program to print hollow square number pattern. 


Input from user:

Enter the number: 5


Expected output:

22222

2       2

2       2

2       2

22222





  Step by step logic of the given program:


To understand this pattern, first you need to understand previous exercises hollow square star 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)


5. Otherwise print spaces, to make it hollow.


6. Outside the block of inner for loop, print newline \n.



Program:


#include<stdio.h>

void main()

{

int i,j,no;

printf("Enter the number:");

scanf("%d",&no);

for(i=1;i<=no;i++)

{

for(j=1;j<=no;j++)

{

/*Conditions to print number*/
                if(i==1||i==no||j==1||j==no)

{

    /*you can enter any number(symbol) here*/         
                    printf("2");

}

else

{

    /*To print spaces*/

                 printf(" ");

}

}

/*For next row*/

            printf("\n");

}

}



Above program shows the following output:


C program to print hollow square number pattern


C program to print square number pattern 1

Ex: Write a C program to print given square number pattern. How to write a C program to print given square number pattern. C program to print given square number pattern.


Input from user:

Enter the number: 5


Expected output:

11111

11111

11111

11111

11111




  Step by step logic of the given program:


To print this pattern, first you need understand square star pattern:


1. Accept input number from user declare variable say no.


2. Run outer for loop  from 1 to no to print number of rows.


3. Run inner for loop from 1 to no to print number of columns.


4. Inside the inner loop print 1.


5. For next row print new line \n.




Program:


#include<stdio.h>

void main()

{

int no;

int i,j;

printf("Enter the number:");

scanf("%d",&no);

/*Loop to print rows */

for(i=1;i<=no;i++)

{

          /*Loop to print columns */

for(j=1;j<=no;j++)

{

/*you can add any number here to print their pattern*/

                      printf("1");

}

/*for new line*/

printf("\n");

}

}



Above program shows the following output:


C program to print square number pattern 1




C Program to Print Heart Shape Star Pattern

Ex: Write a C program to print heart shape star pattern. How to write a C program to print heart shape star pattern. C program  to print heart shape star pattern.


Input from user:

Enter the number: 5

Expected output:
    **     **
  ****   ****
   *********
     *******
       *****
         ***
           *




  Step by step logic of the given program:


1. Accept input number from user.

2. To print upper part of the pattern:
  • Print left peak:
     **
   ****

  • Print right peak:
     **
   ****

3. After that print lower part means inverted triangle:
*********
  *******
    *****
      ***
        *

4. Then you will get heart shape pattern:
    **     **
  ****  ****
  *********
    *******
      *****
        ***
          *




Program:

#include<stdio.h>
void main()
{
int no,i,j,k,z;
printf("Enter the number:\n");
scanf("%d",&no);
/*To print upper part*/
z=no*2;
for(i=no/2;i<=no;i=i+2)
{
/*To print left peak */
for(j=1;j<no-i;j=j+2)
{    
                    printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
/*To print right peak*/
for(j=1;j<=no-i;j++)
{
    printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
/*To print lower part means inverted triangle*/
for(i=1;i<=no;i++)
{
for(j=1;j<i;j++)
{
printf(" ");
}
for(k=1;k<z;k++)
{
printf("*");
}
z-=2;
printf("\n");
}
}


Above program shows the following output:

C program to print heart shape star pattern, heart pattern in C



C program to print inverted half right side of pyramid star pattern

 Ex: Write a C program to print inverted half right side of pyramid star pattern. How to write a C program to print inverted half right side of pyramid star pattern. C program to print inverted half right side of pyramid star pattern.


Input from user:

Enter the number:

5


Expected output:

*****

****

***

**

*



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer for loop from 1 to no which shows rows in pattern:

for(i=1;i<=no;i++)


3. Run inner for loop from i to no which shows columns in pattern:

for(j=i;j<=no;j++)


4. Inside the inner loop print star:

printf("*");


5. For next row print \n.





Program:


#include<stdio.h>

void main()

{

int i,j,no;

printf("Enter the number:\n");

scanf("%d",&no);

for(i=1;i<=no;i++)

{

for(j=i;j<=no;j++)

{

printf("*");

}

printf("\n");

}

}



Above program shows the following output:


C program to print inverted half right side of pyramid star pattern



C program to print inverted half left side of pyramid star pattern

 Ex: Write a C program to print inverted half left side of the pyramid star pattern. How to write a C program to print inverted half left side of the pyramid star pattern. C program to print inverted half left side of the pyramid star pattern.



Input from user:

Enter the number:

5


Expected output:

*****

  ****

    ***

      **

        *



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer for loop from 1 to no which shows number of rows:

for(i=1;i<=no;i++)


3. Run inner for loop from 1 to i-1 to print spaces: 

for(j=1;j<i;j++)

{

printf(" ");

}


4. Run another one inner for loop from i to no to print star:

for(k=i;k<=no;k++)

{

printf("*");

}


5. For next row print \n.




Program:


#include<stdio.h>

void main()

{

int i,j,k,no;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops*/

for(i=1;i<=no;i++)

{

                /* Print spaces from second row, in incremented manner*/

for(j=1;j<i;j++)

{

printf(" ");

}

                /* Print stars in decremented manner*/

for(k=i;k<=no;k++)

{

printf("*");

}

                /* For next row*/

printf("\n");

}

}



Above program shows the following output:


C program to print inverted half left side of pyramid star pattern