[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Diamond

Posted on 1998-02-24
Medium Priority
1,372 Views
how can I create a diamond shape using a "for" loop?
It should look like this (it should be a size of 9):

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

The faster I get the responce, the greater pts. u get
0
Question by:Dmitriy
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4

LVL 3

Author Comment

ID: 1182779
Edited text of question
0

LVL 3

Author Comment

ID: 1182780
Edited text of question
0

LVL 3

Author Comment

ID: 1182781
Edited text of question
0

LVL 3

Accepted Solution

q2guo earned 0 total points
ID: 1182782
int main()
{
int i, j;
const int SIZE=9;  // make sure SIZE is odd
int k = SIZE / 2;

for (int i=1; i <= SIZE; i++) {
for (j = 0; j < k-i+1; j++) cout << " ";
for (j = 0; j < i; j++) cout << "*";
for (j = 0; j < k-i+1; j++) cout << " ";
cout << endl;
}
}
0

LVL 3

Author Comment

ID: 7252281
I know this addition is kind of late, but I FINALY got time to update this question.  The answer proposed was NOT correct.  Here is the correct code, albeit in java:

public class Diamond
{

public static void main(String [] args)
{
boolean outer=true;
int line, star, space;
int SIZE=4;

//UPPER PART
for (line = 1; line <= SIZE; line ++)
{
for (star = 0;star < line;star ++)
{     if(outer)
{
for(space = SIZE - line;space >= 0;space --)
{     System.out.print(" ");
outer = false;
}
}
else
{     System.out.print(" ");
}
System.out.print("*");
}

System.out.println();
outer = true;
}

//LOWER PART

for (line = SIZE - 1;line > 0;line --)
{
for (star = 0;star < line;star ++)
{     if(outer)
{
for(space = line - 1;space < SIZE;space ++)
{     System.out.print(" ");
outer = false;
}
}
else
{     System.out.print(" ");
}
System.out.print("*");
}

System.out.println();
outer = true;
}
}
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month13 days, 7 hours left to enroll