Print square of chars

I had to make a program which prints a square like this :
****
*##*
*##*
****
if n was 4 , c was * and d was # .
I wrote it this way and it worked:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
using namespace std;

int main()
{
	int n;
	char c,d;
	cin >> n >> c >> d;
	for(int i = 1 ; i <= n ; ++i)
		cout << c;
	cout << endl;
	for(int i = 2 ; i < n ; ++i){
		cout << c;
		for(int j = 2 ; j < n ; ++j)
			cout << d;
		cout << c << endl;
	}
	for(int i = 1 ; i <= n ; ++i)
		cout << c;
	cout << endl;
	return 0;
} 


But now i have to create a program which prints this square:
*#*#*
#*#*#
*#*#*
#*#*#
*#*#*
and I don't know how to make the chars alternate .
NOTE: n here needs to be an odd number .
Last edited on
hope this helps...

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
#include <iostream>
using namespace std;
int main()
{
int n;
	char c,d;
	cin >> n >> c >> d;
for(int i=0;i<n;i++)  //number of lines
{
if(i==0 || i%2==0)//even will solve your problems
	{
	for(int j=0;j<n;j++)
	{
	if(j==0 || (j%2)==0)
	{
	cout<<c;
	continue;
	}
	else
	{
	cout<<d;
	continue;
	}

	}
cout<<endl;
	}
else
{
for (int k=0;k<n;k++)
{
if(k==0 || k%2==0)
{
cout<<d;
continue;
}
else
	{
	cout<<c;
	continue;
	}
}
cout<<endl;
}
}

return 0;
}
Last edited on
@programmer07 thank you very much !
welcome :D
Topic archived. No new replies allowed.