Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
2.1.
Program in C
2.2.
Program in C++
2.3.
Program in Java
2.4.
Program in Python
3.
Frequently Asked Questions
3.1.
What are rectangles using repetitive and descending numbers patterns?
3.2.
What is the time complexity of rectangles using repetitive and descending numbers patterns?
3.3.
Which loop in the mentioned above code for ‘rectangles using repetitive and descending numbers patterns’ in different languages represents the descending numbers?
3.4.
What do we call that concept when one loop is inside the other loop?
3.5.
What print(“\r”) is doing in the python code?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Rectangle Using Repetitive and Descending Numbers

Author Aditya kumar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @
Interview Puzzles

Introduction

With the help of different programming languages, we will discuss rectangles using repetitive and descending numbers patterns in this article. The interviewer typically asks these questions to assess the programmer's logical reasoning ability. Once we understand the idea of the code, we may create numerous rectangle patterns in C, C++JAVAPYTHONPHP, and other programming languages.

Problem Statement

Write a program for a rectangle using repetitive and descending numbers.

We have some sample inputs for this program, which are as follows:

Sample Input 1

Sample Output 1

7

7777777

6666666

5555555

4444444

3333333

2222222

1111111

Sample Input 2

Sample Output 2

4

4444

3333

2222

1111

This information is necessary for this program:

For input myInput
For output myOutput
Addition Information  We need x for the loops.

 

Program in C

The following is the source code for the C program that prints rectangles using repetitive and descending numbers:

#include < stdio.h >
int main()
{
  printf("Enter the size of the row and column:");

  int myInput, myOutput,x;
  scanf("%d",& myInput);
  for (myOutput = myInput; myOutput >= 1; myOutput--) {
    for (x= 1;x<= myInput;x++)
    printf("%d", myOutput);

    printf("\n");
  }
}

 

Output

Output

Program in C++

The following is the source code for the C++ program that prints rectangles using repetitive and descending numbers:

#include<iostream>
using namespace std;
int main()
{
  cout<<"Enter the size of the row and column:";
  int myInput,myOutput,x;
  cin>>myInput;
  for(myOutput=myInput;myOutput>=1;myOutput--)
  {
  for(x=1;x<=myInput;x++)
    cout<<myOutput;

        cout<<"\n";
  }
}

 

Output

Output

Program in Java

The following is the source code for the Java program that prints rectangles using repetitive and descending numbers:

import java.util.Scanner;
public class myClass {

 public static void main(String[] args) {
  Scanner s=new Scanner(System.in);
  System.out.println("Enter the size of the row and column:");
  int myInput,myOutput,x;
    myInput=s.nextInt();
    for(myOutput=myInput;myOutput>=1;myOutput--)
    {
    for(x=1;x<=myInput;x++)
      System.out.print(myOutput);

          System.out.println();
    }
  s.close();
}
}

 

Output

Output

Program in Python

The following is the source code for the Python program that prints rectangles using repetitive and descending numbers:

myInput = int(input("Enter the size of the row and column:"))
for myOutput in range(myInput, 0, -1):
    for x in range(0, myInput):
        print(myOutput, end="")
    print("\r")

 

Output

Output

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What are rectangles using repetitive and descending numbers patterns?

This is a simple pattern that contains numbers in the pattern of the rectangle with a repetition of numbers in descending order. 
For example,
333
222
111

What is the time complexity of rectangles using repetitive and descending numbers patterns?

The time complexity of this rectangular pattern is O(n2).

Which loop in the mentioned above code for ‘rectangles using repetitive and descending numbers patterns’ in different languages represents the descending numbers?

The loop which is showing the decreasing number for making it in descending order is the outer loop.

What do we call that concept when one loop is inside the other loop?

The term "nested loop" refers to a loop within a loop.

What print(“\r”) is doing in the python code?

“\r” moves the cursor to the beginning of the line and then continues to produce text normally.

Conclusion

In this article, we have discussed rectangles using repetitive and descending number patterns. We have seen the solution in different languages like C++JAVAPYTHON, and PHP.

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 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.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Previous article
Rectangle Pattern Using Number in Ascending Order
Next article
Rectangle Using Descending Numbers
Live masterclass