I need to print this pattern:
JIHGFEDCBA
IHGFEDCBA
HGFEDCBA
GFEDCBA
FEDCBA
EDCBA
DCBA
CBA
BA
A
The bottom is what I have written so far. What am I doing wrong or what else do I have to add?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
using namespace std;
int main()
{
int n = 10;
for (int i = 1; i <= n ; i++)
{
char letter = 'A';
for (int j = 1; j <= (n+1) - i ; j++)
{
cout << letter;
letter++;
}
cout << endl;
}
cout << endl;
system ("PAUSE");
return 0;
}
|
The code I have written prints out:
ABCDEFGHIJ
ABCDEFGHI
ABCDEFGH
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A
Last edited on
Your current inner loop starts with lowest index and then steps ++j towards the highest. Start from highest and step --j towards the lowest.
[Edit] Oh, your loop index j is a bogus, so changing it does not directly help. You do math with char. Do some more. Start from 'J' rather than 'A'.
Last edited on