hi. im trying to create a program in which the user inputs the character and the level. this would output to a triangle and an inverted triangle. i have already figured out the triangle but i cant seem to get the right output for the inverted triangle

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

void main()

{
	int n;
	int m;
	int o;
	char p;

	clrscr();

	printf("Enter a symbol: ");
	scanf("%c", &p);
	printf("Enter levels for traingles: ");
	scanf("%d", &n);
	printf("\nNormal triangle:\n");



	for (m=1; m<=n; m++)
	{
		printf("\n");

		for (o=1; o<=m; o++)
		{
			printf("%c", p);
		}
	}

	printf("\n\nInverted triangle:\n");

	for (m=n; m>0; m--)
	{

		printf("\n");

		for (o=n; o>0; o--)
		{
			printf("%c", p);
		}
	}
	getch();
}

an example output of the 2nd loop is:

entered character: #
entered level: 6

ouput:
######
######
######
######
######
######

pls help. tnx.

Recommended Answers

All 5 Replies

>for (o=n; o>0; o--)
Each row of the second triangle is based on n, and n doesn't change throughout your program. I believe you want to set o to m instead.

pls expound on what you mean by "set o to m instead."

ok.. now i get it.. thnx narue..

1. main returns int, not void

2. Avoid single letter identifiers (with the exception of i,j as being ubiquitous loop variables). For example, o and 0 make for very unreadable code.

yes. thanks for the tip. but i just used that because so that i could finish typing fast.

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.