0% found this document useful (0 votes)
15 views

All Pattern Program

1. The code defines patterns like square, hollow square, left triangle, and right triangle using nested for loops to print stars and spaces. 2. It takes the size of the pattern as input and uses two for loops - an outer loop to iterate through rows and an inner loop to iterate through columns or print stars/spaces. 3. Conditionals are used to print stars only at the edges of rows for hollow patterns or print increasing number of stars from left to right or right to left for triangle patterns.

Uploaded by

himanshu kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

All Pattern Program

1. The code defines patterns like square, hollow square, left triangle, and right triangle using nested for loops to print stars and spaces. 2. It takes the size of the pattern as input and uses two for loops - an outer loop to iterate through rows and an inner loop to iterate through columns or print stars/spaces. 3. Conditionals are used to print stars only at the edges of rows for hollow patterns or print increasing number of stars from left to right or right to left for triangle patterns.

Uploaded by

himanshu kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

ALL PATTERN PROGRAM

1. Square pattern in C++ 6. Right down pattern


*****
*****
****
*****
***
*****
**
*****
*
*****
7. Hollow triangle pattern
2. HOLLOW square pattern
*
*****
**
* *
**
* *
* *
* *
* *
*****
******
3. Left triangle star pattern
8. pyramid pattern
*
*
**
***
***
*****
****
*******
*****
*********
4. right triangle star pattern
9. reverse pyramid pattern
*
*********
**
*******
***
*****
****
***
*****
*
5. Left down pattern
10. hollow pyramid pattern
*****
*
****
**
***
* *
**
* *
*
********
11. Diamond start pattern 14. Right pascal star pattern
* *
*** **
***** ***
******* ****
********* *****
******* ****
***** ***
*** **
* 15. left pascal star pattern
12. Hollow star diamond *
* **
** ***
* * ****
* * *****
* * ****
* * ***
* * **
** *
*
13. hourglass pattern 16. Heart pattern
********* *** ***
******* ***** *****
***** ***********
*** *********
* ******
*** *****
***** ***
*******
********* 17. plus start pattern
*
*
*****
*
*
18. cross start pattern
* * 20. Right triangle number
**
* 1
** 12
* * 123
1234
19. Left triangle number 12345

1 21. Number pyramid pattern


12
123 1
1234 123
12345 12345
1234567
123456789

23. Hollow number pyramid


pattern

1
12
22. number pyramid reverse 1 2
system 1 2
123456789
123456789
1234567 24. Number diamond pattern
12345
123 1
1 123
12345
1234567
123456789
1234567
12345
123
1
25. Hollow number diamond 28. Hollow alphabet pyramid
pattern
1
12 A
1 2 BC
1 2 D E
1 2 F G
1 2 HIJKLMNOP
1 2
12 29. alphabet diamond pattern
1
26. Alphabet pyramid pattern A
ABC
A ABCDE
ABC ABCDEFG
ABCDE ABCDEFGHI
ABCDEFG ABCDEFG
ABCDEFGHI ABCDE
ABC
27. Reverse alphabet pyramid A
pattern
30. Hollow alphabet pattern
ABCDEFGHI
ABCDEFG A
ABCDE AB
ABC A B
A A B
A B
A B
A B
AB
A
Code in C++

1 Square pattern in C++ 2. HOLLOW square pattern


*****
*****
* *
*****
* *
*****
* *
*****
*****
*****
#include <iostream>
using namespace std;

#include <iostream>
int main() {
using namespace std;
// size of the square
int size = 5;
int main() {
// outer loop
// size of the square
for (int i = 0; i < size; i++) {
int size = 5;
// inner loop
// outer loop
for (int j = 0; j < size; j++) {
for (int i = 0; i < size; i++) {
// print only star in first and
// inner loop
last row
for (int j = 0; j < size; j++) {
if (i == 0 || i == size - 1) {
cout << "*";
cout << "*";
}
}
cout << "\n";
else {
}
// print star only at first and
return 0;
last position row
}
if (j == 0 || j == size - 1) {
cout << "*";
}
else {
cout << " ";
}
}
}
cout << "\n";
}
return 0;
}
3. Left triangle star pattern
* 4. right triangle star pattern
** *
*** **
**** ***
***** ****
*****
#include <iostream>
using namespace std; #include <iostream>
using namespace std;
int main() {
// size of the triangle int main() {
int size = 5; // size of the triangle
// loop to print the pattern int size = 5;
for (int i = 0; i < size; i++) { // loop to print the pattern
// print column for (int i = 0; i < size; i++) {
for (int j = 0; j <= i; j++) { // print spaces
cout << "*"; for (int j = 1; j < size - i; j++) {
} cout << " ";
cout << "\n"; }
} // print stars
return 0; for (int k = 0; k <= i; k++) {
} cout << "*";
}
cout << "\n";
}
return 0;
}
5. Left down pattern 6. Right down pattern
***** *****
**** ****
*** ***
** **
* *

