+ 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; }

16th Oct 2018, 8:32 AM
Aisha Ali
Aisha Ali - avatar
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'.
16th Oct 2018, 9:23 AM
Paul
Paul - avatar
+ 1
16th Oct 2018, 10:09 AM
Aisha Ali
Aisha Ali - avatar
0
but if n is even then x is not look like x
17th Oct 2018, 5:26 AM
iqtiza hasan
iqtiza hasan - avatar
0
iqtiza hasan yes u r right, did you try how to solve this?
19th Oct 2018, 8:19 AM
Aisha Ali
Aisha Ali - avatar
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 ..
24th Sep 2019, 7:32 PM
Alon Reznik
Alon Reznik - avatar