I need to write a program to print the below pattern of * representing the alphabet X.
I am unable to print an X using asterisks in the post.
I hope you can understand the below explanation.
The X should have 7 rows - with three rows at the top and 3 at the bottom. Each row holds 2 *.

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

6
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by freelancelote

did you do any code already?
can you post it?

I need to write a program to print the below pattern of * representing the alphabet X.
I am unable to print an X using asterisks in the post.
I hope you can understand the below explanation.
The X should have 7 rows - with three rows at the top and 3 at the bottom. Each row holds 2 *.

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

Use code tags. It preserves formatting.

[code]

// paste *'s here

[/code]

``````*           *
*       *
*   *
*
*   *
*       *
*           *``````

Each row holds 2 *.

Looks like the middle row only holds one.

You should use a nested for-loop (a for loop within another for loop).

Thanks for the prompt response... I do understand that it can be done through nested loops.. but can you specify the conditions for the FOR and the nested FOR loop.

Thanks once again.

Shakeel

The pattern is as below

``````*           *
*       *
*   *
*
*   *
*       *
*           *``````

A simple solution:

``````int draw()
{
int pat[7][7] = { 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 1, 0,
0, 0, 1, 0, 1, 0, 0,
0, 0, 0, 1, 0, 0, 0,
0, 0, 1, 0, 1, 0, 0,
0, 1, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1
};

int i, j;

for(i = 0; i < 7; i++)
{
for(j = 0; j < 7; j++)
{
if(pat[i][j] == 1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.print("\n");
}
return 0;
}``````

Well, sure, except for three things:
1) it's C and this is the Java forum
2) If you are going to hard-code the whole pattern, why would you bother to use 1 and 0 to encode it just so you can translate it back with if() statements to the '*' you want to print? You may as well just put the asterisks in an array and print it in that case.
3) I would assume figuring out the loop logic is a goal of the assignment.

hey luckychap thanx 4 da reply...bt i m still having a problem wid it....
look this is what i tried

``````class pattern
{
void disp()
{
int a[7][7]={{*,0,0,0,0,0,*},
{0,*,0,0,0,*,0},
{0,0,*,0,*,0,0},
{0,0,0,*,0,0,0},
{0,0,*,0,*,0,0},
{0,*,0,0,0,*,0},
{*,0,0,0,0,0,*}};
for(int i = 0;i<7;i++)
{
for int(j = 0; j<7;j++)
{
if(a[i][j] == '*')
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}``````

evryime i try 2 compile this program its displaying
']' expected
....i dont understand it...
what shall i do now????
n also i think ezzaral has a point when he says we can simply put the asterisks in the array n print it

There is a syntax error in the second for loop..
the opening braces should be before "int"

hey thanx....i dint notice that before but i m still facing a problem....u c
int a[7][7]={{*,0,0,0,0,0,*},
this line is getting highlighted n i m still facing the same error!!!

If this is a class assignment, I almost guarantee that you aren't supposed to use the 2-D array. Using a pre-set 2-D array is just printing it a character at a time, which doesn't demonstrate any knowledge of the pattern, which I'm sure is what the goal. Otherwise why bother with an array OR for-loops? Just print the lines.

Hi,
here is an idea: you can make a V and then use the same for loop but reversed.
Here is an idea to make a V:

``````class V {
public static void main(String[] args) {
/*define variable fields:
one to describe where to print the first star
second one to describe where to print the second star
third one to describe how many spaces you need between the two stars, which is actually the second variable minus the first one minus 2 */

/* define a for loop to loop for as high as you want your V to be.
* inside the first loop, define a second for loop to print as many blank spaces as variable one says
* variableOne++
* print a star
* inside the first loop, define a third for loop to print as many blank spaces as your third variable says
* variableThree -= 2
* println the second star
*/

}``````

It's pretty basic really and perhaps one of the senior members will help you with a snikier method.

shakeelahmed22, just another thing:
I don't think Ezzaral was in any way rude. He or she was just pointing out the obvious and repeated so many times in this forum: you'll have a lot more help if you follow a few rules as stated in the "read me first before you post" section. And if you are ready for it you'll also learn a lot more from this forum.
Just make sure you read, understand and follow the links they gave you in this and the other two or three posts you wrote at the moment.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.