Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to print a hollow triangle with asterisks

0 votes
asked Jul 10, 2018 by Novice (240 points)
I can make a full triangle with asterisks. What should I do to make a hollow triabgle. Here is the code so far:

#include <stdio.h>

int main()

{

int n,j,i;

printf("enter height > ");

scanf("%d",&n);

for (j=1;j<=n;j++) {

for(i=1;i<=j;i++) {

printf("* ");

}

printf("\n");

}

}

Please give me a detailed answer so that I can understand it. and how it works.

Many many thanks.

3 Answers

0 votes
answered Jul 10, 2018 by anonymous
/******************************************************************************

                            Online C Compiler.
                Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>

int main()
{
    int i, j, rows;

 printf("Enter number of rows : ");
    scanf("%d", &rows);

    for(i=1; i<=rows; i++)
    {
        /* Print trailing spaces */
        for(j=i; j<rows; j++)
        {
            printf(" ");
        }

        /* Print hollow pyramid */
        for(j=1; j<=(2*i-1); j++)
        {
            /*
             * Print star for last row (i==rows),
             * first column(j==1) and for
             * last column (j==(2*i-1)).
             */
            if(i==rows || j==1 || j==(2*i-1))
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        /* Move to next line */
        printf("\n");
    }

    return 0;
}
0 votes
answered Jul 11, 2018 by Vishnu Priyan
#include <stdio.h>

int main()

{

int n,j,i;

printf("enter height > ");

scanf("%d",&n);

for (j=1;j<=n;j++) {

for(i=1;i<=j;i++) {
if(i==1 || i==j && j!=n) //check for boundaries to print "*" and j != n for printing last line with "*"
{
printf("* ");
}
else if(j==n) //printing last line(base of triangle)
{
    printf("* ");
}
else //printing hollow spaces
{
    printf("  ");
}
}

printf("\n");

}

}
0 votes
answered Jul 12, 2018 by anonymous
#include <stdio.h>

int main()

{

int n,j,i;

printf("enter height > ");

scanf("%d",&n);

for (j=1;j<=n;j++)
{

for(i=1;i<=j;i++)
{

printf("* ");

}

printf("\n");

}

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...