+ 3
How to draw X shape using *
I tried this code but only works when num=5: void draw_X_Shape(int n) { int i,j; for(i=0;i<5;i++) { for(j=0;j<5;j++) { if((i==j) || (i+j)==(n-1)) { printf("*"); } else printf(" "); } printf("\n"); } } int main() { int num=5; draw_X_Shape(num); return 0; }
5 Antworten
+ 6
Try it like this:
void draw_X_Shape(int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i==j) || (i+j)==(n-1))
{
printf("*");
}
else
printf(" ");
}
printf("\n");
}
}
int main()
{
int num=7;
draw_X_Shape(num);
return 0;
}
I have changed the '5' in the lines that start with for into 'n'.
+ 1
Thanks Paul Jacobs
0
but if n is even then x is not look like x
0
iqtiza hasan yes u r right, did you try how to solve this?
0
public static void Main(string[] args)
{
int n=5;
for (int i =0;i<n;i++){
for(int j=0;j<n;j++){
if (j==i||(n-j)==i){
Console.Writeline("*");
}
else
{
Console.Writeline(" ");
}
}
}
Console.Writeline("");
}
}
Probably the simplest way i could have think of ..