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
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++
#include <iostream>
using namespace std;
int main()
{
int rows, columns, number = 1, n = 5;
for (rows = 0; rows <= n; rows++) {
for (columns = 0; columns < rows; columns++) {
cout << number << " " ;
number++;
}
cout << "\n" ;
}
return 0;
}
|
Output
1
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++
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 0, n = 5;
int number = 1;
while (rows <= n) {
while (columns <= rows - 1) {
cout << number << " " ;
columns++;
number++;
}
columns = 0;
rows++;
cout << endl;
}
return 0;
}
|
Output
1
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++
#include <iostream>
using namespace std;
int main()
{
int rows, columns, n = 5;
for (rows = 1; rows <= n; rows++) {
for (columns = 1; columns <= rows; columns++) {
cout << rows << " " ;
}
cout << "\n" ;
}
return 0;
}
|
Output
1
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++
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 0, n = 5;
while (rows <= n) {
while (columns <= rows - 1) {
cout << rows << " " ;
columns++;
}
columns = 0;
rows++;
cout << endl;
}
return 0;
}
|
Output
1
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++
#include <iostream>
using namespace std;
int main()
{
int rows, columns, n = 5;
for (rows = 1; rows <= n; rows++) {
for (columns = 1; columns <= rows; columns++) {
cout << columns << " " ;
}
cout << "\n" ;
}
return 0;
}
|
Output
1
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++
#include <iostream>
using namespace std;
int main()
{
int rows = 1, columns = 1, n = 6;
int number = 1;
while (rows <= n) {
while (columns <= rows - 1) {
cout << columns << " " ;
columns++;
}
columns = 1;
rows++;
cout << endl;
}
return 0;
}
|
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Time Complexity: O(n2)
Auxiliary Space: O(1)
Last Updated :
23 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...