Home Tools

Triangle program in C language

Hi friends, today I write a simple C program to create triangle pattern structure and its very easy and fun also.
triangle.c
1#include<stdio.h>
2int main() {
3	int triangleHeight = 0;
4	int triangleWidth = 0;
5	
6	int middlePoint = 0;
7	int startPoint = 0;
8	int endPoint = 0;
9	
10	int outerLoop = 0;
11	int innerLoop = 0;
12	
13	printf("Please enter the triangle Height:");
14	scanf("%d",&triangleHeight);
15	if(triangleHeight >= 2) {
16		
17		triangleWidth = triangleHeight * 2;
18		if(triangleWidth%2 == 1)
19		{	triangleWidth++;	}
20		
21		middlePoint = triangleWidth/2;
22		startPoint = middlePoint;
23		endPoint = middlePoint;
24		
25		for(outerLoop=1;outerLoop<=triangleHeight;outerLoop++) {
26			for(innerLoop=1;innerLoop<=triangleWidth;innerLoop++) {
27				if(startPoint <= innerLoop && innerLoop <= endPoint) 
28				{	printf("*");	}
29				else
30				{	printf(" ");	}
31			}
32			startPoint--;
33			endPoint++;
34			printf("\n");
35		}
36	}
37	return 0;
38}

Output

Please enter the triangle Height:6
     *      
    ***     
   *****    
  *******   
 *********  
***********