0

Draw this following pattern using any language using looping statement

3 3 3 3 3 3 2 2 2 3 3 2 1 2 3 3 2 2 2 3 3 3 3 3 3

12th Jun 2018, 5:00 AM
Anil Praharaj
Anil Praharaj - avatar
6 Respostas
+ 4
Pattern function in Java: static void pattern(int n) { int l, c, cl; l = n - 1; if((n & 1) != 0) { c = n / 2; cl = c; } else { c = n / 2 - 1; cl = c + 1; } for(int rows = 0; rows < n; ++rows) { for(int cols = 0; cols < n; ++cols) { if(rows != 0 && rows < l) { if(cols != 0 && cols < l) { if((rows == c || rows == cl) && (cols == c || cols == cl)) System.out.print("1 "); else System.out.print("2 "); } else System.out.print("3 "); } else System.out.print("3 "); } System.out.println(); } System.out.println("\n"); }
13th Jun 2018, 10:08 PM
Ipang
+ 6
Anil Praharaj here it is in C++: #include <iostream> void pattern(int n) { int l, c, cl; l = n - 1; if(n & 1) { c = n / 2; cl = c; } else { c = n / 2 - 1; cl = c + 1; } for(int rows = 0; rows < n; ++rows) { for(int cols = 0; cols < n; ++cols) { if(rows && rows < l) { if(cols && cols < l) { if((rows == c || rows == cl) && (cols == c || cols == cl)) std::cout << "1 "; else std::cout << "2 "; } else std::cout << "3 "; } else std::cout << "3 "; } std::cout << "\n"; } std::cout << "\n\n"; } // Main procedure int main() { int n; std::cin >> n; pattern(n); }
13th Jun 2018, 9:38 PM
Ipang
+ 3
Did you have tried to make it yourself?
12th Jun 2018, 5:31 AM
Akash Pal
Akash Pal - avatar
+ 2
Anil Praharaj add the code link to the question so you get help : )
13th Jun 2018, 7:40 AM
Ipang
+ 1
ya i have tried but but the inner part logic. its difficult to how it may work
13th Jun 2018, 7:04 AM
Anil Praharaj
Anil Praharaj - avatar
13th Jun 2018, 9:05 AM
Anil Praharaj
Anil Praharaj - avatar