Creating Triangles Using Do..While Loops

  • Context: Comp Sci 
  • Thread starter Thread starter ashooo
  • Start date Start date
  • Tags Tags
    Loops Triangles
Click For Summary
SUMMARY

The forum discussion focuses on creating triangles using Do..While loops in C++. A user shares their code attempting to generate a specific triangle pattern but encounters difficulties, particularly with formatting and output. The code provided includes nested Do..While loops for generating three triangles, with the first triangle being printed incorrectly. Another user suggests using For loops for a simpler implementation and emphasizes the importance of clear code formatting and comments for better understanding.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with Do..While loops in C++
  • Basic knowledge of nested loops and their applications
  • Experience with console output in C++ using printf
NEXT STEPS
  • Learn about C++ For loops and their advantages over Do..While loops
  • Explore formatting output in C++ for better readability
  • Study debugging techniques for C++ code to identify and fix errors
  • Investigate best practices for writing clean and maintainable code in C++
USEFUL FOR

Beginner C++ programmers, educators teaching programming concepts, and anyone interested in mastering loop constructs and output formatting in C++.

ashooo
Messages
1
Reaction score
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
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.
 
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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K