#include <iostream>
#include <iostream> using namespace std;
using namespace std;
int main() {
int main() { // size of the triangle
// size of the triangle int size = 5;
int size = 5; for (int i = 0; i < size; i++) {
for (int i = 0; i < size; i++) { // print spaces
// print stars for (int j = 0; j < i; j++) {
for (int j = 0; j < size - i; j++) { cout << " ";
cout << "*"; }
} // print stars
cout << "\n"; for (int j = size; j > i; j--) {
} cout << "*";
return 0; }
} cout << "\n";
}
return 0;
}
7. Hollow triangle pattern 8. pyramid pattern
* *
** ***
** *****
* * *******
* * *********
******
#include <iostream>
#include <iostream> using namespace std;
using namespace std;
int main() {
int main() { int size = 5;
// size of the triangle for (int i = 0; i < size; i++) {
int size = 5; // print spaces
for (int i = 1; i <= size; i++) { for(int j = 0;j<size-i - 1; j++) {
for (int j = 0; j < i; j++) { cout << " ";
// not last row }
if (i != size) { // print stars
if (j == 0 || j == i - 1) { for(int k =0; k< 2 * i + 1; k++) {
cout << "*"; cout << "*";
} else { }
cout << " "; cout << "\n";
} }
} return 0;
// last row }
else {
cout << "*";
}
}
cout << "\n";
}
return 0;
}
9. reverse pyramid pattern
*********
*******
*****
***
*

#include <iostream>
using namespace std;

int main() {
// size of the pyramid
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < i; j++) {
cout << " "; }
// print stars
for (int k = 0; k < 2 * (size - i) - 1; k++) {
cout << "*";
}
cout << "\n";
}
return 0;
}
10. hollow pyramid pattern
*
**
* *
* *
********

#include <iostream>
using namespace std;

