w3resource

C++ Exercises: Display such a pattern for n number of rows using number

C++: For Loop Exercise-47 with Solution

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:

Flowchart: Display such a pattern for n number of rows using number

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to display Pascal's triangle like right angle triangle.
Next: Write a program in C++ to display the pattern like pyramid using the alphabet.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/for-loop/cpp-for-loop-exercise-47.php