Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

C++ Program to print pattern 2*N Number of rows

To Print 2*N Number of rows pattern

In this article we will learn to print 2*N row patterns of numbers, where we need to use many loops in C++ programming language.Here ‘i’ loop is used to access each line from 1 to N and ‘j’ loop is used to print values in each line.
Here we are given you solution to deal with this problem.

C++ Program to print pattern 2*N Number of rows

Implementation:-

  • Given input from user i.e number of lines required (2*N value).
  • Take a result variable (say ‘num’).
  • Take two loops and two subloops one for each line (say ‘i’) and other for each digit in a particular line (say ‘j’).
  • Here ‘i’ loop is used to access each line from 1 to N and ‘j’ loop is used to print values in each line.
  • Decrement the ‘a’ value at the beginning of each line to 2*N values.
  • Repeat the ‘i’ loop until it reaches ‘N’ lines.

Cpp Code:-

//C++ Program to print 2*N Number of rows pattern
#include <iostream>
using namespace std;
int main()
    {
        int n=4,num=n-1;   // Predefined input
        for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=i;j++) 
                cout<<num;
                num++;
                cout<<endl;
            }
                num--;
                for(int i=n;i>=1;i--)
                {
                    for(int j=1;j<=i;j++)
                    cout<<num;
                    num--;
                    cout<<endl;
                }
     return 0;
    }

One comment on “C++ Program to print pattern 2*N Number of rows”


  • CHINTA

    n = int(input())
    m=n-1
    for i in range(1,2*n+1):
    if i < n or i == n:
    print(i*str(m))
    m=m+1
    else:
    m = m-1
    print((2*n-i)*str(m-1))