954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating inverted triangle in C

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.

timominous
Newbie Poster
5 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

timominous
Newbie Poster
5 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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

timominous
Newbie Poster
5 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

timominous
Newbie Poster
5 posts since Dec 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You