Question: C++ program Write a program that asks the user to enter the size of a triangle to write to a textfile (an integer from 1
C++ program
Write a program that asks the user to enter the size of a triangle to write to a textfile (an integer from 1 to 50), then print the triangle into a file by printing a series of lines consisting of asterisks. If the user enters an integer less than 1 or more than 50 tell them it is out of range and have them re-enter it (do not print the triangle for the invalid input, only for the final value in the range of 1-50 inclusive: 1 and 50 are both ok).
For example, if the user enters 5, the output would be:
*
**
***
****
*****
****
***
**
*
This is my code :
#include
#include
using namespace std;
int main()
{
int row;
int colum;
int num;
int space;
//enter inputs from the user
cout<< "Please enter a number to form a tringle from 1 to 50: ";
cin>>num;
{
for(int row = 0; row <= num; row++)
{
for(space = 0; space < row; space++)
{
cout<<"*";
}
cout<
}
for(int row= num; row > num;row--)
{
for(int c=0; c < row; c++)
{
cout<<"*";
}
cout<
}
for (int r = num-1; r>0;r--)
{
for(int c=0; c< r;c++)
{
cout<<"*";
}
cout<
}
}
return 0;
}
How can I use the loops to finish my code
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
