Inner Reducing Pattern printing in C++15 May 2025 | 11 min read Introduction:Pattern printing is a fundamental concept in programming that helps improve logical thinking and understanding of nested loops. One specific type of pattern is the inner reducing pattern, where the number of elements in each row decreases progressively as you move downward. In this pattern, you typically begin with a fixed number of elements in the first row. For every subsequent row, the number of elements is reduced by one. The elements themselves could be numbers, characters, or symbols, depending on the requirement. This type of pattern is popular in basic programming exercises and is often used to teach students how to manage loops and conditions effectively. What is the Inner Reducing Pattern?For example, you might want to print something like this: This pattern shows:
Steps to Create the Pattern in C++:Understand the Rows and Columns:
Use Nested Loops:
Logic for Printing Numbers:
Move to the Next Line:
Approach 1: Simple ApproachExample:Output: Enter the number of rows: 5 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Explanation:Step 1: Input from the User The program starts by asking the user to enter the number of rows (n). This input determines how many rows the pattern will have. For example, if the user enters 5, the pattern will have 5 rows. This makes the program flexible because it can generate patterns of any size based on the user's input. Step 2: Outer Loop for Rows The outer loop controls the number of rows in the pattern. It runs from i = 0 to i < n. Each time the outer loop runs, it represents one row of the pattern.
Step 3: Inner Loop for Columns The inner loop is nested inside the outer loop. Its job is to print the numbers in each row.
For example:
This is how the decreasing number of elements is managed. Step 4: Printing Numbers Inside the inner loop, each number (j) is printed. After printing each number, a space (" ") is added to separate the numbers on the same row. This ensures that the numbers are displayed side by side in a single row. Step 5: Moving to the Next Row Once all the numbers in a row are printed, the program uses cout << endl; to move to the next line. This ensures that the numbers for the next row start on a new line. Step 6: Repeating the Process The outer loop continues to run, repeating the inner loop for each row. With each new row, the number of columns decreases because of the condition j <= n - i. This is why the pattern looks like a triangle with fewer numbers in each subsequent row. Step 7: Ending the Program Once all rows are printed, the program ends. The complete pattern is displayed on the screen. Example WalkthroughLet’s say the user enters 5:
The program completes, and the final output is the full pattern. Complexity analysis:Time Complexity The time complexity of the inner reducing pattern printing depends on the number of rows (n) and how many elements are printed in total. Outer Loop: The outer loop runs n times, once for each row. Inner Loop:
The total number of iterations for the inner loop is the sum of numbers from n to 1: Total iterations = n + (n-1) + (n-2) + ... + 1 = n * (n + 1) / 2 This simplifies to O(n²) for time complexity. Space Complexity The program uses only a few variables for looping and storing input (n, i, j), so the space complexity is O(1).
Approach 2: Reverse Loop ReductionThis approach uses a reverse loop for the inner loop, starting from the maximum number for each row and decreasing toward 1. It achieves the same result but provides a different perspective on loop management. Example:Output: Enter the number of rows: 5 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Explanation:Step 1: Getting Input from the User The program begins by asking the user to enter the number of rows (n). The value of n defines how many rows will be printed. For example, if the user enters 5, the pattern will have 5 rows. Step 2: Outer Loop for Rows The outer loop runs from 1 to n, which means it will iterate from i = 1 to n.
Step 3: Inner Loop for Columns (Reverse Counting) The inner loop runs from n down to i, where n is the maximum number of elements in the current row.
Step 4: Printing the Numbers Inside the inner loop, the number (n - j + 1) is printed.
Step 5: Moving to the Next Row After printing all the numbers in a row, the program uses cout << endl; to move to the next line.
Step 6: Repeating the Process The outer loop continues from i = 2 to n, reducing the number of columns printed each time.
Step 7: Ending the Program Once all rows are printed, the program finishes, and the full pattern is displayed on the screen. Example Walkthrough with n = 5:
The program displays the entire pattern in reverse decreasing order, and this approach keeps the logic clean and efficient. Complexity analysis:Time Complexity:
Time Complexity: O(n²) Space Complexity:
Approach 3: Single Loop Reduction ApproachThe Single Loop Reduction Approach is a pattern printing technique where we use a single loop to reduce the number of elements printed in each subsequent row. Instead of printing numbers from a fixed range (like decreasing from n), we gradually decrease the range based on the current row. This approach simplifies the logic and reduces the need for nested loops, making it more efficient in terms of both time and space complexity. Example:Output: Enter the number of rows: 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Explanation:Step 1: Input from the User
Step 2: Outer Loop for Rows
Step 3: Inner Loop for Printing Numbers
Step 4: Printing Numbers
Step 5: Completing the Rows The outer loop continues reducing i from n to 1. Each iteration of i ensures that fewer numbers are printed for each row. For example:
This way, we print fewer numbers with each row. Step 6: End of the Program
Complexity Analysis:Time Complexity:
Time Complexity: O(n²) Space Complexity:
Next TopicHow to use typename Keyword in C++ |
Introduction The "" is a famous algorithmic issue that includes determining how many ways friends may be matched together for different activities while sticking to specific limitations. In this problem, we are given a group of friends and asked to determine the total number of ways they...
6 min read
In this article, we discuss the differences between Range-based For Loops and Iterator-based For Loops in C++. Before discussing their differences, we must know about the Range-based For Loops and Iterator-based For Loops in C++ with their syntax, parameters, and examples. What is the Range-based For...
6 min read
In this article, we will discuss about traits in C++. C++ trait is an interesting function and variable inside of which the features and skills of the class are created for runtime. The Characters, which are no longer a common language feature in object-oriented programming languages,...
3 min read
In this article, we will discuss the , including its syntax, examples, benefits, and many others. Introduction The problem of concurrency in C++ may be caused by the possible race conditions and deadlocks. In order to mitigate these issues, the C++ standard library provides synchronization primitives, including the...
7 min read
Introduction: Some movies have restrictions such as having age limits and even limiting the number of seats available in a movie theatre. So, on the basis of these criteria, can we figure out how many people can possibly watch the movie? We will speak about this issue...
11 min read
Recursion is one of the core concepts of computer science and programming, where a function calls the same function to solve the given problem. The approach is very effective in solving problems that can be divided into several similar problems with the same solution. Iterative...
9 min read
Introduction The std::destroy_at is a function that was included in C++17 and it is located in the memory library. It serves a specific purpose in the realm of memory management to actually obliterate an object at a specific memory address without freeing the memory space. This function...
8 min read
Introduction An incidence matrix is a fundamental data structure used in graph theory to represent the relationship between vertices and edges in a graph. In a graph, the vertices are represented by rows, and the edges by columns in the incidence matrix. Each element of the matrix...
7 min read
A "K'th Boom Number" in C++ is the K-th number in the series of numbers produced until certain requirements are satisfied, such as containing a predefined digit (like "7") or being divisible. One method would produce numbers one after the other until the condition verified...
4 min read
However, the Painting Fence Algorithm becomes an interesting and realizable problem in the universe of competitive programming and algorithm designing. The specific issue may be defined in terms of computing the number of ways to paint a fence with a fixed number of posts in a...
10 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India