int main() {
// size of the pyramid
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
cout << " ";
}
// print stars
for (int k = 0; k < 2 * i + 1; k++) {
if (i == 0 || i == size - 1) {
cout << "*";
}
else {
if (k == 0 || k == 2 * i) {
cout << "*";
}
else {
cout << " ";
}
}
}
cout << "\n";
}
return 0;
}
11. Diamond start pattern 12. Hollow star diamond
* *
*** **
***** * *
******* * *
********* * *
******* * *
***** * *
*** **
* *
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main() {
int size = 5; int size = 5;
for(int i = 1; i <= size; i++) { for(int i = 1; i <= size; i++) {
for(int j = size; j > i; j--) { for (int j = size; j > i; j--) {
cout << " "; cout << " ";
} }
// printing star for(int k = 0;k<i*2- 1; k++) {
for(int k =0; k<i*2-1; k++) { if(k == 0 || k == 2*i - 2) {
cout << "*"; cout << "*";
} }
cout << "\n"; else {
} cout << " ";
// downside pyramid }
for(int i = 1; i<=size-1; i++) { }
// printing spaces cout << "\n";
for (int j = 0; j < i; j++) { }
cout << " "; for (int i = 1; i < size; i++) {
} // printing spaces
// printing star for (int j = 0; j < i; j++) {
for(int k=(size-i)*2-1;k>0; k--) { cout << " ";
cout << "*"; }
} // printing star
cout << "\n"; for(int k = (size - i) * 2 - 1; k >=1;
} k--) {
return 0; if(k == 1 || k == (size - i) * 2 - 1)
} {
cout << "*"; }
} cout << "\n";
else { }
cout << " "; // pyramid star pattern
} for (int i = 2; i <= size; i++) {
} // printing spaces
cout << "\n"; for (int j = size; j > i; j--) {
} cout << " ";
return 0; }
} // printing star
for (int k = 0; k < i * 2 - 1; k++)
{
13. hourglass pattern cout << "*";
********* }
******* cout << "\n";
***** }
*** return 0;
* }
***
*****
******* 14. Right pascal star pattern
********* *
**
#include <iostream> ***
using namespace std; ****
*****
int main() { ****
int size = 5; ***
// reversed pyramid star pattern **
for (int i = 0; i < size; i++) {
// printing spaces #include <iostream>
for (int j = 0; j < i; j++) { using namespace std;
cout << " ";
} int main() {
// printing star // right pasal triangle
for (int k = 0; k < (size - i) * 2 - int size = 5;
1; k++) {
cout << "*"; for (int i = 1; i <= size; i++) {
for (int j = 0; j < i; j++) { for (int k = 0; k < i; k++) {
cout << "*"; cout << "*";
} }
cout << "\n"; cout << "\n";
} }
for (int i = 1; i <= size - 1; i++) {
for (int i = 1; i <= size - 1; i++) { for (int j = 0; j < i; j++) {
for (int j = 0; j < size - i; j++) { cout << " ";
cout << "*"; }
} for (int k = 0; k < size - i; k++) {
cout << "\n"; cout << "*";
} }
return 0; cout << "\n";
} }
return 0;
}
15. left pascal star pattern
*
** 16. Heart pattern
*** *** ***
**** ***** *****
***** ***********
**** *********
*** *******
** *****
* ***

#include <iostream> #include <iostream>


using namespace std; using namespace std;

int main() { int main() {


// left pasal triangle // heart star pattern
int size = 5; int size = 6;

for (int i = 1; i <= size; i++) { for (int i = size / 2; i < size; i += 2)
for (int j = 0; j < size - i; j++) { {
cout << " "; // print first spaces
}
for (int j = 1; j < size - i; j += 2) 17. plus start pattern
{ *
cout << " "; *
} *****
// print first stars *
for (int j = 1; j < i + 1; j++) { *
cout << "*";
} #include <iostream>
// print second spaces using namespace std;
for (int j = 1; j < size - i + 1;
j++) { int main() {
cout << " "; // size of plus, use odd number
} int size = 5;
// print second stars
for (int j = 1; j < i + 1; j++) { for (int i = 0; i < size; i++) {
cout << "*"; for (int j = 0; j < size; j++) {
} // print only stars in middle
cout << "\n"; row
} if (i == size / 2) {
// lower part cout << "*";
// inverted pyramid }
for (int i = size; i > 0; i--) { // other than middle row,
for (int j = 0; j < size - i; j++) { print star only at index size/2
cout << " "; else {
} if (j == size / 2) {
for (int j = 1; j < i * 2; j++) { cout << "*";
cout << "*"; } else {
} cout << " ";
cout << "\n"; }
} }
return 0; }
} cout << "\n";
}
return 0;
}
18. cross start pattern
* *
**
*
**
* *

#include <iostream>
using namespace std;

int main() {
// size of cross, use odd number
int size = 5;

for (int i = 0; i < size; i++) {


for (int j = 0; j < size; j++) {
if (i==j || i+j==size-1) {
cout << "*";
} else {
cout << " ";
}
}
cout << "\n";
}
return 0;
}
19. Left triangle number

1
12
123
1234
12345

#include <iostream>
using namespace std;

int main() {

int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i++) {
// print column
for (int j = 0; j <= i; j++) {
cout << (j+1);
}
cout << "\n";
}
return 0;
}
20. Right triangle number

1
12
123
1234
12345

#include <iostream>
using namespace std;

int main() {

int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 1; j < size - i; j++) {
cout << " ";
}
// print number
for (int k = 0; k <= i; k++) {
cout << (k+1);
}
cout << "\n";
}
return 0;
}
21. Number pyramid pattern

1
123
12345
1234567
123456789

#include <iostream>
using namespace std;

int main() {

int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
cout << " ";
}
// print number
for (int k = 0; k < 2 * i + 1; k++) {
cout << (k+1);
}
cout << "\n";
}
return 0;
}
22. number pyramid reverse system

123456789
1234567
12345
123
1

#include <iostream>
using namespace std;

int main() {

int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < i; j++) {
cout << " ";
}
// print number
for (int k = 0; k < 2 * (size - i) - 1; k++) {
cout << (k+1);
}
cout << "\n";
}
return 0;
}
23. Hollow number pyramid pattern

1
12
1 2
1 2
123456789

#include <iostream>
using namespace std;

int main() {

int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
cout << " ";
}
// print number
int num = 1;
for (int k = 0; k < 2 * i + 1; k++) {
if (i == 0 || i == size - 1) {
cout << num++;
} else {
if (k == 0 || k == 2 * i) {
cout << num++;
} else {
cout << " ";
}
}
}
cout << "\n";
}
return 0;
}
24. Number diamond pattern

1
123
12345
1234567
123456789
1234567
12345
123
1

#include <iostream>
using namespace std;

