
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++, JAVA, PYTHON, PHP, 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
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
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
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