Hello there everyone. I am in need of assistance in writing a code for my program. What I have to do is write a code to generate the following:

(1)
*
**
***
****
*****

(2)
*****
****
***
**
*

(3)
*****
****
***
**
*

(4)
*
**
***
****
*****

So far I was able to make a code for (1) and (2) which is:

for(a=0; a<5; a++)
{
for(b=0; b<=a; b++)
printf("*");
printf("\n");
}

for(a=5; a>0; a--)
{
for(b=0; b<a; b++)
printf("*");
printf("\n");
}

Now I have been trying to write code for (3) and (4) but I am unsuccesful. Can anyone guide me in the right direction. Also can someone explain how this works so that in the future I can be quick and efficient. Thank You.

The (3) is supposed to have a triangle that looks like \|
and (4) is supposed to have a triangle that looks like /|
Sorry, but the editor wont let me show the stars the way I want it to

To show shapes and ALWAYS to post code

<< Use [code] tags >>

Then the editor will retain ( more of less ;) ), the shape you want, AND the code will be shown in a font that's easy to study code with.

Click on the [code] icon in the editor, then paste your code. It will automatically put your cursor right between the tags, perfectly.

To draw triangles, general understanding is needed:

You have a max width (number of stars on the longest row).

The mid point of the triangle is (maxWidth/2 + 1) if the number of stars in max width is odd (should always be odd).

When you start the first row, you need to print 32 (space char), until you have counted out maxWidth/2 spaces, then print the * at the top of the pyramid.

Every row thereafter, you will print one fewer (generally) space char's, and two more stars (asterisks).

When you have printed maxWidth stars, you have printed the last row.

Use paper and pencil to work through this, until the light bulb goes off. When you really understand the problem, you'll be able to do pyramids of any orientation.

``````#include<stdio.h>

int main()
{

int i,j,temp;

printf("\nEnter the Number :");
scanf("%d",&temp);

switch(temp)
{
case 1:
for(i=0;i<5;i++)
{

for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
break;

case 2:

for(i=5;i>0;i--)
{

for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
break;

case 3:
for(i=5;i>0;i--)
{

for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}

break;

case 4:
for(i=0;i<5;i++)
{

for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
break;

default:
/* Do nothing*/
}

return 0;
}``````

Adak is absolutely correct.. But what I don't understand is, if you did the code for (1) and (2), (3) and (4) is copy pasting stuff. Anyways here is the code, hope this helps :)

``````#include<stdio.h>

int main()
{

int i,j,temp;

printf("\nEnter the Number :");
scanf("%d",&temp);

switch(temp)
{
case 1:
for(i=0;i<5;i++)
{

for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
break;

case 2:

for(i=5;i>0;i--)
{

for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
break;

case 3:
for(i=5;i>0;i--)
{

for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}

break;

case 4:
for(i=0;i<5;i++)
{

for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
break;

default:
/* Do nothing*/
}

return 0;
}``````

Adak is absolutely correct.. But what I don't understand is, if you did the code for (1) and (2), (3) and (4) is copy pasting stuff. Anyways here is the code, hope this helps :)

You didn't read the entirety of his post, he wanted the triangles like.

``````(1)
*
**
***
****
*****

(2)
*****
****
***
**
*

(3)
*****
****
***
**
*

(4)
*
**
***
****
*****``````
``````#include<stdio.h>
int main()
{
int a,b,n;
printf("Please enter the number of lines.\n");
scanf("%d",&n);
for(a=n;a>0;a--){
for(b=a;b>0;b--){
printf("*");
}
printf("\n");

}
return 0;
}
``````
``````#include<stdio.h>
int main()
{
int i,j,n;
printf("Please enter the number of lines.\n");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.