Computer Science, asked by ruhanC, 1 month ago

WAP to print the following pattern in java. Number of rows will be the input.

Attachments:

Answers

Answered by anindyaadhikari13
4

\texttt{\textsf{\large{\underline{Solution}:}}}

The given problem is solved using language - Java.

import java.util.*;

public class Pattern{

   public static void main(String s[]){

       Scanner sc=new Scanner(System.in);

       int n,i,j;

       System.out.print("Enter the number of rows: ");

       n=sc.nextInt();

       for(i=1;i<=n;i++){

           char ch='A';

           for(j=1;j<i;j++)

               System.out.print(' ');

           for(j=1;j<=n-i+1;j++)

               System.out.print(ch++);

           ch-=2;

           for(j=1;j<=n-i;j++){

               System.out.print(ch--);

           }

           System.out.println();

       }

   }

}

Note: This program works when 1 ≤ n ≤ 26. For other values of n, strange results will come.

\texttt{\textsf{\large{\underline{Sample I/O}:}}}

Enter the number of rows: 6

ABCDEFEDCBA

 ABCDEDCBA

   ABCDCBA

     ABCBA

       ABA

         A

See attachment for verification.

Attachments:
Similar questions