Skip to content

Commit 4f8121a

Browse files
committed
print diamond pattern of stars recursively
1 parent f66a542 commit 4f8121a

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Recursion/diamond.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void diamond(int index, int odd_number);
5+
int main()
6+
{
7+
int odd_number;
8+
cin>>odd_number;
9+
diamond(1,odd_number);
10+
cout<<endl;
11+
return 0;
12+
13+
}
14+
void diamond(int index, int odd_number)
15+
{
16+
if(index>odd_number)
17+
return;
18+
19+
for(int i=1;i<=(odd_number-index)/2;i++)
20+
cout<<" ";
21+
for(int i=1;i<=index;i++)
22+
cout<<"*";
23+
cout<<endl;
24+
25+
diamond(index+2,odd_number);
26+
27+
if(index!=odd_number)
28+
{
29+
for(int i=1;i<=(odd_number-index)/2;i++)
30+
cout<<" ";
31+
for(int i=1;i<=index;i++)
32+
cout<<"*";
33+
cout<<endl;
34+
}
35+
36+
37+
}
38+
/*
39+
40+
Test case 1:
41+
5
42+
*
43+
***
44+
*****
45+
***
46+
*
47+
48+
Test case 2:
49+
11
50+
*
51+
***
52+
*****
53+
*******
54+
*********
55+
***********
56+
*********
57+
*******
58+
*****
59+
***
60+
*
61+
62+
*/

0 commit comments

Comments
 (0)