C++ Exercises: Display such a pattern for n number of rows using number
47. Number Pyramid with 1 at Ends and Row Number in Middle
Write a program in C++ to display such a pattern for n number of rows using numbers. Odd numbers will appear in each row. The first and last number of each row will be 1 and the middle column will be the row number.
Sample Solution:
C++ Code :
#include <iostream> // Include the input/output stream library
using namespace std; // Using standard namespace
int main() // Main function where the execution of the program starts
{
    int i, j, n; // Declare integer variables i, j, and n
    // Display message asking for input
    cout << "\n\n Display a pattern using odd number of numbers, the first and last number of each row is 1:\n";
    cout << "-----------------------------------------------------------------------------------------------\n";
    cout << " Input number of rows: ";
    cin >> n; // Read input for the number of rows from the user
    for (i = 0; i <= n; i++) // Loop for the number of rows
    {
        /* print blank spaces */
        for (j = 1; j <= n - i; j++) // Loop to print spaces before the numbers
            cout << " "; // Print a space
        /* Display numbers in ascending order up to the middle */
        for (j = 1; j <= i; j++) // Loop to print numbers in ascending order
            cout << j; // Print the number
        /* Display numbers in reverse order after the middle */
        for (j = i - 1; j >= 1; j--) // Loop to print numbers in reverse order
            cout << j; // Print the number in reverse order
        cout << endl; // Move to the next line after each row is printed
    }
}
Sample Output:
 Display a pattern using odd number of numbers, the first and last number of each row is 1:                                                   
-----------------------------------------------------------------------------------------------                                               
 Input number of rows: 5                                               
                                                                       
    1                                                                  
   121                                                                 
  12321                                                                
 1234321                                                               
123454321 
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C++ program to print a numeric pyramid where each row starts and ends with 1, and the middle digits form an increasing sequence up to the row number.
 - Write a C++ program to generate a pyramid pattern in which the first and last numbers of each row are 1, and the central number equals the row number.
 - Write a C++ program that reads the number of rows and constructs a symmetric pyramid of numbers with 1’s at the boundaries and the row number in the center.
 - Write a C++ program to display a pyramid where the pattern in each row is 1, 2, …, n, then n-1, …, 1, ensuring the first and last digits are 1.
 
Go to:
PREV :Pascal's Triangle as Right-Angle Triangle.
 NEXT :  Alphabet Pyramid.
C++ Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
