Following code will print the following pattern::
A
A B A
A B C B A
Code::
import java.util.*;
public class Gndec
{
public static void main(String nt[])
{
Scanner in = new Scanner(System.in);
System.out.println("Input the number of rows: ");
int n = in.nextInt();
int d=n; //for space
int count = 1; // for no of characters in a row
char c = 'A';
System.out.println("Pattern is :");
for (int i = 1; i < n; i++)
{
for (int spc =d+3 ;spc > 0; spc--)
//print space
{
System.out.print(" ");
}
d--;
for (int j = 0; j < count; j++)
{
System.out.print(c);//print Character
if (j < count / 2)
{
c++;
} else
{
c--;
}
}
count=count+2;
c = 'A';
System.out.println();
}
}
}
Output:
A
A B A
A B C B A
Code::
import java.util.*;
public class Gndec
{
public static void main(String nt[])
{
Scanner in = new Scanner(System.in);
System.out.println("Input the number of rows: ");
int n = in.nextInt();
int d=n; //for space
int count = 1; // for no of characters in a row
char c = 'A';
System.out.println("Pattern is :");
for (int i = 1; i < n; i++)
{
for (int spc =d+3 ;spc > 0; spc--)
//print space
{
System.out.print(" ");
}
d--;
for (int j = 0; j < count; j++)
{
System.out.print(c);//print Character
if (j < count / 2)
{
c++;
} else
{
c--;
}
}
count=count+2;
c = 'A';
System.out.println();
}
}
}
Output: