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

# 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:

C++ Code Editor: