patterns with while loop, even odd number

Hi I had one week trying to solve this program. I need to draw a box if the number is even and a triangle for odd numbers
for example
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
     * * * *       * *     3 = *             *
4 =  *     *   2 = * *         * *      5 =  * *
     *     *                   * * *         * * *
     * * * *                                 * * * *
                                             * * * * *  
[code]                                            
my base code ( I can't change it only add code )

[code]         
 #include <iostream>
 #include <iomanip>
 #include <fstream>
 using namespace std;
 int main ()
{
int y=1;
while ( y <5)
 {
  int x =1;
  while (x<5)
   {
    cout << "* ";
    x++;;
    }
  cout << endl;
  y++;
 return 0;
} 


now this it my code
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
         
  

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
	int z;
	int ch;
	cout << "Enter a character and a number ";
	cin >> z >> ch;
	if (z % 2 == 0)
	{
		int y = 1;
		while (y < 5)
		{
			int x = 1;
			
			while (x < 5)
			{
				if (z>= x) cout << "* ";
				else if (z <= y) cout << "* ";
				x++;
				
			}
			cout << endl;;
			y++;
		}


	}
	else 
	{
		int y = 1;

		while (y < 5)
		{
			int x = 1;
		  		
			while (x < 5)
			{
				
			
			  if (x==1) cout << "* ";
			  else if (x <= z) cout << "*";
			  else if (y == 1) cout << "* ";
			  else if (y <= x) cout << " ";
			  x++;

			}
			
			cout << endl;;
			y++;
		}
	}

	return  0;
}


Last edited on
Question: Why are you asking the user to input a character?

You only need a number, and the number represents the dimensions of the square or the triangle. You draw either a square or a rectangle depending on whether the number is even or odd.

So let's do that first. Try to see if you can draw a square and a rectangle using while. ^_^
And then we need to identify what part of the while we can change so that the dimensions change according to the number we input.

You already know how to check whether the number is even ;)
Topic archived. No new replies allowed.