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
6 Answers
+ 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");
}
+ 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);
}
+ 3
Did you have tried to make it yourself?
+ 2
Anil Praharaj add the code link to the question so you get help : )
+ 1
ya i have tried but but the inner part logic. its difficult to how it may work