In: Computer Science
Problem
General Statement: Read in a letter and a number. The number indicates how big the letter triangle should be. The number indicating the size of the triangle will have a range from 0 to 250. num>=0 and num<=250
Input: The first number indicates the number of data sets to follow. Each data set will contain one letter and one number. All letter input will be uppercase.
Data File : pr36.dat
Output: Output the letter triangle specified.
Helpful Hints / Assumptions: The letters must wrap around from Z to A. If you start with Z and have to print 5 levels, you must wrap around and start with A after the Z level is complete.
Sample Input :
3
5 A
3 Z
4 C
Sample Output :
A
BB
CCC
DDDD
EEEEE
Z
AA
BBB
C
DD
EEE
FFFF
Pattern.java
import java.io.File;
import java.util.Scanner;
public class Pattern {
static char letters[] = {'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
public static void main(String[] args) {
try {
//reading input
file
File file = new File("pr36.dat");
Scanner input = new Scanner(file);
int n = Integer.parseInt(input.nextLine());
for(int i=0;i<n;i++){
String data[] = input.nextLine().split(" ");
int num = Integer.parseInt(data[0]);
char letter = data[1].charAt(0);
//reading number and letter to print
printPattern(num,letter);
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void printPattern(int num, char
letter) {
//getting index from letters array
based on characters
int index = (int)letter-65;
for(int i=0;i<num;i++){
for(int
j=0;j<i+1;j++){
//printing character that many times
System.out.print(letters[index]);
}
//this is to
iterate if Z came then make it as A
index++;
if(index ==
26){
index = 0;
}
System.out.println();
}
}
}
pr36.dat
3
5 A
3 Z
4 C