Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution
3.1.
C
3.2.
C++
3.3.
Python
3.4.
Java
4.
Frequently Asked Questions
4.1.
What are rectangles using descending numbers?
4.2.
What is the time complexity for rectangles using descending numbers in C++? 
4.3.
When one loop is within the other loop, what do we call it?
4.4.
What is the purpose of print("\r") in the Python code?
4.5.
Is it possible to enter a negative number?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Rectangle Using Descending Numbers

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

Introduction

This blog will look at rectangles using descending numbers patterns using several programming languages. These questions are frequently asked by the interviewer to measure the programmer's ability to reason logically. We can generate numerous rectangular patterns in C, C++PythonJavaPHP, and other programming languages after we understand the code's concept.

Problem Statement

Write a program for rectangles using descending numbers.

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

Sample Input 1

Sample Output 1

5

54321

54321

54321

54321

54321

Sample Input 2

Sample Output 2

4

4321

4321

4321

4321

 

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

Solution

C

The source code for the C program that prints rectangles using descending numbers is as follows:

#include<stdio.h>

int main() {
    printf("Enter the row size: ");
    int row_size, output, input;
    scanf("%d", &row_size);
    for(output=row_size; output>=1; output--){
      for(input=row_size; input>=1; input--){
          printf("%d", input);
    }
    printf("\n");
  }
}

 

Output:

Output

C++

The source code for the C++ program that prints rectangles using descending numbers is as follows:

#include<iostream>
using namespace std;

int main() {
    cout<<"Enter the row size: ";
    int row_size, output, input;
    cin>>row_size;
    for(output=row_size; output>=1; output--){
      for(input=row_size; input>=1; input--){
          cout<<input;
    }
    cout<<"\n";
  }
}

 

Output:

Output

Python

The source code for the Python program that prints rectangles using descending numbers is as follows:

print("Enter the row size: ")

row_size=int(input())
for output in range(row_size, 0, -1):
    for i in range(row_size, 0, -1):
        print(i, end="")

    print("\r")

 

Output:

Output

Java

The source code for the Java program that prints rectangles using descending numbers is as follows:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner cs=new Scanner(System.in);
        System.out.println("Enter the row size:");
        int row_size, output, input;
        row_size=cs.nextInt();
        for(output=row_size; output>=1; output--) {
            for(input=row_size; input>=1; input--)
                System.out.print(input);
            
            System.out.println();
        }
        cs.close();
    }
}

 

Output:

Output

Frequently Asked Questions

What are rectangles using descending numbers?

This is a simple pattern with the digits in a row repeated in decreasing order in the rectangular pattern.
For instance, 
321
321
321

What is the time complexity for rectangles using descending numbers in C++

The time complexity for rectangles using descending numbers is O(N2) as we're iterating through two nested for loops, each of which has a range of N to 0.

When one loop is within the other loop, what do we call it?

One loop within another loop is referred to as a "nested loop".

What is the purpose of print("\r") in the Python code?

"\r" moves the cursor to the beginning of the line and then resumes normal text production.

Is it possible to enter a negative number?

No, we are not able to enter negative numbers since the number must be non-negative due to the problem constraint.

Conclusion

In this blog, we learned about the concepts of rectangles using descending numbers patterns in various languages like C, C++, Python and Java.

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.

Previous article
Rectangle Using Repetitive and Descending Numbers
Next article
Full Pyramid Pattern
Live masterclass