Creating Triangles Using Do..While Loops

  • Context: Comp Sci 
  • Thread starter Thread starter ashooo
  • Start date Start date
  • Tags Tags
    Loops Triangles
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
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