C++ Program To Print Number Pattern
Last Updated :
23 Nov, 2022
Here, we will see a C++ program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.
3 Different Number Patterns:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
1
12
123
1234
12345
Pattern 1:
Input: n = 5
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Using for loop:
The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the number and increment the number to print the next number.
C++
// C++ program to print number patterns using for loop
#include <iostream>
using namespace std;
int main()
{
int rows, columns, number = 1, n = 5;
// first for loop is used to identify number of rows
for (rows = 0; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 0; columns < rows; columns++) {
// printing number pattern based on the number
// of columns
cout << number << " ";
// incrementing number at each column to print
// the next number
number++;
}
// print the next line for each row
cout << "\n";
}
return 0;
}
Output1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Time Complexity: O(n2)
Auxiliary Space: O(1)
Using while loop:
The while loops check the condition until the condition is false. If the condition is true then enter into the loop and execute the statements.
C++
// C++ program to print number
// patterns using while loop
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 0, n = 5;
// 1 value is assigned to the number
// helpful to print the number pattern
int number = 1;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
cout << number << " ";
// incrementing columns value
columns++;
// incrementing number value to
// print the next number
number++;
}
columns = 0;
// incrementing rows value
rows++;
cout << endl;
}
return 0;
}
Output1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Time Complexity: O(n2)
Auxiliary Space: O(1)
Pattern 2:
Input: n = 5
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Using for loop:
The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the row number to get the required output.
C++
// C++ program to print number
// patterns using for loop
#include <iostream>
using namespace std;
int main()
{
int rows, columns, n = 5;
// first for loop is used to
// identify number of rows
for (rows = 1; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 1; columns <= rows; columns++) {
// printing number pattern based on the number
// of rows
cout << rows << " ";
}
// print the next line for each row
cout << "\n";
}
return 0;
}
Output1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Time Complexity: O(n2)
Auxiliary Space: O(1)
Using while loop:
The while loops check the condition until the condition is false. If the condition is true then enter into the loop and execute the statements.
C++
// C++ program to print number
// patterns using while loop
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 0, n = 5;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
cout << rows << " ";
// incrementing columns value
columns++;
}
columns = 0;
// incrementing rows value
rows++;
cout << endl;
}
return 0;
}
Output1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Time Complexity: O(n2)
Auxiliary Space: O(1)
Pattern 3:
Input: n = 5
Output:
1
12
123
1234
12345
Using for loop:
The first for loop is used to iterate the number of rows and the second for loop is used to repeat the number of columns. Then print the column number to get the required output.
C++
// C++ program to print number
// patterns using for loop
#include <iostream>
using namespace std;
int main()
{
int rows, columns, n = 5;
// second for loop is used to identify number of rows
for (rows = 1; rows <= n; rows++) {
// second for loop is used to identify number of
// columns and here the values will be changed
// according to the first for loop
for (columns = 1; columns <= rows; columns++) {
// print the number to be print based on the
// number of columns
cout << columns << " ";
}
// print the next line
cout << "\n";
}
return 0;
}
Output1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Time Complexity: O(n2)
Auxiliary Space: O(1)
Using while loop:
The while loops check the condition until the condition is false. If condition is true then enter into loop and execute the statements.
C++
// C++ program to print number
// patterns using while loop
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 1, n = 6;
int number = 1;
// while loops check the condition and repeat
// the loop until the condition is false
while (rows <= n) {
while (columns <= rows - 1) {
// printing number to get required pattern
cout << columns << " ";
// incrementing columns value
columns++;
}
columns = 1;
// incrementing rows value
rows++;
cout << endl;
}
return 0;
}
Output1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Time Complexity: O(n2)
Auxiliary Space: O(1)
Similar Reads
C++ Program To Print Pyramid Patterns
In this article, we will discuss the following top 16 pattern programs in C++ using star ( * ), numbers or other characters. Table of ContentSimple Pyramid Pattern in C++Flipped Simple Pyramid Pattern in C++Inverted Pyramid Pattern in C++Flipped Inverted Pyramid Pattern in C++Triangle Pattern in C++
15+ min read
C++ Program To Print Right Half Pyramid Pattern
Here we will build a C++ Program To Print Right Half Pyramid Pattern with the following 2 approaches: Using for loop Using while loop Input: rows = 5 Output: * * * * * * * * * * * * * * * 1. Using for loop First for loop is used to identify the number of rows and the second for loop is used to ident
2 min read
C++ Program To Print Left Half Pyramid Pattern
Here, we will build a C++ program to print the left half of pyramid pattern using 2 approaches i.e. Using for loopUsing while loop1. Using for loop Input: rows = 5 Output: * ** *** **** ***** First, for loop is used to identify the number of rows and the second for loop is used to identify the numbe
3 min read
C++ Program To Print Triangle Pattern
Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here: Right Triangle.Inverted Right Triangle.Equilateral Triangle.Inverted Equilateral Triangle.Inverted Mirrored Right Triangle. Let's start discussing each of these in detail. 1. Right Triangle Belo
6 min read
C++ Program To Print Inverted Pyramid
Here we will build a C++ Program To Print Inverted Pyramid using 3 different approaches: Half Inverted Using "*"Half Inverted Using NumbersFull Inverted Pyramid using " * "1. Program to print inverted half Triangle using " * " Input: n=4 Output: * * * * * * * * * * As we can observe from the given e
3 min read
C++ Program To Print Number Pattern
Here, we will see a C++ program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.3 Different Number Patterns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 1 12 123 1234 12345Pattern 1:In
6 min read
C++ Program To Print Number Without Reassigning
Here, we will build a C++ program to print the number pattern without Reassigning using 2 approaches i.e. Using for loopUsing while loop1. Using for loop Input: n = 5 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The first for loop is used to iterate the number of rows and the second for loop is used
3 min read
C++ Program To Print Hollow Star Pyramid Diamond Shape Pattern
Here, we will build a C++ program to print the hollow star pyramid diamond shape pattern that can be achieved with two approaches i.e. Using for LoopUsing while loop Input: n = 5 Output: * * * * * * * * * * * * * * * *1. Using for loop C++ // C++ program to print hollow diamond pattern #include <
3 min read
C++ Program to Print Cross or X Pattern
Given a number n, we need to print an X pattern of size n. Input : n = 3Output : $ $ $ $ $Input : n = 5Output : $ $ $ $ $ $ $ $ $Input : n = 4Output : $ $ $$ $$ $ $ We need to print n rows and n columns. So we run two nested loops. The outer loop prints all rows one by one (runs for i = 1 to n). The
2 min read
C++ Program to Print the Pattern 'G"
In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program,
2 min read