Print 4 patterns by a number

10:58 0 Comments A+ a-

Problem:
Write a program that creates four different patterns of different sizes. The size of each pattern is determined by the number of columns or rows. For example, a pattern of size 5 has 5 columns and 5 rows. Each pattern is made of character $ and a digit, which shows the size.
The size must be between 2 and 9. E-g;

Sample Output


Enter  the size between 2 and 9  : 5

Pattern 1       Pattern 2                   Pattern 3                   Pattern 4
5$$$$             $$$$5                         $$$$$                         $$$$$
$5$$$             $$$5$                         $$$$5                         5$$$$
$$5$$             $$5$$                         $$$55                         55$$$
$$$5$             $5$$$                         $$555                         555$$
$$$$5             5$$$$                         $5555                         5555$
Solution:
# include <iostream>
using namespace std;
void first(int t, int n);
void second(int x, int n);
void third(int o, int n, int i);
void fourth(int m, int n, int i);
int main()
{
int n;
cout << "Please enter a Number (from 2-9) : ";
cin >> n;
while (n<2 || n>9)
{
cout << "Number entered is out of range" << endl;
cout << "Please enter a number again (from 2-9) : ";
cin >> n;
}
int t = 0;
int x = n - 1;
int o = n;
int m = n;
cout << endl;
cout << "Pattern 1 \t Pattern 2 \t Pattern 3 \t Pattern 4" << endl;
for (int i = 0; i < n; i++)
{
first(t, n);
cout << " \t ";
t++;
second(x, n);
x--;
cout << " \t ";
third(o, n, i);
o--;
cout << " \t ";
fourth(m, n,i);
m--;
cout << endl;
}

system("pause");
}
void first(int t, int n)
{
for (int j = 0; j<n; j++)
{
if (j == t)
{
cout << n;
continue;
}
else if (j != t)
{
cout << "#";
}
}
}
void second(int x, int n)
{
for (int j = 0; j<n; j++)
{
if (j == x)
{
cout << n;
continue;
}
else if (j != x)
{
cout << "#";
}
}

}
void third(int o, int n, int i)
{
for (int k = 0; k<o; k++)
{
cout << "#";
}
for (int j = 0; j<i; j++)
{
cout << n;
}
}
void fourth(int m, int n, int i)
{
for (int j = 0; j<i; j++)
{
cout << n;
}
for (int k = 0; k<m; k++)
{
cout << "#";
}

}