Internal Varsity Number Square Printing

PRINTING PATTERN:

333

313

323

333

PREREQUISITE:

Basic knowledge of C language and loops.

ALGORITHM:

  1. Take number of rows as input from the user and save it in any variable (‘r’in this case).
  2. Run a loop ‘r’ number of times to iterate through the rows. From i=0 to i<r.  The loop should be structured as for( i=0 ; i<r ; i++).
  3. Inside this if condition use an id condition to print the top and bottom rows. if(i==0 || i==r-1).
  4. Inside this run a nested loop to iterate through the columns. From  j=0 to j<3. The loop should be structured as for(j=0 ; j<3 ; j++).
  5. Print “3” in this loop.
  6. Write an else condition. Inside it a run a similar loop . From  j=0 to j<3. The loop should be structured as for(j=0 ; j<3 ; j++).
  7. Use an if condition. if(j==0 || j==2). And  print “3”.
  8. Else print the iterating variable for rows.
  9. Inside the main loop print a newline

CODE IN C:

#include<stdio.h>
int main()
{
int i,j,r;                                          //declaring integer variables i,j for loops , r for number of rows
printf("Enter the number of rows :\n");             //asking user for input
scanf("%d",&r);                                     //saving number of rows in variable r
for(i=0;i<r;i++)                                    //loop for number of rows
   {
      if(i==0 || i==r-1)                            //if condition to print first and last row
         {
            for(j=0;j<3;j++)                        //loop to print first and last row
               {
                  printf("3");                      //printing 3
               }
         }
      else                                          //else to print rest of the square
         {
            for(j=0;j<3;j++)                        //loop to print the rest of the square
              {
                  if(j==0 || j==2)                  //if condition to print the outer values
                     {
                        printf("3");                //printing 3
                     }
                  else//else condition to
                     {
                        printf("%d",i);              //Printing the middle column
                     }
              }
         }
      printf("\n");                                  //printing newline
   }
}

TAKING INPUT:
DISPLAYING OUTPUT:

5 comments on “Internal Varsity Number Square Printing”


  • riteshraj31020

    Simplest code in c++ convert it in any other language.
    oid pattern3(int n){
    for(int i=0; i<n; ++i){
    for(int j=0; j<3; ++j){
    if(i==0 || i==n-1 ||j==0||j==2){
    cout<<"3";
    }else{
    cout<<i;
    }
    }
    cout<<endl;
    }
    }


  • Mo

    C++ Solution

    #include

    using namespace std;

    int main()
    {
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)//Loop
    {
    if(i==1||i==n)//condition checking
    {
    for(int j=1;j<n;j++)//Loop
    cout<<"3";
    }
    else
    {
    for(int j=1;j<n;j++)//Loop
    {
    if(j==1||j==3)
    cout<<"3";
    else
    cout<<i-1;
    }
    }
    cout<<endl;
    }

    return 0;
    }


  • NewTechHub

    Python 3
    n=int(input())

    for i in range(n):
    for j in range(n):
    if(i==0 or j==0 or j==n-1 or i==n-1 or j!=n//2):
    print(n,end=”)
    else:
    print(i,end=”)
    print(‘\n’)


    • NewTechHub

      n=int(input())
      if n%2!=0 and n>2:
      for i in range(1,n+2):
      for j in range(0,n):
      if(i==1 or i==n+1 or j==0 or j==n-1 or j>n//2 or j<n//2):
      print(n,end='')
      else:
      print(i-1,end='')
      print()