Introduction
This program is very simple, and it prints a number pattern program in ascending order, and the number of digits decreases per line.This is a classic example of a nested loop, which is commonly used in programming to teach control structures and pattern logic.
Logical Complexity
Very Easy / BeginnerExplanation of the Pattern
Let’s break down how the pattern is formed row by row:Row 1: Starts at 1 and prints up to 5 → 12345
Row 2: Starts at 1 and prints up to 4 → 1234
Row 3: Starts at 1 and prints up to 3 → 123
Row 4: Starts at 1 and prints up to 2 → 12
Row 5: Starts at 1 and prints up to 1 → 1
We can do this number pattern with other loops also, but the best loop for this would be the for loop.
Loop Type | Recommended | Why |
---|---|---|
for Loop | Yes | Best when the number of iterations is known Clean and easy-to-read syntax Simple and efficient counting management |
while Loop | Not Ideal | Works but needs manual control of loop variables Less clean and readable for fixed-range loops Requires careful counter management |
do-while Loop | No | Always runs the loop at least once Can lead to unwanted extra execution in this scenario Not good when you know exactly how many times to repeat something |
Decreasing Number Pattern in C
#include // Include standard input/output library
int main() {
int n = 5; // Number of rows (can be adjusted to change pattern size)
// Outer loop controls the number of rows (starts from n down to 1)
for (int i = n; i >= 1; i--) {
// Inner loop prints numbers from 1 up to the current row number 'i'
for (int j = 1; j <= i; j++) {
printf("%d", j); // Print current number without space
}
// After printing each row, move the cursor to the next line
printf("\n");
}
return 0; // Indicate successful program termination
}
Decreasing Number Pattern in C++
#include // Include the input-output stream library
using namespace std; // Use the standard namespace to avoid std:: prefix
int main() {
int n = 5; // Number of rows (you can change this value to get a bigger or smaller pattern)
// Outer loop to handle the number of rows
// Starts from n and decreases to 1
for (int i = n; i >= 1; i--) {
// Inner loop to print numbers from 1 to the current value of 'i'
for (int j = 1; j <= i; j++) {
cout << j; // Print the current number without space
}
// After printing all numbers in the current row, move to a new line
cout << endl;
}
return 0; // Indicate that the program ended successfully
}
Decreasing Number Pattern in Python
# Number of rows
n = 5
# Outer loop for rows (starts from n down to 1)
for i in range(n, 0, -1):
# Inner loop to print numbers from 1 up to the current row number 'i'
for j in range(1, i + 1):
print(j, end='') # Print the number without a space or newline
# After printing one row, move to the next line
print()
Decreasing Number Pattern in JAVA
public class NumberPattern {
public static void main(String[] args) {
int n = 5; // Number of rows (you can change this to increase/decrease the size)
// Outer loop for rows: starts from n and goes down to 1
for (int i = n; i >= 1; i--) {
// Inner loop to print numbers from 1 to the current row number 'i'
for (int j = 1; j <= i; j++) {
System.out.print(j); // Print the current number without a space
}
// After printing each row, move to the next line
System.out.println();
}
}
}
Decreasing Number Pattern in PHP
<?php
$n = 5; // Number of rows
// Outer loop for rows (from 5 down to 1)
for ($i = $n; $i >= 1; $i--) {
for ($j = 1; $j <= $i; $j++) {
echo $j;
}
echo "\n";
}
?>
Decreasing Number Pattern in JavaScript
let n = 5; // Number of rows (you can change this value to adjust the pattern size)
// Outer loop for rows: starts from n and decreases to 1
for (let i = n; i >= 1; i--) {
let row = ""; // Initialize an empty string to build each row
// Inner loop to generate numbers from 1 to i
for (let j = 1; j <= i; j++) {
row += j; // Append the current number to the row string
}
console.log(row); // Print the constructed row to the console
}
Summary
This is a very simple beginner-level program that can easily understand nested loops and number pattern printing.The for loop is the best choice in this because of its clarity and fixed iteration control, with the outer loop for rows and the inner loop for printing numbers starting from 1.
This same logic for the loop is given in different languages like C, C++, Python, Java, PHP, and JavaScript.