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.

Pictorial Presentation:

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

Sample Solution:

C++ Code :

#include <iostream>
using namespace std;

int main()
{
    int i, j, n;
    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;
    for (i = 1; i <= n;) 
    {
        cout << i;
        for (j = i + 1; j <= n;) 
        {
            cout << j;
            j = j + 1;
        }
        cout << endl;
        i = i + 1;
    }
}

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.