C program to Print Right Angle Triangle

The below code prints a right angle triangle of height h taken from the user,


        *
       **
       *** 
     ****
    *****

Logic: The outer loop controls the number of rows whereas the inner loops control the number of columns.The first inner loop is used to print spaces in  columns and the second one is used to print stars in columns. 

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

void main()
{
    int i,j,k,x;
    printf("Enter height:");
    scanf("%d",&x);

    for(i=x;i>=1;i--)
    {
        for(j=1;j<i;j++)
        {
            printf(" ");
        }
        for(k=x;k>=i;k--)
        {
            printf("*");
        }
        printf("\n");
    }
    getch();
}

Post a Comment

CodeNirvana
Newer Posts Older Posts
© Copyright Encyclopedia of C | Designed By Code Nirvana
Back To Top