0
a program to print the following pattern A ABA ABCBA ABCDCBA
without array and using only for loop
2 Respuestas
0
Use char and loop with ASCII codes.
0
You can manipulate characters as if they were numbers (ie manipulating their ascii value), and then cast them back to a char. See an ascii table on the net for the values.
char('A' + 1) is 'B'
char('A' + 2) is 'C'
...
char('A' + 25) is 'Z'
void printSeq(int nb_lines) {
if (nb_lines > 26) {
nb_lines = 26;
}
for(int i = 1; i <= nb_lines; i++) {
for(int j = 0; j < i; j++) { //prints ABCD...
cout << char('A' + j);
}
for(int k = i-2; k >= 0; k--) { //prints ...CBA
cout << char('A' + k);
}
cout << '\n'; //new line
}
}