a program to print the following pattern A ABA ABCBA ABCDCBA | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

a program to print the following pattern A ABA ABCBA ABCDCBA

without array and using only for loop

27th Aug 2016, 4:04 PM
Asim
Asim - avatar
2 Answers
0
Use char and loop with ASCII codes.
27th Aug 2016, 4:59 PM
Tamas Szekffy
Tamas Szekffy - avatar
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 } }
27th Aug 2016, 9:16 PM
Zen
Zen - avatar