Hi again all,
I am having some troubles making this program. Here is the assignment:
Code:
Write a computer program that prints a triangle of digits. Your program must read two positive integers as follows:
the width of the triangle, and
the position of the triangle relative to the left margin (the amount to indent the triangle, in other words).
You can assume that the user will always enter non-zero positive integers.
The triangle is drawn upside-down using single digit integers. For example, a width 3 indent 3 triangle looks like this:
123
12
1
... and a width 13 triangle indent 0 triangle looks like this:
1234567890123
123456789012
12345678901
1234567890
123456789
12345678
1234567
123456
12345
1234
123
12
1
and here is my code:
Code:
#include <iostream>
using namespace std;
int main ()
{
int width = 0;
int indent = 0;
int digit = 1;
cout<<"Enter the width of the triangle: ";
cin>>width;
cout<<"Enter the position relative to the left margin (the indent): ";
cin>>indent;
for (int x = 0; x < width; x++)
{
for (int i = 0; i < indent; i++)
{
cout<<" ";
}
for (int w = 0; w < width; w++)
{
cout<<digit%10;
digit++;
}
width--;
digit = 1;
cout<<endl;
}
return 0;
}
When the program is run it looks like this:
Code:
Enter the width of the triangle: 5
Enter the position relative to the left margin (the indent): 3
12345
1234
123
I understand why it quits short but am unable to figure out a fix for the problem.
Anyone got some suggestions? Thank you!