Question 69 : Write a program in Java to print the given pattern :
*****
* *
* *
* *
* *
* *
* *
* *
*****
(Spacing in Blogger is not as in Java. Note that spacing inside the hexagon is in the order : 5, 7, 11, 13, 11, 7, 5)
Java Program :
class pattern_39
{
static void print(int n)
{
int i,j,k,m=0;
for(i=1;i<n;i++)
System.out.print(" ");
for(i=1;i<=n;i++)
System.out.print("*");
System.out.print("\n");
for(i=1;i<n;i++)
{
for(j=1;j<(n-i);j++)
System.out.print(" ");
System.out.print("*");
m=(n+(i*2-1));
for(k=1;k<m;k++)
System.out.print(" ");
System.out.print("*");
System.out.print("\n");
}
m=m-1;
for(i=1;i<n-1;i++)
{
for(j=1;j<=i;j++)
System.out.print(" ");
System.out.print("*");
for(k=1;k<(m-(i*2-1));k++)
System.out.print(" ");
System.out.print("*");
System.out.print("\n");
}
for(i=1;i<n;i++)
System.out.print(" ");
for(i=1;i<=n;i++)
System.out.print("*");
}
}
*****
* *
* *
* *
* *
* *
* *
* *
*****
(Spacing in Blogger is not as in Java. Note that spacing inside the hexagon is in the order : 5, 7, 11, 13, 11, 7, 5)
Java Program :
class pattern_39
{
static void print(int n)
{
int i,j,k,m=0;
for(i=1;i<n;i++)
System.out.print(" ");
for(i=1;i<=n;i++)
System.out.print("*");
System.out.print("\n");
for(i=1;i<n;i++)
{
for(j=1;j<(n-i);j++)
System.out.print(" ");
System.out.print("*");
m=(n+(i*2-1));
for(k=1;k<m;k++)
System.out.print(" ");
System.out.print("*");
System.out.print("\n");
}
m=m-1;
for(i=1;i<n-1;i++)
{
for(j=1;j<=i;j++)
System.out.print(" ");
System.out.print("*");
for(k=1;k<(m-(i*2-1));k++)
System.out.print(" ");
System.out.print("*");
System.out.print("\n");
}
for(i=1;i<n;i++)
System.out.print(" ");
for(i=1;i<=n;i++)
System.out.print("*");
}
}
1
ReplyDelete2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Yo....
ReplyDelete< >
|
_____