-
Notifications
You must be signed in to change notification settings - Fork 139
/
patterns.cpp
110 lines (87 loc) · 2.31 KB
/
patterns.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int ch;
char choice;
do
{
printf("\t\t\t 1. Pascal's Triangle\n");
printf("\t\t\t 2. Floyd's Triangle\n");
printf("Select the choice of pattern: ");
scanf("%d", &ch);
//Pascal Triangle
if (ch == 1)
{
int rows, coef = 1, space, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("\n-------PASCAL'S' TRIANGLE--------\n\n");
for (i = 0; i < rows; i++)
{
for (space = 1; space <= rows - i; space++)
printf(" ");
for (j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
}
//Floyd's Triangle
else if (ch == 2)
{
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("\n-------FLOYD'S TRIANGLE--------\n\n");
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; ++j)
{
printf("%d ", number);
++number;
}
printf("\n");
}
}
else{
printf("Invalid Choice!!");
}
printf("\n\nDo you want to continue(y/Y): ");
cin>>choice;
} while(choice == 'y' || choice == 'Y');
return 0;
}
/*OUTPUT:
1. Pascal's Triangle
2. Floyd's Triangle
Select the choice of pattern: 1
Enter the number of rows: 6
-------PASCAL'S' TRIANGLE--------
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Do you want to continue(y/Y): y
1. Pascal's Triangle
2. Floyd's Triangle
Select the choice of pattern: 2
Enter the number of rows: 5
-------FLOYD'S TRIANGLE--------
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Do you want to continue(y/Y): n
--------------------------------
Process exited after 15.85 seconds with return value 0
Press any key to continue . . .*/