Write a C++ program to print same numbers in square rows and columns pattern using for loop.
#include<iostream> using namespace std; int main() { int i, j, k, rows; cout << "Enter Square Number Pattern Rows = "; cin >> rows; cout << "Print Same Numbers in Rows & Columns of a Square Pattern\n"; for(i = 1; i <= rows; i++) { for(j = i; j < rows + 1; j++) { cout << j << " "; } for(k = 1; k < i; k++) { cout << k << " "; } cout << "\n"; } return 0; }
This C++ example prints the square number pattern where rows and columns have the same numbers using a while loop.
#include<iostream> using namespace std; int main() { int i = 1, j, k, rows; cout << "Enter Square Number Pattern Rows = "; cin >> rows; cout << "Print Same Numbers in Rows & Columns of a Square Pattern\n"; while(i <= rows) { j = i; while( j < rows + 1) { cout << j << " "; j++; } k = 1; while( k < i) { cout << k << " "; k++; } cout << "\n"; i++; } return 0; }
Enter Square Number Pattern Rows = 15
Print Same Numbers in Rows & Columns of a Square Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 4 5 6 7 8 9 10 11 12 13 14 15 1
3 4 5 6 7 8 9 10 11 12 13 14 15 1 2
4 5 6 7 8 9 10 11 12 13 14 15 1 2 3
5 6 7 8 9 10 11 12 13 14 15 1 2 3 4
6 7 8 9 10 11 12 13 14 15 1 2 3 4 5
7 8 9 10 11 12 13 14 15 1 2 3 4 5 6
8 9 10 11 12 13 14 15 1 2 3 4 5 6 7
9 10 11 12 13 14 15 1 2 3 4 5 6 7 8
10 11 12 13 14 15 1 2 3 4 5 6 7 8 9
11 12 13 14 15 1 2 3 4 5 6 7 8 9 10
12 13 14 15 1 2 3 4 5 6 7 8 9 10 11
13 14 15 1 2 3 4 5 6 7 8 9 10 11 12
14 15 1 2 3 4 5 6 7 8 9 10 11 12 13
15 1 2 3 4 5 6 7 8 9 10 11 12 13 14