write a program in Java to print the following pattern
Answers
{
public static void main(String args[])
{
int i, j, space = 1;
space = 5 - 1;
for (j = 1; j <= 5; j++)
{
for (i = 1; i <= space; i++)
{
System.out.print(" ");
}
space--; for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("a");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= 5 - 1; j++)
{
for (i = 1; i <= space; i++)
{ System.out.print(" ");
}
space++; for (i = 1; i <= 2 * (5 - j) - 1; i++)
{
System.out.print("a");
}
System.out.println("");
}
}
}
________________________________
class b2
{
static void main ( int n )
{
for (int i=0; i<n; i++)
{
// inner loop to handle number spaces
// values changing acc. to requirement
for (int j=n-i; j>1; j--)
{
// printing spaces
System.out.print(" ");
}
// inner loop to handle number of columns
// values changing acc. to outer loop
for (int k=0; k<=i; k++ )
{
System.out.print(" a ");
}
// ending line after each row
System.out.println();
}
}
// For the first triangle shaped that is :-
a
a a
a a a
a a a a
a a a a a
// For the reversed one :-
class b3
{
public static void main ( int n)
{
for (int i=0; i<n; i++)
{
for (int j=n-i; j>1; j--)
{
// printing spaces
System.out.print(" ");
}
for (int j=5; j<=i; j++ )
{
System.out.print("a");
}
System.out.println();
}
}
___________________________________