Skip to content

Commit

Permalink
pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohan2309 committed Aug 29, 2021
1 parent e692b17 commit 1eb88ce
Show file tree
Hide file tree
Showing 16 changed files with 588 additions and 0 deletions.
6 changes: 6 additions & 0 deletions patterns/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"iostream": "cpp",
"ostream": "cpp"
}
}
30 changes: 30 additions & 0 deletions patterns/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* * * *
* * * *
* * * *
* * * *
* * * *
Rows 5
Cols 4
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
int rows = 5, cols = 4;
int i, j;
for (i = 0; i < rows; ++i)
{
for (j = 0; j < cols; ++j)
{
cout << "* ";
}
cout << endl;
}

return 0;
}
37 changes: 37 additions & 0 deletions patterns/10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* * * *
* * * *
* * * *
* * * *
* * * *
n 5
rows 1 to n
cols
n-i = spaces at first {space print from 1 to n-i}
then print starts 1 to n
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n = 5;
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n - i; ++j)
{
cout << " ";
}
for (j = 0; j < n; ++j)
{
cout << "* ";
}
cout << endl;
}

return 0;
}
43 changes: 43 additions & 0 deletions patterns/11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
n=5
Rows i to n (i ptr)
space will be n-i
cols 1 to i numbers
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n = 5;
int i, j;
int cnt = 0;
for (i = 1; i <= n; ++i) //for rows
{
for (j = 1; j <= n; ++j) //for cols

{

if (j <= n - i)
{
cout << " ";
}
else
{
cout << ++cnt << " ";
}
}
cnt = 0;
cout << endl;
}

return 0;
}
46 changes: 46 additions & 0 deletions patterns/12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
n=5
Rows 1 to n (i ptr)
cols has 3 parts
space -> 1 to n-i, when n =5 and row npo is 1 we have 4 spaces
nos in decreasing order n-i to 1 -> k= row number (i), print k-- till 1
nos in incre order -> let k = 2 , k++ till n
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n = 5;
int i, j;
for (i = 1; i <= n; i++) //for rows
{
for (j = 1; j <= n - i; j++) //space -> 1 to n-i, when n =5 and row npo is 1 we have 4 spaces

{
cout << " ";
}
int k = i; //nos in decreasing order n-i to n -> k= row number (i), print k-- till 1
for (; j <= n; j++) // j=n-i that is 4 to 5(n) (k is 1)
{
cout << k-- << " ";
}
k = 2; //nos in incre order -> let k = 2 , k++ till
for (; j <= n + i - 1; j++) //writing in terms of rows
{
cout << k++ << " ";
}
cout << endl;
}

return 0;
}
68 changes: 68 additions & 0 deletions patterns/13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
*
* * *
* * * * *
* * * * * * *
* * * * * * *
* * * * *
* * *
*
*
* * *
* * * * *
* * * * * * *
rows 1 to n
cols
spaces n-i
stars 2*i - 1 will give stars will give me odd nos
n=4
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n = 4;
int i, j;
int cnt = 0;
//for first half
for (i = 1; i <= n; ++i) //for rows
{

for (j = 1; j <= n - i; ++j) //for spaces

{
cout << " ";
}
for (j = 1; j <= (2 * i) - 1; ++j) //for stars (2*i - 1 will give stars will give me odd nos)

{

cout << "* ";
}
cout << endl;
}
//for 2nd half
for (i = n; i >= 1; --i) //for rows
{

for (j = 1; j <= n - i; ++j) //for spaces

{
cout << " ";
}
for (j = 1; j <= (2 * i) - 1; ++j) //for stars (2*i - 1 will give stars will give me odd nos)

{

cout << "* ";
}
cout << endl;
}
return 0;
}
42 changes: 42 additions & 0 deletions patterns/14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* *
* * * *
* * *
n=9
rows = 3
cols
chech col and row no of stars
when we add i+j we get 4,6,8,10,12
so i+j%4==0 will be acceptable for 4 and 8 and 12
but for 6 and 10
when i = 2 and j%4==0
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n = 9;
int i, j;
for (i = 1; i <= 3; ++i) //for rows
{
for (j = 1; j <= n; ++j) //for cols

{
if ((j + i) % 4 == 0 or (i == 2 and j % 4 == 0))
{
cout << "* ";
}
else
{
cout << " ";
}
}
cout << endl;
}

return 0;
}
41 changes: 41 additions & 0 deletions patterns/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* * * *
* *
* *
* *
* * * *
Rows 5
Cols 4
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
int rows = 5, cols = 4;
int i, j;
for (i = 0; i < rows; ++i)
{
for (j = 0; j < cols; ++j)
{
if (i == 0 or i == rows - 1) //when 1st row or last row
{
cout << "* ";
}
else if (j == 0 or j == cols - 1) //when 1st column or last column
{
cout << "* ";
}
else
{
cout << " ";
}
}
cout << endl;
}

return 0;
}
30 changes: 30 additions & 0 deletions patterns/3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* * * * * n=5
* * * * 4
* * * .
* * .
* 1
Rows 5
*/

#include <bits/stdc++.h>
using namespace std;

int main()
{
int n = 5;
int i, j;
for (i = n; i > 0; --i) //for rows
{
for (j = 0; j < i; ++j) //for cols
{
cout << "* ";
}
cout << endl;
}

return 0;
}
Loading

0 comments on commit 1eb88ce

Please sign in to comment.