In this article, we will learn how to print a diagonal pattern using numbers in C++. We will print the same number in the upper diagonal and mix numbers in the lower diagonal of the square box.
Desired Output for Size 5:
1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5
- 6 ways to Reverse Array in C++
- How to Find Max Value in C++ Array
- How to Find Minimum Value in C++ Array
- Different ways to Declare arrays in C++
- Create Pointer arrays in C++ dynamically
- Different ways to Declare arrays of strings in C++
- Loop through Array in C++
- 6 Ways to Find Size of Array in C++
- How to return an Array From a Function in C++
In this code sample, we are asking the user to enter the number of rows. When the user enters the number of rows then we are printing the pattern using the logic.
Let us understand this with the below code example.
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Please enter the size of Square Box: ";
cin>> num;
for(int row=1; row<=num; row++)
{
for(int col=1; col<=num; col++)
{
if(col <= num-row)
cout<<"1";
else
cout<<row;
}
cout<<"\n";
}
return 0;
}
return 0;
}
Actual Output:
Please enter the size of Square Box: 5
111111
111122
111333
114444
155555