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


No comments:

Post a Comment

If you have any doubts, please discuss here...👇