Table of contents
1.
Introduction
2.
Approach
2.1.
Printing pattern in C
2.2.
Printing pattern in C++
2.3.
Printing pattern in Java
2.4.
Printing pattern in Python
3.
Frequently Asked Questions
3.1.
What is the time complexity of printing the above pattern of n rows?
3.2.
Which of the following programming languages is fastest- Python, C, C++, Java?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Printing Rectangle using Numbers

Author APURV RATHORE
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

Introduction

Computer programmers must possess a diverse set of abilities in order to carry out the position's various obligations with the utmost expertise. Knowledge, aptitude, and technical competency are combined with soft skills such as the ability to work as part of a team and communicate well with others by the most effective programmers. Both sorts of talents must be demonstrated by aspiring computer programmers.

This article will help you improve your technical skills. 

In this blog, we will learn how to print the following pattern in different programming languages. 

Approach

For printing the following pattern, we will use two loops. The upper loop will be to traverse through the row, and the second loop will be to traverse through the column,

1 1 1 1 1 1 
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6

Printing pattern in C

The following code illustrates how to print the above pattern in C programming language. 

#include<stdio.h>
int main()
{
  int n = 6;
  for (int i=1;i<=n;i++){
      for (int j=0;j<n;j++){
          printf("%d ",i);
      }
      printf("\n");
  }
}

 

Output:

1 1 1 1 1 1 
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6

Printing pattern in C++

The following code illustrates how to print the above pattern in C++ programming language. 

#include<iostream>
using namespace std;
int main()
{
  int n = 6;
  for (int i=1;i<=n;i++){
      for (int j=0;j<n;j++){
          cout<<i<<" ";
      }
      cout<<endl;
  }
}

 

Output:

1 1 1 1 1 1 
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6

Printing pattern in Java

The following code illustrates how to print the above pattern in Java programming language. 
 

public class A {
    public static void main(String[] args) {  
         for (int i=1;i<=6;i++){
             for (int j=0;j<6;j++){
                 System.out.print(i+" ");
             }
             System.out.println();
         }
       }

 

Output:

1 1 1 1 1 1 
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6

Printing pattern in Python

The following code illustrates how to print the above pattern in Python programming language. 

n = 6
for i in range(1,6+1):
    for j in range(6):
        print(i,end=" ")
    print()

 

Output:

1 1 1 1 1 1 
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6

Also See, Fibonacci Series in Python

Also Read - Strong number in c

Frequently Asked Questions

What is the time complexity of printing the above pattern of n rows?

The average time complexity of printing the pattern is O(n^2)

Which of the following programming languages is fastest- Python, C, C++, Java?

C is the fastest programming language among Python, C, C++, Java. 

Conclusion

In this article, we have extensively discussed printing patterns in different programming languages.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass