To draw this


Hello I'm beginner of C++ programming. I want to draw the diamond shape by using characters "*" by using Turbo C++. Can you help me?
Thanks
Last edited on
You could do a for loop, where (n=1; n!=0), then have another loop where you cout "*" for n number of times. Have a boolean that starts out true, which means that you are increasing n on each loop, and set that boolean to false when you reach the max number of "*" that you want. Then use that false boolean to start n counting backward.
What specifically is your problem? What code have you done thus far?
Hi thanks for your comment. I want to make the diamond shape by character "*" on C++.

*
***
*****
*******
**********
********
*****
***
*

Please tell me the right code. Thanks
Last edited on
maybe you want to try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
for(i = 0; i < 9; i++){
 for(j = 0; j < abs(4-i); j++){
  printf(" ");
 }
 for(j = 0; j < 1 + ((4 - abs(i - 4)) << 1); j++){
  printf("*");
 }
 for(j = 0; j < abs(4-i); j++){
  printf(" ");
 }
 printf("\n");
}
getchar();

You need to include stdio in advance.
Last edited on
Thanks jmc,
Can you help me to do by for loops?

*
***
*****
*******
**********
********
*****
***
*
Last edited on
Sry, what exactly do you want?
Don't just give him code. Like Zaita said, "what have you done to try to solve this yourself?"

You cannot learn anything unless you work your way through it yourself. And professional programmers really chafe at having to work with someone who didn't do his own homework.

To solve the problem, consider the geometric properties of the item you wish to draw. Graph paper will help. There are actually two characters you need to print: a space and an asterisk. In the following, I use a dot to represent spaces:

· · · · *
· · · * * *
· · * * * * *
· * * * * * * *
* * * * * * * * *
· * * * * * * *
· · * * * * *
· · · * * *
· · · · *

Knowing that you can do something (print a character) multiple times in a loop, think about how the number of spaces and the number of asterisks change on each line.

You will want to use "nested" loops: an outer one for each line, and one or more inner ones to print the spaces and asterisks.

Give it a go and post back if you get stuck on some specific part.
Last edited on
Dear Programmers thank you. I only used this syntax to draw it. If you can, just tell me how I can use by for loops. It's not my assignment.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

cout<<" * " <<endl;
cout<<" *** " <<endl;
cout<<" ***** " <<endl;
cout<<" ******* " <<endl;
cout<<" ********* " <<endl;
cout<<" ******* " <<endl;
cout<<" ***** " <<endl;
cout<<" *** " <<endl;
cout<<" * " <<endl;

getch();
}
That wouldn't even draw a diamond...
Here's what it would draw
*
***
****
...

You need to add spaces.

http://www.cplusplus.com/src/

look for triangle and study the source code
Han, I suggest you do the tutorials on this site. Looks like you would only need to go through them until the control structures. That's only about six. I can't see where you would need to learn anything more to do this.
Topic archived. No new replies allowed.