- 0
Notifications Mark All Read
- Login
- Get Prime
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.
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.
C++
Java
C++
//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;
}
Java
class Main
{
public static void main(String[] args)
{
int n=4,num=n-1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
System.out.print(num);
num++;
System.out.println();
}
num--;
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
System.out.print(num);
num--;
System.out.println();
}
}
}
// INCLUDE HEADER FILES NEEDED BY YOUR PROGRAM
// SOME LIBRARY FUNCTIONALITY MAY BE RESTRICTED
// DEFINE ANY FUNCTION NEEDED
// FUNCTION SIGNATURE BEGINS, THIS FUNCTION IS REQUIRED
#include
using namespace std;
void incrementPatternPrint(int s, int n)
{
int i=0,j=0,k=0;
int row=1;
for(i=s;in)
{
break;
}
else
{
row++;
}
for(j=1;j<row;j++)
{
cout<<i;
}
cout<=s-1;i–)
{
if(row=1;j–)
{
cout<<i;
}
cout<<endl;
}
}
// FUNCTION SIGNATURE ENDS
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))