How to get the following output using c?
---1
--12
-345
6789

Please guide me with the code. It can be a blank space instead of '-'.

Recommended Answers

All 8 Replies

I'm getting
1
12
345
6789

by using the following code. Help me to get the expected code.

#include <stdio.h>
#include <conio.h>

int main()
{
	int i,j,m=0;
	clrscr();
	for (i=0;i<4;i++)
	{
	for(k=1;k<=31-i;k++)printf(" ");
	  if(i==0)
		  printf("1");
	  else
		for (j=i+1;j>=1 ;j-- )
		{
		  m=m+1;
		  printf("%d",m);
		}
	  printf("\n");
	}
getch();

	return 0;
}

What are you missing in your display?

What are you missing in your display?

I need to have an equilateral Triangle shape in the output.

I need to have an equilateral Triangle shape in the output.

Yes, so as WaltP asked, what are you missing? What additional output do you need to form the triangle? Where in your code should you be adding whatever 'it' is?

I need to have an equilateral Triangle shape in the output.

hold on, your example triangle you posted, with spaces replacing the dashes, you said you wanted it to look like this:

1
  12
 345
6789

this example is not an equilateral triangle. it's a right triangle, that is right-justified, whereas what youre getting currently is a left-justified one.

1
12
345
6789

an equilateral triangle would look like this

1
 234
56789

now which one do you want?


.

hold on, your example triangle you posted, with spaces replacing the dashes, you said you wanted it to look like this:

1
  12
 345
6789

this example is not an equilateral triangle. it's a right triangle, that is right-justified, whereas what youre getting currently is a left-justified one.

1
12
345
6789

an equilateral triangle would look like this

1
 234
56789

now which one do you want?


.

I need equilateral Triangle.

well, then, you need to print some spaces at the beginning of each line before each number.

how many spaces do you print?

it depends on the length of the number.

think about how you're going to accomplish this.

I'm getting
1
12
345
6789

by using the following code. Help me to get the expected code.

#include <stdio.h>
#include <conio.h>

int main()
{
	int i,j,m=0;
	clrscr();
	for (i=0;i<4;i++)
	{
	for(k=1;k<=31-i;k++)printf(" ");
	  if(i==0)
                  // use space here
		  printf(" 1");
	  else
		for (j=i+1;j>=1 ;j-- )
		{
		  m=m+1;
                  // and space here
		  printf(" %d",m);
		}
	  printf("\n");
	}
getch();

	return 0;
}

Then you get equilateral triangle (more or less so...).
Good luck!

commented: Did this post serve any useful purpose? -2
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.