Creating Triangles Using Do..While Loops

In summary: This conversation is about a person who is new to C++ and is struggling with an assignment to create several triangles using Do...While loops. They have split the shapes into a top and bottom half but are having trouble getting the code to work. Another person suggests using the for loop instead and provides a simpler example.
  • #1
ashooo
1
0
Hey,
Im pretty new to c++ so my knowledge is limited.
I have been set an assignment to create the following triangles only using Do...While loops. I'm having difficulty trying to code it.
The underscores are just their to illustrate spaces.

1 *_________________5 * 5__________* * * * 1 * ____3 *
__2 * *_____________* 4 *___________* * * 2 *______2 * *
____3 * * *______3 * 3 * 3 * 3________* * 3 * ______1 * * *
__4 * * * *______* 2 * 2 * 2 * ________* 4 * _____4 * * * *
5 * * * * *___1 * 1 * 1 * 1 * 1 * 1______5 *____5 * * * * *

I have split the shapes into two, with the top half being 3 lines and the bottom half the remaining 2. However, as fas as i can tell this only works for the first shape.

#include <stdio.h>

void main()
{
// Declaring the variables
int row,space,number,x, number1, number2;

// Inserting numbers into variables
row=1;
number=1;
number1=1;
number2=1;

do // Top of shapes
{
space=1; // First Triangle
do
{
printf(" ");
space++;
}
while (space<=row);


printf("%d", number);
number++;


x=1;
do
{
printf("* ");
x++;
}
while (x<=row);


space=3;
do
{
printf(" ");
space--;
}
while (space>=row);






space=5; // Second Triangle
do
{
printf(" ");
space--;
}
while (space>=row);

x=1;
do
{
printf("%d * ", number1);
x++;
}
while (x<=row);

printf("%d", number1);
number1++;

space=5;
do
{
printf(" ");
space--;
}
while (space>=row);



printf("\t");




space=1; // Third Triangle
do
{
printf(" ");
space++;
}
while (space<=row);

x=5;
do
{
printf("* ");
x--;
}
while (x>=row+1);

printf("%d *", number2);
number2++;






printf("\n");
row++;
}
while (row<=3);




do // Bottom Of shape
{
space=5; // First Shape
do
{
printf(" ");
space--;
}
while(space>=row);

printf("%d", number);
number++;


x=1;
do
{
printf("* ");
x++;
}
while (x<=row);


space=1;
do
{
printf(" ");
space++;
}
while (space<=1);



space=5; // Second Triangle
do
{
printf(" ");
space--;
}
while (space>=row);

x=1;
do
{
printf("%d * ", number1);
x++;
}
while (x<=row);

printf("%d", number1);
number1++;

space=5;
do
{
printf(" ");
space--;
}
while (space>=row);


printf("\t");


space=1; // Third Triangle
do
{
printf(" ");
space++;
}
while (space<=row);


printf("* ");
printf("%d *", number2);
number2++;


printf("\n");
row++;
}
while (row<=5);
}
 
Physics news on Phys.org
  • #2
It would help if you showed what output that gave you. It is kind of hard working with uncommented, unspaced code. Try using the
Code:
command? I think they have that here.
 
  • #3
why to write such a big progarm.let me say you in a simple method using for loops.
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,j;
clrscr();
printf("enter the elements");
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
if you will declare i in place of ("%d",i) you will get the reverse triangle
 

1. How is a triangle created using Do..While Loops?

A triangle can be created using Do..While Loops by using a nested loop structure. The outer loop controls the number of rows in the triangle, while the inner loop controls the number of stars in each row. The number of stars in each row increases by one with each iteration of the inner loop, creating a triangular shape.

2. What are the advantages of using Do..While Loops for creating triangles?

Do..While Loops are advantageous for creating triangles because they allow for a more efficient and concise code. The number of rows and stars in each row can be easily controlled using the loop structure, making it easier to create different sizes and shapes of triangles.

3. Are Do..While Loops the only way to create triangles in programming?

No, there are other ways to create triangles in programming such as using for loops or if/else statements. However, Do..While Loops are a popular choice due to their simplicity and efficiency in creating triangle shapes.

4. Can you create different types of triangles using Do..While Loops?

Yes, Do..While Loops can be used to create various types of triangles such as equilateral, isosceles, and right triangles. The shape and size of the triangle can be easily controlled by adjusting the conditions of the loop.

5. What are some common mistakes to avoid when using Do..While Loops to create triangles?

One common mistake to avoid is not properly incrementing the loop counter, which can result in an infinite loop. It is also important to pay attention to the conditions of the loop to ensure the correct shape and size of the triangle is created. Additionally, make sure to properly close the loops to avoid any errors in the code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
668
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top