int main() {
int size = 5;
int num = 1;
// upside pyramid
for (int i = 1; i <= size; i++) {
// printing spaces
for (int j = size; j > i; j--) {
cout << " ";
}
// printing number
for (int k = 0; k < i * 2 - 1; k++) {
cout << num++;
}
// set the number to 1
num = 1;
cout << "\n";
}
// downside pyramid
for (int i = 1; i <= size - 1; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
cout << " ";
}
// printing number
for (int k = (size - i) * 2 - 1; k > 0; k--) {
cout << num++;
}
// set num to 1
num = 1;
cout << "\n";
}
return 0;
}

25. Hollow number diamond

1
12
1 2
1 2
1 2
1 2
1 2
12
1

#include <iostream>
using namespace std;

int main() {
int size = 5, num = 1;
// upside pyramid
for (int i = 1; i <= size; i++) {
// printing spaces
for (int j = size; j > i; j--) {
cout << " ";
}
// printing number
for (int k = 0; k < i * 2 - 1; k++) {
if (k == 0 || k == 2 * i - 2) {
cout << num++;
} else {
cout << " ";
}
}
// set the number to 1
num = 1;
cout << "\n";
}
// downside triangle
for (int i = 1; i < size; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
cout << " ";
}
// printing number
for (int k = (size - i) * 2 - 1; k >= 1; k--) {
if (k == 1 || k == (size - i) * 2 - 1) {
cout << num++;
} else {
cout << " ";
}
}
// set the number to 1
num = 1;
cout << "\n";
}
return 0;
}
26. Alphabet pyramid pattern

A
ABC
ABCDE
ABCDEFG
ABCDEFGHI

#include <iostream>
using namespace std;

int main() {

int size = 5, alpha = 65;


for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
cout << " ";
}
// print alphabets
for (int k = 0; k < 2 * i + 1; k++) {
cout << ((char)(alpha+k));
}
cout << "\n";
}
return 0;
}
27. Reverse alphabet pyramid pattern

ABCDEFGHI
ABCDEFG
ABCDE
ABC
A

#include <iostream>
using namespace std;

int main() {
// size of the square
int size = 5, alpha = 65;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < i; j++) {
cout << " ";
}
// print alphabets
for (int k = 0; k < 2 * (size - i) - 1; k++) {
cout << ((char) (alpha + k));
}
cout << "\n";
}
return 0;
}
28. Hollow alphabet pyramid pattern

A
BC
D E
F G
HIJKLMNOP

#include <iostream>
using namespace std;

int main() {

int size = 5, alpha = 65, num = 0;


for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
cout << " ";
}
// print alphabets
for (int k = 0; k < 2 * i + 1; k++) {
if (i == 0 || i == size - 1) {
cout << ((char)(alpha+num++));
} else {
if (k == 0 || k == 2 * i) {
cout << ((char)(alpha + num++));
} else {
cout << " ";
}
}
}
cout << "\n";
}
return 0;
}
29. alphabet diamond pattern

A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A

#include <iostream>
using namespace std;
int main() {
int size = 5;
int alpha = 65;
int num = 0;
for (int i = 1; i <= size; i++) {
for (int j = size; j > i; j--) {
cout << " ";
}
// printing alphabets
for (int k = 0; k < i * 2 - 1; k++) {
cout << ((char)(alpha+num++));
}
// set the number to 0
num = 0;
cout << "\n";
}
// downside pyramid
for (int i = 1; i <= size - 1; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
cout << " ";
}
// printing alphabets
for (int k = (size - i) * 2 - 1; k > 0; k--) {
cout << ((char)(alpha+num++));
}
// set num to 0
num = 0;
cout << "\n";
}
return 0;
}

30. Hollow alphabet pattern

A
AB
A B
A B
A B
A B
A B
AB
A

#include <iostream>
using namespace std;

int main() {
int size = 5;
int alpha = 65;
int num = 0;
// upside pyramid
for (int i = 1; i <= size; i++) {
// printing spaces
for (int j = size; j > i; j--) {
cout << " ";
}
// printing alphabets
for (int k = 0; k < i * 2 - 1; k++) {
if (k == 0 || k == 2 * i - 2) {
cout << ((char)(alpha+num++));
} else {
cout << " ";
}
}
// set the number to 0
num = 0;
cout << "\n";
}
// downside triangle
for (int i = 1; i < size; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
cout << " ";
}
// printing alphabets
for (int k = (size - i) * 2 - 1; k >= 1; k--) {
if (k == 1 || k == (size - i) * 2 - 1) {
cout << ((char)(alpha+num++));
} else {
cout << " ";
}
}
// set the number to 0
num = 0;
cout << "\n";
}
return 0;
}

You might also like