w3resource

C++ Exercises: Print a pattern like highest numbers of columns appear in first row

C++ For Loop: Exercise-50 with Solution

Write a C++ program to print a pattern in which the highest number of columns appears in the first row.

Visual Presentation:

C++ Exercises: Print a pattern like highest numbers of columns appear in first row

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 the pattern like highest numbers of columns appear in first row:\n";
    cout << "------------------------------------------------------------------------------\n";
    cout << " Input the number of rows: ";
    cin >> n; // Read input for the number of rows from the user

    for (i = 1; i <= n;) // Loop for the number of rows
    {
        cout << i; // Print the current value of 'i'

        for (j = i + 1; j <= n;) // Loop to print the numbers after 'i'
        {
            cout << j; // Print the number
            j = j + 1; // Increment 'j'
        }

        cout << endl; // Move to the next line after each row is printed
        i = i + 1; // Increment 'i' for the next row
    }
}

Sample Output:

 Display the pattern like highest numbers of columns appear in first row:                                                                    
------------------------------------------------------------------------------                                                                
 Input the number of rows: 5                                           
12345                                                                  
2345                                                                   
345                                                                    
45                                                                     
5 

Flowchart:

Flowchart: Print a pattern like heighest numbers of columns appear in first row

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to print a pyramid of digits as shown below for n number of lines.
Next: Write a program in C++ to display the pattern using digits with right justified and the highest columns appears in first row.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.