The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Given the number of rows, the task is to print Square Pattern with Numbers in C, C++, and Python
Examples:
Example1:
Input:
Given number of rows = 9
Output:
1 2 3 4 5 6 7 8 9 2 2 3 4 5 6 7 8 9 3 3 3 4 5 6 7 8 9 4 4 4 4 5 6 7 8 9 5 5 5 5 5 6 7 8 9 6 6 6 6 6 6 7 8 9 7 7 7 7 7 7 7 8 9 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
Example2:
Input:
Given number of rows = 6
Output:
1 2 3 4 5 6 2 2 3 4 5 6 3 3 3 4 5 6 4 4 4 4 5 6 5 5 5 5 5 6 6 6 6 6 6 6
- Python Program to Print Number Reduction Pattern
- Python Program to Print Right Triangle Number Pattern
- Python Program to Print Reverse Number Pattern
Program to Print Square Pattern with Numbers in C, C++, and Python
Below are the ways to print Square Pattern with Numbers in C, C++, and Python
Method #1: Using For Loop (Static Input)
Approach:
- Give the number of rows as static input and store it in a variable.
- Loop from 1 to the number of rows using For loop.
- Loop from 1 to the number of rows using another for loop(Nested For loop).
- Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of theFor Loop using the If conditional Statement.
- If it is true then print the iterator value of the parent For loop.
- Else print the iterator value of the inner For loop.
- Print the Newline character after the end of the inner loop.
- The Exit of the Program.
1) Python Implementation
Below is the implementation:
# Give the number of rows as static input and store it in a variable. numbrrows = 9 # Loop from 1 to the number of rows using For loop. for m in range(1, numbrrows+1): # Loop from 1 to the number of rows using another for loop(Nested For loop). for n in range(1, numbrrows+1): ''' Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of the For Loop using the If conditional Statement.''' if(n <= m): # If it is true then print the iterator value of the parent For loop. print(m, end=' ') else: print(n, end=' ') # Print the Newline character after the end of the inner loop. print()
Output:
1 2 3 4 5 6 7 8 9 2 2 3 4 5 6 7 8 9 3 3 3 4 5 6 7 8 9 4 4 4 4 5 6 7 8 9 5 5 5 5 5 6 7 8 9 6 6 6 6 6 6 7 8 9 7 7 7 7 7 7 7 8 9 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9
2) C++ Implementation
Below is the implementation:
#include <iostream> using namespace std; int main() { // Give the number of rows as static input and store it // in a variable. int numbrrows = 5; // Loop from 1 to the number of rows using For loop. for (int m = 1; m <= numbrrows; m++) { // Loop from 1 to the number of rows using another // for loop(Nested For loop). for (int n = 1; n <= numbrrows; n++) { /*Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of the For Loop using the If conditional Statement.*/ if (n <= m) // If it is true then print the iterator // value of the parent For loop. cout << m << " "; else cout << n << " "; } // Print the Newline character after the end of the // inner loop. cout << endl; } return 0; }
Output:
1 2 3 4 5 2 2 3 4 5 3 3 3 4 5 4 4 4 4 5 5 5 5 5 5
3) C Implementation
Below is the implementation:
#include <stdio.h> int main() { // Give the number of rows as static input and store it // in a variable. int numbrrows = 3; // Loop from 1 to the number of rows using For loop. for (int m = 1; m <= numbrrows; m++) { // Loop from 1 to the number of rows using another // for loop(Nested For loop). for (int n = 1; n <= numbrrows; n++) { /*Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of the For Loop using the If conditional Statement.*/ if (n <= m) // If it is true then print the iterator // value of the parent For loop. printf("%d ", m); else printf("%d ", n); } // Print the Newline character after the end of the // inner loop. printf("\n"); } return 0; }
Output:
1 2 3 2 2 3 3 3 3
Method #2: Using For Loop (User Input)
Approach:
- Give the number of rows as user input and store it in a variable.
- Loop from 1 to the number of rows using For loop.
- Loop from 1 to the number of rows using another for loop(Nested For loop).
- Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of theFor Loop using the If conditional Statement.
- If it is true then print the iterator value of the parent For loop.
- Else print the iterator value of the inner For loop.
- Print the Newline character after the end of the inner loop.
- The Exit of the Program.
1) Python Implementation
Give the number of rows as user input using int(input()) and store it in a variable.
Below is the implementation:
# Give the number of rows as user input using int(input()) and store it in a variable. numbrrows = int(input('Enter some random number of rows = ')) # Loop from 1 to the number of rows using For loop. for m in range(1, numbrrows+1): # Loop from 1 to the number of rows using another for loop(Nested For loop). for n in range(1, numbrrows+1): ''' Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of the For Loop using the If conditional Statement.''' if(n <= m): # If it is true then print the iterator value of the parent For loop. print(m, end=' ') else: print(n, end=' ') # Print the Newline character after the end of the inner loop. print()
Output:
Enter some random number of rows = 7 1 2 3 4 5 6 7 2 2 3 4 5 6 7 3 3 3 4 5 6 7 4 4 4 4 5 6 7 5 5 5 5 5 6 7 6 6 6 6 6 6 7 7 7 7 7 7 7 7
2) C++ Implementation
Give the number of rows as user input using cin and store it in a variable.
Below is the implementation:
#include <iostream> using namespace std; int main() { // Give the number of rows as user input using // int(input()) and store it in a variable. int numbrrows; cin >> numbrrows; // Loop from 1 to the number of rows using For loop. for (int m = 1; m <= numbrrows; m++) { // Loop from 1 to the number of rows using another // for loop(Nested For loop). for (int n = 1; n <= numbrrows; n++) { /*Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of the For Loop using the If conditional Statement.*/ if (n <= m) // If it is true then print the iterator // value of the parent For loop. cout << m << " "; else cout << n << " "; } // Print the Newline character after the end of the // inner loop. cout << endl; } return 0; }
Output:
4 1 2 3 4 2 2 3 4 3 3 3 4 4 4 4 4
3) C Implementation
Give the number of rows as user input using scanf and store it in a variable.
Below is the implementation:
#include <math.h> #include <stdio.h> int main() { // Give the number of rows as user input using scanf and // store it in a variable. int numbrrows; scanf("%d", &numbrrows); // Loop from 1 to the number of rows using For loop. for (int m = 1; m <= numbrrows; m++) { // Loop from 1 to the number of rows using another // for loop(Nested For loop). for (int n = 1; n <= numbrrows; n++) { /*Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of the For Loop using the If conditional Statement.*/ if (n <= m) // If it is true then print the iterator // value of the parent For loop. printf("%d ", m); else printf("%d ", n); } // Print the Newline character after the end of the // inner loop. printf("\n"); } return 0; }
Output:
6 1 2 3 4 5 6 2 2 3 4 5 6 3 3 3 4 5 6 4 4 4 4 5 6 5 5 5 5 5 6 6 6 6 6 6 6
Related Programs:
- python program to print pattern with a combination of numbers and stars
- python program to print the natural numbers summation pattern
- python program to print square number pattern
- python program to print square star pattern
- python program to print hollow square star pattern
- python program to print hollow square star with diagonals
- python program to print hollow box pattern of numbers