C program to Print Triangle

The below code prints a triangle of height 'h' taken from the user.

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

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

void main()
{
    int i,j,k,x;
    printf("Enter height of triangle:");
    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");
    }
    printf("\npress any key to continue...");
    getch();
}
 

Post a Comment

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