dimanche 10 juillet 2016

A program that prints the following code

I've been trying to print a pattern in which there is a right triangle and its mirror image and I am unable to figure out the mirror image part.

Pattern:

A       A
AB     BA
ABC   CBA
ABCD DCBA
ABCDEDCBA

Pattern I've been able to print:

A         A 
AB       AB
ABC     ABC
ABCD   ABCD
ABCDE ABCDE 

Here is the code:

public static void main(String args[])
{
    i,j,k,l;
    System.out.print("Enter the numbers of rows you want to print: ");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    for (i=1;i<=n;i++)
    {
        int ch = 65,ch1 = 65;
        for (j=1;j<=i;j++)
        {
            System.out.print((char)ch);
            ch++;
        }
        for (k=n;k>=i+1;k--)
        {
            System.out.print("  ");
        }

        for (l=1;l<=i;l++)
        {
            System.out.print((char)ch1);
            ch1++;
        }
        System.out.println("");
    }
};

Aucun commentaire:

Enregistrer un commentaire