WAP to print the following pattern in java. Number of rows will be the input.
Answers
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.
Enter the number of rows: 6
ABCDEFEDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
See attachment for verification.